Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef nsJSThingHashtable_h__
8 : #define nsJSThingHashtable_h__
9 :
10 : #include "nsHashKeys.h"
11 : #include "nsBaseHashtable.h"
12 :
13 : namespace JS {
14 : template<class T>
15 : class Heap;
16 : } /* namespace JS */
17 :
18 : /**
19 : * A wrapper for hash keys that sets ALLOW_MEMMOVE to false.
20 : *
21 : * This is used in the implementation of nsJSThingHashtable and is not intended
22 : * to be used directly.
23 : *
24 : * It is necessary for hash tables containing JS::Heap<T> values as these must
25 : * be copied rather than memmoved.
26 : */
27 : template<class T>
28 84 : class nsHashKeyDisallowMemmove : public T
29 : {
30 : public:
31 41 : explicit nsHashKeyDisallowMemmove(const typename T::KeyTypePointer aKey) : T(aKey) {}
32 : enum { ALLOW_MEMMOVE = false };
33 : };
34 :
35 :
36 : /**
37 : * Templated hashtable class for use on the heap where the values are JS GC things.
38 : *
39 : * Storing JS GC thing pointers on the heap requires wrapping them in a
40 : * JS::Heap<T>, and this class takes care of that while presenting an interface
41 : * in terms of the wrapped type T.
42 : *
43 : * For example, to store a hashtable mapping strings to JSObject pointers, you
44 : * can declare a data member like this:
45 : *
46 : * nsJSThingHashtable<nsStringHashKey, JSObject*> mStringToObjectMap;
47 : *
48 : * See nsBaseHashtable for complete declaration
49 : * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
50 : * for a complete specification.
51 : * @param DataType the datatype being wrapped, must be a JS GC thing.
52 : * @see nsInterfaceHashtable, nsClassHashtable
53 : */
54 : template<class KeyClass, class DataType>
55 4 : class nsJSThingHashtable
56 : : public nsBaseHashtable<nsHashKeyDisallowMemmove<KeyClass>,
57 : JS::Heap<DataType>, DataType>
58 : {
59 : };
60 :
61 : #endif // nsJSThingHashtable_h__
|