nsJSThingHashtable.h 1.9 KB

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