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_HANDLE_H_ | 5 #ifndef GIN_HANDLE_H_ |
6 #define GIN_HANDLE_H_ | 6 #define GIN_HANDLE_H_ |
7 | 7 |
8 #include "gin/converter.h" | 8 #include "gin/converter.h" |
9 | 9 |
10 namespace gin { | 10 namespace gin { |
(...skipping 13 matching lines...) Expand all Loading... |
24 } | 24 } |
25 | 25 |
26 bool IsEmpty() const { return !object_; } | 26 bool IsEmpty() const { return !object_; } |
27 | 27 |
28 void Clear() { | 28 void Clear() { |
29 wrapper_.Clear(); | 29 wrapper_.Clear(); |
30 object_ = NULL; | 30 object_ = NULL; |
31 } | 31 } |
32 | 32 |
33 T* operator->() const { return object_; } | 33 T* operator->() const { return object_; } |
34 v8::Handle<v8::Value> ToV8() { return wrapper_; } | 34 v8::Handle<v8::Value> ToV8() const { return wrapper_; } |
35 T* get() const { return object_; } | 35 T* get() const { return object_; } |
36 | 36 |
37 private: | 37 private: |
38 v8::Handle<v8::Value> wrapper_; | 38 v8::Handle<v8::Value> wrapper_; |
39 T* object_; | 39 T* object_; |
40 }; | 40 }; |
41 | 41 |
42 template<typename T> | 42 template<typename T> |
43 struct Converter<gin::Handle<T> > { | 43 struct Converter<gin::Handle<T> > { |
44 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 44 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
45 const gin::Handle<T>& val) { | 45 const gin::Handle<T>& val) { |
46 return val.ToV8(); | 46 return val.ToV8(); |
47 } | 47 } |
48 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | 48 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
49 gin::Handle<T>* out) { | 49 gin::Handle<T>* out) { |
50 T* object = NULL; | 50 T* object = NULL; |
51 Converter<T*>::FromV8(isolate, val, &object); | 51 if (!Converter<T*>::FromV8(isolate, val, &object)) { |
52 return gin::Handle<T>(val, object); | 52 return false; |
| 53 } |
| 54 *out = gin::Handle<T>(val, object); |
| 55 return true; |
53 } | 56 } |
54 }; | 57 }; |
55 | 58 |
56 // This function is a convenient way to create a handle from a raw pointer | 59 // 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. | 60 // without having to write out the type of the object explicitly. |
58 template<typename T> | 61 template<typename T> |
59 gin::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) { | 62 gin::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) { |
60 return gin::Handle<T>(object->GetWrapper(isolate), object); | 63 return gin::Handle<T>(object->GetWrapper(isolate), object); |
61 } | 64 } |
62 | 65 |
63 } // namespace gin | 66 } // namespace gin |
64 | 67 |
65 #endif // GIN_HANDLE_H_ | 68 #endif // GIN_HANDLE_H_ |
OLD | NEW |