2014-05-24 01:32:38 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for representing MozMap arguments. This is an nsTHashtable
|
|
|
|
* under the hood, but we don't want to leak that implementation
|
|
|
|
* detail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_MozMap_h
|
|
|
|
#define mozilla_dom_MozMap_h
|
|
|
|
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsTArray.h"
|
2015-03-19 10:46:40 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-05-24 01:32:38 +04:00
|
|
|
#include "mozilla/Move.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace binding_detail {
|
|
|
|
template<typename DataType>
|
|
|
|
class MozMapEntry : public nsStringHashKey
|
|
|
|
{
|
|
|
|
public:
|
2014-08-08 17:49:39 +04:00
|
|
|
explicit MozMapEntry(const nsAString* aKeyTypePointer)
|
2014-05-24 01:32:38 +04:00
|
|
|
: nsStringHashKey(aKeyTypePointer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move constructor so we can do MozMaps of MozMaps.
|
|
|
|
MozMapEntry(MozMapEntry<DataType>&& aOther)
|
|
|
|
: nsStringHashKey(aOther),
|
|
|
|
mData(Move(aOther.mData))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DataType mData;
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
|
2014-05-24 01:32:38 +04:00
|
|
|
} // namespace binding_detail
|
|
|
|
|
|
|
|
template<typename DataType>
|
|
|
|
class MozMap : protected nsTHashtable<binding_detail::MozMapEntry<DataType>>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename binding_detail::MozMapEntry<DataType> EntryType;
|
|
|
|
typedef nsTHashtable<EntryType> Base;
|
|
|
|
typedef MozMap<DataType> SelfType;
|
|
|
|
|
|
|
|
MozMap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move constructor so we can do MozMap of MozMap.
|
|
|
|
MozMap(SelfType&& aOther) :
|
|
|
|
Base(Move(aOther))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// The return value is only safe to use until an AddEntry call.
|
|
|
|
const DataType& Get(const nsAString& aKey) const
|
|
|
|
{
|
|
|
|
const EntryType* ent = this->GetEntry(aKey);
|
|
|
|
MOZ_ASSERT(ent, "Why are you using a key we didn't claim to have?");
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:25:09 +04:00
|
|
|
DataType& Get(const nsAString& aKey)
|
|
|
|
{
|
|
|
|
EntryType* ent = this->GetEntry(aKey);
|
|
|
|
MOZ_ASSERT(ent, "Why are you using a key we didn't claim to have?");
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2014-05-24 01:32:38 +04:00
|
|
|
// The return value is only safe to use until an AddEntry call.
|
|
|
|
const DataType* GetIfExists(const nsAString& aKey) const
|
|
|
|
{
|
|
|
|
const EntryType* ent = this->GetEntry(aKey);
|
|
|
|
if (!ent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &ent->mData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetKeys(nsTArray<nsString>& aKeys) const {
|
2015-07-14 17:49:30 +03:00
|
|
|
for (auto iter = this->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
aKeys.AppendElement(iter.Get()->GetKey());
|
|
|
|
}
|
2014-05-24 01:32:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXXbz we expose this generic enumerator for tracing. Otherwise we'd end up
|
|
|
|
// with a dependency on BindingUtils.h here for the SequenceTracer bits.
|
2015-07-14 17:49:30 +03:00
|
|
|
typedef void (* Enumerator)(DataType* aValue, void* aClosure);
|
2014-05-24 01:32:38 +04:00
|
|
|
void EnumerateValues(Enumerator aEnumerator, void *aClosure)
|
|
|
|
{
|
2015-07-14 17:49:30 +03:00
|
|
|
for (auto iter = this->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
aEnumerator(&iter.Get()->mData, aClosure);
|
|
|
|
}
|
2014-05-24 01:32:38 +04:00
|
|
|
}
|
|
|
|
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE
|
2015-03-19 10:46:40 +03:00
|
|
|
DataType* AddEntry(const nsAString& aKey)
|
2014-05-24 01:32:38 +04:00
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
EntryType* ent = this->PutEntry(aKey, fallible);
|
2014-05-24 01:32:38 +04:00
|
|
|
if (!ent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &ent->mData;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_MozMap_h
|