1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsJSThingHashtable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,58 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsJSThingHashtable_h__ 1.10 +#define nsJSThingHashtable_h__ 1.11 + 1.12 +#include "nsHashKeys.h" 1.13 +#include "nsBaseHashtable.h" 1.14 + 1.15 +namespace JS { 1.16 +template <class T> 1.17 +class Heap; 1.18 +} /* namespace JS */ 1.19 + 1.20 +/** 1.21 + * A wrapper for hash keys that sets ALLOW_MEMMOVE to false. 1.22 + * 1.23 + * This is used in the implementation of nsJSThingHashtable and is not intended 1.24 + * to be used directly. 1.25 + * 1.26 + * It is necessary for hash tables containing JS::Heap<T> values as these must 1.27 + * be copied rather than memmoved. 1.28 + */ 1.29 +template<class T> 1.30 +class nsHashKeyDisallowMemmove : public T 1.31 +{ 1.32 + public: 1.33 + nsHashKeyDisallowMemmove(const T& key) : T(key) {} 1.34 + enum { ALLOW_MEMMOVE = false }; 1.35 +}; 1.36 + 1.37 + 1.38 +/** 1.39 + * Templated hashtable class for use on the heap where the values are JS GC things. 1.40 + * 1.41 + * Storing JS GC thing pointers on the heap requires wrapping them in a 1.42 + * JS::Heap<T>, and this class takes care of that while presenting an interface 1.43 + * in terms of the wrapped type T. 1.44 + * 1.45 + * For example, to store a hashtable mapping strings to JSObject pointers, you 1.46 + * can declare a data member like this: 1.47 + * 1.48 + * nsJSThingHashtable<nsStringHashKey, JSObject*> mStringToObjectMap; 1.49 + * 1.50 + * See nsBaseHashtable for complete declaration 1.51 + * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h 1.52 + * for a complete specification. 1.53 + * @param DataType the datatype being wrapped, must be a JS GC thing. 1.54 + * @see nsInterfaceHashtable, nsClassHashtable 1.55 + */ 1.56 +template<class KeyClass,class DataType> 1.57 +class nsJSThingHashtable : 1.58 + public nsBaseHashtable<nsHashKeyDisallowMemmove<KeyClass>, JS::Heap<DataType>, DataType> 1.59 +{ }; 1.60 + 1.61 +#endif // nsJSThingHashtable_h__