WeakMapPtr.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  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. #ifndef js_WeakMapPtr_h
  7. #define js_WeakMapPtr_h
  8. #include "jspubtd.h"
  9. #include "js/TypeDecls.h"
  10. namespace JS {
  11. // A wrapper around the internal C++ representation of SpiderMonkey WeakMaps,
  12. // usable outside the engine.
  13. //
  14. // The supported template specializations are enumerated in WeakMapPtr.cpp. If
  15. // you want to use this class for a different key/value combination, add it to
  16. // the list and the compiler will generate the relevant machinery.
  17. template <typename K, typename V>
  18. class JS_PUBLIC_API(WeakMapPtr)
  19. {
  20. public:
  21. WeakMapPtr() : ptr(nullptr) {}
  22. bool init(JSContext* cx);
  23. bool initialized() { return ptr != nullptr; }
  24. void destroy();
  25. virtual ~WeakMapPtr() { MOZ_ASSERT(!initialized()); }
  26. void trace(JSTracer* tracer);
  27. V lookup(const K& key);
  28. bool put(JSContext* cx, const K& key, const V& value);
  29. private:
  30. void* ptr;
  31. // WeakMapPtr is neither copyable nor assignable.
  32. WeakMapPtr(const WeakMapPtr& wmp) = delete;
  33. WeakMapPtr& operator=(const WeakMapPtr& wmp) = delete;
  34. };
  35. } /* namespace JS */
  36. #endif /* js_WeakMapPtr_h */