atom_api_key_weak_map.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_
  5. #define ATOM_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_
  6. #include "atom/common/key_weak_map.h"
  7. #include "native_mate/handle.h"
  8. #include "native_mate/object_template_builder.h"
  9. #include "native_mate/wrappable.h"
  10. namespace atom {
  11. namespace api {
  12. template <typename K>
  13. class KeyWeakMap : public mate::Wrappable<KeyWeakMap<K>> {
  14. public:
  15. static mate::Handle<KeyWeakMap<K>> Create(v8::Isolate* isolate) {
  16. return mate::CreateHandle(isolate, new KeyWeakMap<K>(isolate));
  17. }
  18. static void BuildPrototype(v8::Isolate* isolate,
  19. v8::Local<v8::FunctionTemplate> prototype) {
  20. prototype->SetClassName(mate::StringToV8(isolate, "KeyWeakMap"));
  21. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  22. .SetMethod("set", &KeyWeakMap<K>::Set)
  23. .SetMethod("get", &KeyWeakMap<K>::Get)
  24. .SetMethod("has", &KeyWeakMap<K>::Has)
  25. .SetMethod("remove", &KeyWeakMap<K>::Remove);
  26. }
  27. protected:
  28. explicit KeyWeakMap(v8::Isolate* isolate) {
  29. mate::Wrappable<KeyWeakMap<K>>::Init(isolate);
  30. }
  31. ~KeyWeakMap() override {}
  32. private:
  33. // API for KeyWeakMap.
  34. void Set(v8::Isolate* isolate, const K& key, v8::Local<v8::Object> object) {
  35. key_weak_map_.Set(isolate, key, object);
  36. }
  37. v8::Local<v8::Object> Get(v8::Isolate* isolate, const K& key) {
  38. return key_weak_map_.Get(isolate, key).ToLocalChecked();
  39. }
  40. bool Has(const K& key) { return key_weak_map_.Has(key); }
  41. void Remove(const K& key) { key_weak_map_.Remove(key); }
  42. atom::KeyWeakMap<K> key_weak_map_;
  43. DISALLOW_COPY_AND_ASSIGN(KeyWeakMap);
  44. };
  45. } // namespace api
  46. } // namespace atom
  47. #endif // ATOM_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_