content/base/src/nsPropertyTable.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsPropertyTable.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,199 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim:cindent:ts=2:et:sw=2:
     1.6 + *
     1.7 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    1.10 + *
    1.11 + * This Original Code has been modified by IBM Corporation. Modifications made by IBM 
    1.12 + * described herein are Copyright (c) International Business Machines Corporation, 2000.
    1.13 + * Modifications to Mozilla code or documentation identified per MPL Section 3.3
    1.14 + *
    1.15 + * Date             Modified by     Description of modification
    1.16 + * 04/20/2000       IBM Corp.      OS/2 VisualAge build.
    1.17 + */
    1.18 +
    1.19 +/**
    1.20 + * nsPropertyTable allows a set of arbitrary key/value pairs to be stored
    1.21 + * for any number of nodes, in a global hashtable rather than on the nodes
    1.22 + * themselves.  Nodes can be any type of object; the hashtable keys are
    1.23 + * nsIAtom pointers, and the values are void pointers.
    1.24 + */
    1.25 +
    1.26 +#ifndef nsPropertyTable_h_
    1.27 +#define nsPropertyTable_h_
    1.28 +
    1.29 +#include "mozilla/MemoryReporting.h"
    1.30 +#include "nscore.h"
    1.31 +
    1.32 +class nsIAtom;
    1.33 +
    1.34 +typedef void
    1.35 +(*NSPropertyFunc)(void           *aObject,
    1.36 +                  nsIAtom        *aPropertyName,
    1.37 +                  void           *aPropertyValue,
    1.38 +                  void           *aData);
    1.39 +
    1.40 +/**
    1.41 + * Callback type for property destructors.  |aObject| is the object
    1.42 + * the property is being removed for, |aPropertyName| is the property
    1.43 + * being removed, |aPropertyValue| is the value of the property, and |aData|
    1.44 + * is the opaque destructor data that was passed to SetProperty().
    1.45 + **/
    1.46 +typedef NSPropertyFunc NSPropertyDtorFunc;
    1.47 +class nsINode;
    1.48 +class nsIFrame;
    1.49 +
    1.50 +class nsPropertyOwner
    1.51 +{
    1.52 +public:
    1.53 +  nsPropertyOwner(const nsPropertyOwner& aOther) : mObject(aOther.mObject) {}
    1.54 +
    1.55 +  // These are the types of objects that can own properties. No object should
    1.56 +  // inherit more then one of these classes.
    1.57 +  // To add support for more types just add to this list.
    1.58 +  nsPropertyOwner(const nsINode* aObject) : mObject(aObject) {}
    1.59 +  nsPropertyOwner(const nsIFrame* aObject) : mObject(aObject) {}
    1.60 +
    1.61 +  operator const void*() { return mObject; }
    1.62 +  const void* get() { return mObject; }
    1.63 +
    1.64 +private:
    1.65 +  const void* mObject;
    1.66 +};
    1.67 +
    1.68 +class nsPropertyTable
    1.69 +{
    1.70 + public:
    1.71 +  /**
    1.72 +   * Get the value of the property |aPropertyName| for node |aObject|.
    1.73 +   * |aResult|, if supplied, is filled in with a return status code.
    1.74 +   **/
    1.75 +  void* GetProperty(nsPropertyOwner aObject,
    1.76 +                    nsIAtom    *aPropertyName,
    1.77 +                    nsresult   *aResult = nullptr)
    1.78 +  {
    1.79 +    return GetPropertyInternal(aObject, aPropertyName, false, aResult);
    1.80 +  }
    1.81 +
    1.82 +  /**
    1.83 +   * Set the value of the property |aPropertyName| to
    1.84 +   * |aPropertyValue| for node |aObject|.  |aDtor| is a destructor for the
    1.85 +   * property value to be called if the property is removed.  It can be null
    1.86 +   * if no destructor is required.  |aDtorData| is an optional pointer to an
    1.87 +   * opaque context to be passed to the property destructor.  Note that the
    1.88 +   * destructor is global for each property name regardless of node; it is an
    1.89 +   * error to set a given property with a different destructor than was used
    1.90 +   * before (this will return NS_ERROR_INVALID_ARG). If aOldValue is non-null
    1.91 +   * it will contain the old value after the function returns (the destructor
    1.92 +   * for the old value will not be run in that case). If |aTransfer| is true
    1.93 +   * the property will be transfered to the new table when the property table
    1.94 +   * for |aObject| changes (currently the tables for nodes are owned by their
    1.95 +   * ownerDocument, so if the ownerDocument for a node changes, its property
    1.96 +   * table changes too). If |aTransfer| is false the property will just be
    1.97 +   * deleted instead.
    1.98 +   */
    1.99 +  NS_HIDDEN_(nsresult) SetProperty(nsPropertyOwner     aObject,
   1.100 +                                   nsIAtom            *aPropertyName,
   1.101 +                                   void               *aPropertyValue,
   1.102 +                                   NSPropertyDtorFunc  aDtor,
   1.103 +                                   void               *aDtorData,
   1.104 +                                   bool                aTransfer = false,
   1.105 +                                   void              **aOldValue = nullptr)
   1.106 +  {
   1.107 +    return SetPropertyInternal(aObject, aPropertyName, aPropertyValue,
   1.108 +                               aDtor, aDtorData, aTransfer, aOldValue);
   1.109 +  }
   1.110 +
   1.111 +  /**
   1.112 +   * Delete the property |aPropertyName| in the global category for object
   1.113 +   * |aObject|. The property's destructor function will be called.
   1.114 +   */
   1.115 +  NS_HIDDEN_(nsresult) DeleteProperty(nsPropertyOwner aObject,
   1.116 +                                      nsIAtom    *aPropertyName);
   1.117 +
   1.118 +  /**
   1.119 +   * Unset the property |aPropertyName| in the global category for object
   1.120 +   * |aObject|, but do not call the property's destructor function.  The
   1.121 +   * property value is returned.
   1.122 +   */
   1.123 +  void* UnsetProperty(nsPropertyOwner aObject,
   1.124 +                      nsIAtom    *aPropertyName,
   1.125 +                      nsresult   *aStatus = nullptr)
   1.126 +  {
   1.127 +    return GetPropertyInternal(aObject, aPropertyName, true, aStatus);
   1.128 +  }
   1.129 +
   1.130 +  /**
   1.131 +   * Deletes all of the properties for object |aObject|, calling the
   1.132 +   * destructor function for each property.
   1.133 +   */
   1.134 +  NS_HIDDEN_(void) DeleteAllPropertiesFor(nsPropertyOwner aObject);
   1.135 +
   1.136 +  /**
   1.137 +   * Transfers all properties for object |aObject| that were set with the
   1.138 +   * |aTransfer| argument as true to |aTable|. Deletes the other properties
   1.139 +   * for object |aObject|, calling the destructor function for each property.
   1.140 +   * If transfering a property fails, this deletes all the properties for
   1.141 +   * object |aObject|.
   1.142 +   */
   1.143 +  NS_HIDDEN_(nsresult)
   1.144 +    TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject,
   1.145 +                                     nsPropertyTable *aOtherTable);
   1.146 +
   1.147 +  /**
   1.148 +   * Enumerate the properties for object |aObject|.
   1.149 +   * For every property |aCallback| will be called with as arguments |aObject|,
   1.150 +   * the property name, the property value and |aData|.
   1.151 +   */
   1.152 +  NS_HIDDEN_(void) Enumerate(nsPropertyOwner aObject,
   1.153 +                             NSPropertyFunc aCallback, void *aData);
   1.154 +
   1.155 +  /**
   1.156 +   * Enumerate all the properties.
   1.157 +   * For every property |aCallback| will be called with arguments the owner,
   1.158 +   * the property name, the property value and |aData|.
   1.159 +   */
   1.160 +  NS_HIDDEN_(void) EnumerateAll(NSPropertyFunc aCallback, void *aData);
   1.161 +
   1.162 +  /**
   1.163 +   * Deletes all of the properties for all objects in the property
   1.164 +   * table, calling the destructor function for each property.
   1.165 +   */
   1.166 +  NS_HIDDEN_(void) DeleteAllProperties();
   1.167 +
   1.168 +  nsPropertyTable() : mPropertyList(nullptr) {}  
   1.169 +  ~nsPropertyTable() {
   1.170 +    DeleteAllProperties();
   1.171 +  }
   1.172 +
   1.173 +  /**
   1.174 +   * Function useable as destructor function for property data that is
   1.175 +   * XPCOM objects. The function will call NS_IF_RELASE on the value
   1.176 +   * to destroy it.
   1.177 +   */
   1.178 +  static void SupportsDtorFunc(void *aObject, nsIAtom *aPropertyName,
   1.179 +                               void *aPropertyValue, void *aData);
   1.180 +
   1.181 +  class PropertyList;
   1.182 +
   1.183 +  size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
   1.184 +
   1.185 + private:
   1.186 +  NS_HIDDEN_(void) DestroyPropertyList();
   1.187 +  NS_HIDDEN_(PropertyList*) GetPropertyListFor(nsIAtom *aPropertyName) const;
   1.188 +  NS_HIDDEN_(void*) GetPropertyInternal(nsPropertyOwner aObject,
   1.189 +                                        nsIAtom    *aPropertyName,
   1.190 +                                        bool        aRemove,
   1.191 +                                        nsresult   *aStatus);
   1.192 +  NS_HIDDEN_(nsresult) SetPropertyInternal(nsPropertyOwner     aObject,
   1.193 +                                           nsIAtom            *aPropertyName,
   1.194 +                                           void               *aPropertyValue,
   1.195 +                                           NSPropertyDtorFunc  aDtor,
   1.196 +                                           void               *aDtorData,
   1.197 +                                           bool                aTransfer,
   1.198 +                                           void              **aOldValue);
   1.199 +
   1.200 +  PropertyList *mPropertyList;
   1.201 +};
   1.202 +#endif

mercurial