Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef GIN_HANDLE_H_ | |
| 6 #define GIN_HANDLE_H_ | |
| 7 | |
| 8 #include "gin/converter.h" | |
| 9 | |
| 10 namespace gin { | |
| 11 | |
| 12 // You can use gin::Handle on the stack to retain a gin::Wrappable object. | |
| 13 // Currently we don't have a mechanism for retaining a gin::Wrappable object | |
| 14 // in the C++ heap because strong references from C++ to V8 can cause memory | |
| 15 // leaks. | |
| 16 template<typename T> | |
| 17 class Handle { | |
| 18 public: | |
| 19 Handle() : object_(0) {} | |
|
Aaron Boodman
2013/12/04 22:04:42
NULL
abarth-chromium
2013/12/04 22:24:12
Done.
| |
| 20 | |
| 21 Handle(v8::Handle<v8::Value> wrapper, T* object) | |
| 22 : wrapper_(wrapper), | |
| 23 object_(object) { | |
| 24 } | |
| 25 | |
| 26 bool IsEmpty() const { return !object_; } | |
| 27 | |
| 28 void Clear() { | |
| 29 wrapper_.Clear(); | |
| 30 object_ = 0; | |
| 31 } | |
| 32 | |
| 33 T* operator->() const { return object_; } | |
| 34 v8::Handle<v8::Value> ToV8() { return wrapper_; } | |
| 35 T* Get() const { return object_; } | |
|
Aaron Boodman
2013/12/04 22:04:42
Since this is a trivial getter, it should be lower
abarth-chromium
2013/12/04 22:24:12
Done.
| |
| 36 | |
| 37 private: | |
| 38 v8::Handle<v8::Value> wrapper_; | |
| 39 T* object_; | |
| 40 }; | |
| 41 | |
| 42 template<typename T> | |
| 43 struct Converter<gin::Handle<T> > { | |
| 44 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | |
| 45 gin::Handle<T> val) { | |
|
Aaron Boodman
2013/12/04 22:04:42
Since gin::Handle is two pointers, it really is ch
abarth-chromium
2013/12/04 22:24:12
Done.
| |
| 46 return val.ToV8(); | |
| 47 } | |
| 48 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
| 49 gin::Handle<T>* out) { | |
| 50 T* object = NULL; | |
| 51 Converter<T*>::FromV8(isolate, val, &object); | |
| 52 return gin::Handle<T>(val, object); | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 // This function is a convenient way to create a handle from a raw pointer | |
| 57 // without having to write out the type of the object explicitly. | |
| 58 template<typename T> | |
| 59 gin::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) { | |
| 60 return gin::Handle<T>(object->GetWrapper(isolate), object); | |
| 61 } | |
| 62 | |
| 63 } // namespace gin | |
| 64 | |
| 65 #endif // GIN_HANDLE_H_ | |
| OLD | NEW |