WeakMapPtr.h 1.3 KB

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