2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim:cindent:ts=2:et:sw=2:
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* This Original Code has been modified by IBM Corporation. Modifications made by IBM
|
|
|
|
* described herein are Copyright (c) International Business Machines Corporation, 2000.
|
|
|
|
* Modifications to Mozilla code or documentation identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. OS/2 VisualAge build.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsPropertyTable allows a set of arbitrary key/value pairs to be stored
|
|
|
|
* for any number of nodes, in a global hashtable rather than on the nodes
|
|
|
|
* themselves. Nodes can be any type of object; the hashtable keys are
|
|
|
|
* nsIAtom pointers, and the values are void pointers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsPropertyTable.h"
|
|
|
|
#include "pldhash.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIAtom.h"
|
|
|
|
|
|
|
|
struct PropertyListMapEntry : public PLDHashEntryHdr {
|
|
|
|
const void *key;
|
|
|
|
void *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsPropertyTable::PropertyList {
|
|
|
|
public:
|
2010-04-22 19:41:38 -07:00
|
|
|
PropertyList(nsIAtom* aName,
|
2007-03-22 10:30:00 -07:00
|
|
|
NSPropertyDtorFunc aDtorFunc,
|
|
|
|
void* aDtorData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aTransfer) NS_HIDDEN;
|
2007-03-22 10:30:00 -07:00
|
|
|
~PropertyList() NS_HIDDEN;
|
|
|
|
|
|
|
|
// Removes the property associated with the given object, and destroys
|
|
|
|
// the property value
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_HIDDEN_(bool) DeletePropertyFor(nsPropertyOwner aObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Destroy all remaining properties (without removing them)
|
|
|
|
NS_HIDDEN_(void) Destroy();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_HIDDEN_(bool) Equals(nsIAtom *aPropertyName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-22 19:41:38 -07:00
|
|
|
return mName == aPropertyName;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-14 21:10:08 -07:00
|
|
|
size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIAtom> mName; // property name
|
|
|
|
PLDHashTable mObjectValueMap; // map of object/value pairs
|
|
|
|
NSPropertyDtorFunc mDtorFunc; // property specific value dtor function
|
|
|
|
void* mDtorData; // pointer to pass to dtor
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mTransfer; // whether to transfer in
|
2007-03-22 10:30:00 -07:00
|
|
|
// TransferOrDeleteAllPropertiesFor
|
|
|
|
|
|
|
|
PropertyList* mNext;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPropertyTable::DeleteAllProperties()
|
|
|
|
{
|
|
|
|
while (mPropertyList) {
|
|
|
|
PropertyList* tmp = mPropertyList;
|
|
|
|
|
|
|
|
mPropertyList = mPropertyList->mNext;
|
|
|
|
tmp->Destroy();
|
|
|
|
delete tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPropertyTable::DeleteAllPropertiesFor(nsPropertyOwner aObject)
|
|
|
|
{
|
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
prop->DeletePropertyFor(aObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPropertyTable::TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject,
|
|
|
|
nsPropertyTable *aOtherTable)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
if (prop->mTransfer) {
|
2007-07-08 00:08:04 -07:00
|
|
|
PropertyListMapEntry *entry = static_cast<PropertyListMapEntry*>
|
|
|
|
(PL_DHashTableOperate(&prop->mObjectValueMap, aObject,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
|
2010-04-22 19:41:38 -07:00
|
|
|
rv = aOtherTable->SetProperty(aObject, prop->mName,
|
2007-03-22 10:30:00 -07:00
|
|
|
entry->value, prop->mDtorFunc,
|
|
|
|
prop->mDtorData, prop->mTransfer);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
DeleteAllPropertiesFor(aObject);
|
|
|
|
aOtherTable->DeleteAllPropertiesFor(aObject);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PL_DHashTableRawRemove(&prop->mObjectValueMap, entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prop->DeletePropertyFor(aObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-22 19:41:38 -07:00
|
|
|
nsPropertyTable::Enumerate(nsPropertyOwner aObject,
|
2007-03-22 10:30:00 -07:00
|
|
|
NSPropertyFunc aCallback, void *aData)
|
|
|
|
{
|
|
|
|
PropertyList* prop;
|
|
|
|
for (prop = mPropertyList; prop; prop = prop->mNext) {
|
2010-04-22 19:41:38 -07:00
|
|
|
PropertyListMapEntry *entry = static_cast<PropertyListMapEntry*>
|
|
|
|
(PL_DHashTableOperate(&prop->mObjectValueMap, aObject, PL_DHASH_LOOKUP));
|
|
|
|
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
|
|
|
|
aCallback(const_cast<void*>(aObject.get()), prop->mName, entry->value,
|
|
|
|
aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-15 11:02:06 -08:00
|
|
|
struct PropertyEnumeratorData
|
|
|
|
{
|
|
|
|
nsIAtom* mName;
|
|
|
|
NSPropertyFunc mCallBack;
|
|
|
|
void* mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
PropertyEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aNumber, void* aArg)
|
2012-01-15 11:02:06 -08:00
|
|
|
{
|
|
|
|
PropertyListMapEntry* entry = static_cast<PropertyListMapEntry*>(aHdr);
|
|
|
|
PropertyEnumeratorData* data = static_cast<PropertyEnumeratorData*>(aArg);
|
|
|
|
data->mCallBack(const_cast<void*>(entry->key), data->mName, entry->value,
|
|
|
|
data->mData);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPropertyTable::EnumerateAll(NSPropertyFunc aCallBack, void* aData)
|
|
|
|
{
|
|
|
|
for (PropertyList* prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
PropertyEnumeratorData data = { prop->mName, aCallBack, aData };
|
|
|
|
PL_DHashTableEnumerate(&prop->mObjectValueMap, PropertyEnumerator, &data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void*
|
|
|
|
nsPropertyTable::GetPropertyInternal(nsPropertyOwner aObject,
|
|
|
|
nsIAtom *aPropertyName,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aRemove,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult *aResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aPropertyName && aObject, "unexpected null param");
|
|
|
|
nsresult rv = NS_PROPTABLE_PROP_NOT_THERE;
|
2012-07-30 07:20:58 -07:00
|
|
|
void *propValue = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-22 19:41:38 -07:00
|
|
|
PropertyList* propertyList = GetPropertyListFor(aPropertyName);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (propertyList) {
|
2007-07-08 00:08:04 -07:00
|
|
|
PropertyListMapEntry *entry = static_cast<PropertyListMapEntry*>
|
|
|
|
(PL_DHashTableOperate(&propertyList->mObjectValueMap, aObject,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_LOOKUP));
|
|
|
|
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
|
|
|
|
propValue = entry->value;
|
|
|
|
if (aRemove) {
|
|
|
|
// don't call propertyList->mDtorFunc. That's the caller's job now.
|
|
|
|
PL_DHashTableRawRemove(&propertyList->mObjectValueMap, entry);
|
|
|
|
}
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aResult)
|
|
|
|
*aResult = rv;
|
|
|
|
|
|
|
|
return propValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPropertyTable::SetPropertyInternal(nsPropertyOwner aObject,
|
|
|
|
nsIAtom *aPropertyName,
|
|
|
|
void *aPropertyValue,
|
|
|
|
NSPropertyDtorFunc aPropDtorFunc,
|
|
|
|
void *aPropDtorData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aTransfer,
|
2007-03-22 10:30:00 -07:00
|
|
|
void **aOldValue)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aPropertyName && aObject, "unexpected null param");
|
|
|
|
|
2010-04-22 19:41:38 -07:00
|
|
|
PropertyList* propertyList = GetPropertyListFor(aPropertyName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (propertyList) {
|
|
|
|
// Make sure the dtor function and data and the transfer flag match
|
|
|
|
if (aPropDtorFunc != propertyList->mDtorFunc ||
|
|
|
|
aPropDtorData != propertyList->mDtorData ||
|
|
|
|
aTransfer != propertyList->mTransfer) {
|
|
|
|
NS_WARNING("Destructor/data mismatch while setting property");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2010-04-22 19:41:38 -07:00
|
|
|
propertyList = new PropertyList(aPropertyName, aPropDtorFunc,
|
2007-03-22 10:30:00 -07:00
|
|
|
aPropDtorData, aTransfer);
|
|
|
|
if (!propertyList || !propertyList->mObjectValueMap.ops) {
|
|
|
|
delete propertyList;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
propertyList->mNext = mPropertyList;
|
|
|
|
mPropertyList = propertyList;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current property value (if there is one) is replaced and the current
|
|
|
|
// value is destroyed
|
|
|
|
nsresult result = NS_OK;
|
2007-07-08 00:08:04 -07:00
|
|
|
PropertyListMapEntry *entry = static_cast<PropertyListMapEntry*>
|
|
|
|
(PL_DHashTableOperate(&propertyList->mObjectValueMap, aObject, PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-04-02 18:14:24 -07:00
|
|
|
// A nullptr entry->key is the sign that the entry has just been allocated
|
|
|
|
// for us. If it's non-nullptr then we have an existing entry.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (entry->key) {
|
|
|
|
if (aOldValue)
|
|
|
|
*aOldValue = entry->value;
|
|
|
|
else if (propertyList->mDtorFunc)
|
2007-07-08 00:08:04 -07:00
|
|
|
propertyList->mDtorFunc(const_cast<void*>(entry->key), aPropertyName,
|
2007-03-22 10:30:00 -07:00
|
|
|
entry->value, propertyList->mDtorData);
|
|
|
|
result = NS_PROPTABLE_PROP_OVERWRITTEN;
|
|
|
|
}
|
|
|
|
else if (aOldValue) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*aOldValue = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
entry->key = aObject;
|
|
|
|
entry->value = aPropertyValue;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPropertyTable::DeleteProperty(nsPropertyOwner aObject,
|
|
|
|
nsIAtom *aPropertyName)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aPropertyName && aObject, "unexpected null param");
|
|
|
|
|
2010-04-22 19:41:38 -07:00
|
|
|
PropertyList* propertyList = GetPropertyListFor(aPropertyName);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (propertyList) {
|
|
|
|
if (propertyList->DeletePropertyFor(aObject))
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_PROPTABLE_PROP_NOT_THERE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPropertyTable::PropertyList*
|
2010-04-22 19:41:38 -07:00
|
|
|
nsPropertyTable::GetPropertyListFor(nsIAtom* aPropertyName) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PropertyList* result;
|
|
|
|
|
|
|
|
for (result = mPropertyList; result; result = result->mNext) {
|
2010-04-22 19:41:38 -07:00
|
|
|
if (result->Equals(aPropertyName)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2010-04-22 19:41:38 -07:00
|
|
|
nsPropertyTable::PropertyList::PropertyList(nsIAtom *aName,
|
2007-03-22 10:30:00 -07:00
|
|
|
NSPropertyDtorFunc aDtorFunc,
|
|
|
|
void *aDtorData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aTransfer)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mName(aName),
|
|
|
|
mDtorFunc(aDtorFunc),
|
|
|
|
mDtorData(aDtorData),
|
|
|
|
mTransfer(aTransfer),
|
2012-07-30 07:20:58 -07:00
|
|
|
mNext(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PL_DHashTableInit(&mObjectValueMap, PL_DHashGetStubOps(), this,
|
|
|
|
sizeof(PropertyListMapEntry), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPropertyTable::PropertyList::~PropertyList()
|
|
|
|
{
|
|
|
|
PL_DHashTableFinish(&mObjectValueMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-03-22 10:30:00 -07:00
|
|
|
DestroyPropertyEnumerator(PLDHashTable *table, PLDHashEntryHdr *hdr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t number, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsPropertyTable::PropertyList *propList =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsPropertyTable::PropertyList*>(table->data);
|
|
|
|
PropertyListMapEntry* entry = static_cast<PropertyListMapEntry*>(hdr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
propList->mDtorFunc(const_cast<void*>(entry->key), propList->mName,
|
2007-03-22 10:30:00 -07:00
|
|
|
entry->value, propList->mDtorData);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPropertyTable::PropertyList::Destroy()
|
|
|
|
{
|
|
|
|
// Enumerate any remaining object/value pairs and destroy the value object
|
|
|
|
if (mDtorFunc)
|
|
|
|
PL_DHashTableEnumerate(&mObjectValueMap, DestroyPropertyEnumerator,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPropertyTable::PropertyList::DeletePropertyFor(nsPropertyOwner aObject)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
PropertyListMapEntry *entry = static_cast<PropertyListMapEntry*>
|
|
|
|
(PL_DHashTableOperate(&mObjectValueMap, aObject, PL_DHASH_LOOKUP));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!PL_DHASH_ENTRY_IS_BUSY(entry))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void* value = entry->value;
|
|
|
|
PL_DHashTableRawRemove(&mObjectValueMap, entry);
|
|
|
|
|
|
|
|
if (mDtorFunc)
|
2007-07-08 00:08:04 -07:00
|
|
|
mDtorFunc(const_cast<void*>(aObject.get()), mName, value, mDtorData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-14 21:10:08 -07:00
|
|
|
size_t
|
|
|
|
nsPropertyTable::PropertyList::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
|
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
2013-04-02 18:14:24 -07:00
|
|
|
n += PL_DHashTableSizeOfExcludingThis(&mObjectValueMap, nullptr, aMallocSizeOf);
|
2012-06-14 21:10:08 -07:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsPropertyTable::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
for (PropertyList *prop = mPropertyList; prop; prop = prop->mNext) {
|
|
|
|
n += prop->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsPropertyTable::SupportsDtorFunc(void *aObject, nsIAtom *aPropertyName,
|
|
|
|
void *aPropertyValue, void *aData)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsISupports *propertyValue = static_cast<nsISupports*>(aPropertyValue);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IF_RELEASE(propertyValue);
|
|
|
|
}
|