| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_WRAPPABLE_H_ | 5 #ifndef GIN_WRAPPABLE_H_ |
| 6 #define GIN_WRAPPABLE_H_ | 6 #define GIN_WRAPPABLE_H_ |
| 7 | 7 |
| 8 #include "base/template_util.h" | 8 #include "base/template_util.h" |
| 9 #include "gin/converter.h" | 9 #include "gin/converter.h" |
| 10 #include "gin/gin_export.h" |
| 10 #include "gin/public/wrapper_info.h" | 11 #include "gin/public/wrapper_info.h" |
| 11 | 12 |
| 12 namespace gin { | 13 namespace gin { |
| 13 | 14 |
| 14 namespace internal { | 15 namespace internal { |
| 15 | 16 |
| 16 void* FromV8Impl(v8::Isolate* isolate, v8::Handle<v8::Value> val, | 17 GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate, |
| 17 WrapperInfo* info); | 18 v8::Handle<v8::Value> val, |
| 19 WrapperInfo* info); |
| 18 | 20 |
| 19 } // namespace internal | 21 } // namespace internal |
| 20 | 22 |
| 21 | 23 |
| 22 // Wrappable is a base class for C++ objects that have corresponding v8 wrapper | 24 // Wrappable is a base class for C++ objects that have corresponding v8 wrapper |
| 23 // objects. To retain a Wrappable object on the stack, use a gin::Handle. | 25 // objects. To retain a Wrappable object on the stack, use a gin::Handle. |
| 24 // | 26 // |
| 25 // USAGE: | 27 // USAGE: |
| 26 // // my_class.h | 28 // // my_class.h |
| 27 // class MyClass : Wrappable<MyClass> { | 29 // class MyClass : Wrappable<MyClass> { |
| 28 // ... | 30 // ... |
| 29 // }; | 31 // }; |
| 30 // | 32 // |
| 31 // // my_class.cc | 33 // // my_class.cc |
| 32 // INIT_WRAPABLE(MyClass); | 34 // INIT_WRAPABLE(MyClass); |
| 33 // | 35 // |
| 34 // Subclasses should also typically have private constructors and expose a | 36 // Subclasses should also typically have private constructors and expose a |
| 35 // static Create function that returns a gin::Handle. Forcing creators through | 37 // static Create function that returns a gin::Handle. Forcing creators through |
| 36 // this static Create function will enforce that clients actually create a | 38 // this static Create function will enforce that clients actually create a |
| 37 // wrapper for the object. If clients fail to create a wrapper for a wrappable | 39 // wrapper for the object. If clients fail to create a wrapper for a wrappable |
| 38 // object, the object will leak because we use the weak callback from the | 40 // object, the object will leak because we use the weak callback from the |
| 39 // wrapper as the signal to delete the wrapped object. | 41 // wrapper as the signal to delete the wrapped object. |
| 40 template<typename T> | 42 template<typename T> |
| 41 class Wrappable; | 43 class Wrappable; |
| 42 | 44 |
| 43 | 45 |
| 44 // Non-template base class to share code between templates instances. | 46 // Non-template base class to share code between templates instances. |
| 45 class WrappableBase { | 47 class GIN_EXPORT WrappableBase { |
| 46 protected: | 48 protected: |
| 47 WrappableBase(); | 49 WrappableBase(); |
| 48 virtual ~WrappableBase(); | 50 virtual ~WrappableBase(); |
| 49 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, | 51 v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate, |
| 50 WrapperInfo* wrapper_info); | 52 WrapperInfo* wrapper_info); |
| 51 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate, | 53 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate, |
| 52 WrapperInfo* wrapper_info); | 54 WrapperInfo* wrapper_info); |
| 53 v8::Persistent<v8::Object> wrapper_; // Weak | 55 v8::Persistent<v8::Object> wrapper_; // Weak |
| 54 | 56 |
| 55 private: | 57 private: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { | 92 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) { |
| 91 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, | 93 *out = static_cast<T*>(internal::FromV8Impl(isolate, val, |
| 92 &T::kWrapperInfo)); | 94 &T::kWrapperInfo)); |
| 93 return *out != NULL; | 95 return *out != NULL; |
| 94 } | 96 } |
| 95 }; | 97 }; |
| 96 | 98 |
| 97 } // namespace gin | 99 } // namespace gin |
| 98 | 100 |
| 99 #endif // GIN_WRAPPABLE_H_ | 101 #endif // GIN_WRAPPABLE_H_ |
| OLD | NEW |