Chromium Code Reviews| Index: gin/handle.h |
| diff --git a/gin/handle.h b/gin/handle.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..904b781e42bd2ae28a4bc084203f6b1cd0ac11c8 |
| --- /dev/null |
| +++ b/gin/handle.h |
| @@ -0,0 +1,65 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef GIN_HANDLE_H_ |
| +#define GIN_HANDLE_H_ |
| + |
| +#include "gin/converter.h" |
| + |
| +namespace gin { |
| + |
| +// You can use gin::Handle on the stack to retain a gin::Wrappable object. |
| +// Currently we don't have a mechanism for retaining a gin::Wrappable object |
| +// in the C++ heap because strong references from C++ to V8 can cause memory |
| +// leaks. |
| +template<typename T> |
| +class Handle { |
| + public: |
| + Handle() : object_(0) {} |
|
Aaron Boodman
2013/12/04 22:04:42
NULL
abarth-chromium
2013/12/04 22:24:12
Done.
|
| + |
| + Handle(v8::Handle<v8::Value> wrapper, T* object) |
| + : wrapper_(wrapper), |
| + object_(object) { |
| + } |
| + |
| + bool IsEmpty() const { return !object_; } |
| + |
| + void Clear() { |
| + wrapper_.Clear(); |
| + object_ = 0; |
| + } |
| + |
| + T* operator->() const { return object_; } |
| + v8::Handle<v8::Value> ToV8() { return wrapper_; } |
| + 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.
|
| + |
| + private: |
| + v8::Handle<v8::Value> wrapper_; |
| + T* object_; |
| +}; |
| + |
| +template<typename T> |
| +struct Converter<gin::Handle<T> > { |
| + static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| + 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.
|
| + return val.ToV8(); |
| + } |
| + static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
| + gin::Handle<T>* out) { |
| + T* object = NULL; |
| + Converter<T*>::FromV8(isolate, val, &object); |
| + return gin::Handle<T>(val, object); |
| + } |
| +}; |
| + |
| +// This function is a convenient way to create a handle from a raw pointer |
| +// without having to write out the type of the object explicitly. |
| +template<typename T> |
| +gin::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) { |
| + return gin::Handle<T>(object->GetWrapper(isolate), object); |
| +} |
| + |
| +} // namespace gin |
| + |
| +#endif // GIN_HANDLE_H_ |