2015-05-03 22:32:37 +03: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
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2017-08-17 00:10:56 +03:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-10-03 01:05:19 +03:00
|
|
|
* nsAtom pointers, and the values are void pointers.
|
2004-08-24 22:50:29 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef nsPropertyTable_h_
|
|
|
|
#define nsPropertyTable_h_
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2004-08-24 22:50:29 +04:00
|
|
|
#include "nscore.h"
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
class nsAtom;
|
2005-10-27 18:09:23 +04:00
|
|
|
|
|
|
|
typedef void
|
|
|
|
(*NSPropertyFunc)(void *aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName,
|
2005-10-27 18:09:23 +04:00
|
|
|
void *aPropertyValue,
|
|
|
|
void *aData);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback type for property destructors. |aObject| is the object
|
|
|
|
* the property is being removed for, |aPropertyName| is the property
|
|
|
|
* being removed, |aPropertyValue| is the value of the property, and |aData|
|
|
|
|
* is the opaque destructor data that was passed to SetProperty().
|
|
|
|
**/
|
2005-10-27 18:09:23 +04:00
|
|
|
typedef NSPropertyFunc NSPropertyDtorFunc;
|
2006-01-27 02:40:49 +03:00
|
|
|
class nsINode;
|
|
|
|
class nsIFrame;
|
|
|
|
|
|
|
|
class nsPropertyOwner
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsPropertyOwner(const nsPropertyOwner& aOther) : mObject(aOther.mObject) {}
|
|
|
|
|
|
|
|
// These are the types of objects that can own properties. No object should
|
|
|
|
// inherit more then one of these classes.
|
|
|
|
// To add support for more types just add to this list.
|
2014-08-05 17:19:51 +04:00
|
|
|
MOZ_IMPLICIT nsPropertyOwner(const nsINode* aObject) : mObject(aObject) {}
|
|
|
|
MOZ_IMPLICIT nsPropertyOwner(const nsIFrame* aObject) : mObject(aObject) {}
|
2006-01-27 02:40:49 +03:00
|
|
|
|
|
|
|
operator const void*() { return mObject; }
|
|
|
|
const void* get() { return mObject; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const void* mObject;
|
|
|
|
};
|
2005-10-27 18:09:23 +04:00
|
|
|
|
2004-08-24 22:50:29 +04:00
|
|
|
class nsPropertyTable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Get the value of the property |aPropertyName| for node |aObject|.
|
|
|
|
* |aResult|, if supplied, is filled in with a return status code.
|
|
|
|
**/
|
2016-11-15 01:15:46 +03:00
|
|
|
void* GetProperty(const nsPropertyOwner& aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName,
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult *aResult = nullptr)
|
2005-10-27 18:09:23 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return GetPropertyInternal(aObject, aPropertyName, false, aResult);
|
2005-10-27 18:09:23 +04:00
|
|
|
}
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
/**
|
2010-04-23 06:41:38 +04:00
|
|
|
* Set the value of the property |aPropertyName| to
|
2005-10-27 18:09:23 +04:00
|
|
|
* |aPropertyValue| for node |aObject|. |aDtor| is a destructor for the
|
|
|
|
* property value to be called if the property is removed. It can be null
|
|
|
|
* if no destructor is required. |aDtorData| is an optional pointer to an
|
|
|
|
* opaque context to be passed to the property destructor. Note that the
|
|
|
|
* destructor is global for each property name regardless of node; it is an
|
|
|
|
* error to set a given property with a different destructor than was used
|
|
|
|
* before (this will return NS_ERROR_INVALID_ARG). If aOldValue is non-null
|
|
|
|
* it will contain the old value after the function returns (the destructor
|
2011-10-17 18:59:28 +04:00
|
|
|
* for the old value will not be run in that case). If |aTransfer| is true
|
2006-09-16 21:15:38 +04:00
|
|
|
* the property will be transfered to the new table when the property table
|
|
|
|
* for |aObject| changes (currently the tables for nodes are owned by their
|
|
|
|
* ownerDocument, so if the ownerDocument for a node changes, its property
|
2011-10-17 18:59:28 +04:00
|
|
|
* table changes too). If |aTransfer| is false the property will just be
|
2006-09-16 21:15:38 +04:00
|
|
|
* deleted instead.
|
2005-10-27 18:09:23 +04:00
|
|
|
*/
|
2016-11-15 01:15:46 +03:00
|
|
|
nsresult SetProperty(const nsPropertyOwner& aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName,
|
2004-08-24 22:50:29 +04:00
|
|
|
void *aPropertyValue,
|
|
|
|
NSPropertyDtorFunc aDtor,
|
2005-10-27 18:09:23 +04:00
|
|
|
void *aDtorData,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aTransfer = false,
|
2012-07-30 18:20:58 +04:00
|
|
|
void **aOldValue = nullptr)
|
2005-10-27 18:09:23 +04:00
|
|
|
{
|
2010-04-23 06:41:38 +04:00
|
|
|
return SetPropertyInternal(aObject, aPropertyName, aPropertyValue,
|
2006-09-16 21:15:38 +04:00
|
|
|
aDtor, aDtorData, aTransfer, aOldValue);
|
2005-10-27 18:09:23 +04:00
|
|
|
}
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2005-10-27 18:09:23 +04:00
|
|
|
/**
|
|
|
|
* Delete the property |aPropertyName| in the global category for object
|
|
|
|
* |aObject|. The property's destructor function will be called.
|
|
|
|
*/
|
2014-06-02 16:08:21 +04:00
|
|
|
nsresult DeleteProperty(nsPropertyOwner aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
/**
|
2005-10-27 18:09:23 +04:00
|
|
|
* Unset the property |aPropertyName| in the global category for object
|
|
|
|
* |aObject|, but do not call the property's destructor function. The
|
|
|
|
* property value is returned.
|
|
|
|
*/
|
2016-11-15 01:15:46 +03:00
|
|
|
void* UnsetProperty(const nsPropertyOwner& aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName,
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult *aStatus = nullptr)
|
2005-10-27 18:09:23 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return GetPropertyInternal(aObject, aPropertyName, true, aStatus);
|
2005-10-27 18:09:23 +04:00
|
|
|
}
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes all of the properties for object |aObject|, calling the
|
|
|
|
* destructor function for each property.
|
2005-10-27 18:09:23 +04:00
|
|
|
*/
|
2014-06-02 16:08:21 +04:00
|
|
|
void DeleteAllPropertiesFor(nsPropertyOwner aObject);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2006-09-16 21:15:38 +04:00
|
|
|
/**
|
|
|
|
* Transfers all properties for object |aObject| that were set with the
|
2011-10-17 18:59:28 +04:00
|
|
|
* |aTransfer| argument as true to |aTable|. Deletes the other properties
|
2006-09-16 21:15:38 +04:00
|
|
|
* for object |aObject|, calling the destructor function for each property.
|
|
|
|
* If transfering a property fails, this deletes all the properties for
|
|
|
|
* object |aObject|.
|
|
|
|
*/
|
2014-06-02 16:08:21 +04:00
|
|
|
nsresult
|
2006-09-16 21:15:38 +04:00
|
|
|
TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject,
|
|
|
|
nsPropertyTable *aOtherTable);
|
|
|
|
|
2005-10-27 18:09:23 +04:00
|
|
|
/**
|
2010-04-23 06:41:38 +04:00
|
|
|
* Enumerate the properties for object |aObject|.
|
2005-10-27 18:09:23 +04:00
|
|
|
* For every property |aCallback| will be called with as arguments |aObject|,
|
|
|
|
* the property name, the property value and |aData|.
|
|
|
|
*/
|
2014-06-02 16:08:21 +04:00
|
|
|
void Enumerate(nsPropertyOwner aObject,
|
2005-10-27 18:09:23 +04:00
|
|
|
NSPropertyFunc aCallback, void *aData);
|
|
|
|
|
2012-01-15 23:02:06 +04:00
|
|
|
/**
|
|
|
|
* Enumerate all the properties.
|
|
|
|
* For every property |aCallback| will be called with arguments the owner,
|
|
|
|
* the property name, the property value and |aData|.
|
|
|
|
*/
|
2014-06-02 16:08:21 +04:00
|
|
|
void EnumerateAll(NSPropertyFunc aCallback, void *aData);
|
2012-01-15 23:02:06 +04:00
|
|
|
|
2005-12-06 07:53:03 +03:00
|
|
|
/**
|
|
|
|
* Deletes all of the properties for all objects in the property
|
|
|
|
* table, calling the destructor function for each property.
|
|
|
|
*/
|
2014-06-02 16:08:21 +04:00
|
|
|
void DeleteAllProperties();
|
2010-04-23 06:41:38 +04:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
nsPropertyTable() : mPropertyList(nullptr) {}
|
2005-12-06 07:53:03 +03:00
|
|
|
~nsPropertyTable() {
|
|
|
|
DeleteAllProperties();
|
|
|
|
}
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2006-02-01 07:43:55 +03:00
|
|
|
/**
|
|
|
|
* Function useable as destructor function for property data that is
|
|
|
|
* XPCOM objects. The function will call NS_IF_RELASE on the value
|
|
|
|
* to destroy it.
|
|
|
|
*/
|
2017-10-03 01:05:19 +03:00
|
|
|
static void SupportsDtorFunc(void *aObject, nsAtom *aPropertyName,
|
2006-02-01 07:43:55 +03:00
|
|
|
void *aPropertyValue, void *aData);
|
|
|
|
|
2006-01-27 02:40:49 +03:00
|
|
|
class PropertyList;
|
2004-08-24 22:50:29 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2014-05-01 21:37:54 +04:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-06-15 08:10:08 +04:00
|
|
|
|
2004-08-24 22:50:29 +04:00
|
|
|
private:
|
2014-06-02 16:08:21 +04:00
|
|
|
void DestroyPropertyList();
|
2017-10-03 01:05:19 +03:00
|
|
|
PropertyList* GetPropertyListFor(nsAtom *aPropertyName) const;
|
2014-06-02 16:08:21 +04:00
|
|
|
void* GetPropertyInternal(nsPropertyOwner aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aRemove,
|
2004-08-24 22:50:29 +04:00
|
|
|
nsresult *aStatus);
|
2014-06-02 16:08:21 +04:00
|
|
|
nsresult SetPropertyInternal(nsPropertyOwner aObject,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aPropertyName,
|
2005-10-27 18:09:23 +04:00
|
|
|
void *aPropertyValue,
|
|
|
|
NSPropertyDtorFunc aDtor,
|
|
|
|
void *aDtorData,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aTransfer,
|
2005-10-27 18:09:23 +04:00
|
|
|
void **aOldValue);
|
2004-08-24 22:50:29 +04:00
|
|
|
|
|
|
|
PropertyList *mPropertyList;
|
|
|
|
};
|
|
|
|
#endif
|