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_OBJECT_TEMPLATE_BUILDER_H_ | 5 #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ |
6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ | 6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
10 #include "base/template_util.h" | |
10 #include "gin/converter.h" | 11 #include "gin/converter.h" |
11 #include "gin/function_template.h" | 12 #include "gin/function_template.h" |
12 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
13 | 14 |
14 namespace gin { | 15 namespace gin { |
15 | 16 |
17 namespace { | |
18 | |
19 // The base template is used for non-member function pointers. | |
20 template<typename T, typename Enable = void> | |
21 struct CallbackTraits { | |
22 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | |
23 T callback) { | |
24 return CreateFunctionTemplate(isolate, base::Bind(callback)); | |
25 } | |
26 }; | |
27 | |
28 // This partial specialization is used for member function pointers. | |
Jeffrey Yasskin
2013/12/04 20:00:12
You should say what the member-function-pointer sp
| |
29 template<typename T> | |
30 struct CallbackTraits<T, typename base::enable_if< | |
31 base::is_member_function_pointer<T>::value >::type> { | |
32 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | |
33 T callback) { | |
34 return CreateFunctionTemplate(isolate, base::Bind(callback), | |
35 HolderIsFirstArgument); | |
36 } | |
37 }; | |
38 | |
39 // This full specialization is used for v8::FunctionTemplate. | |
40 template<> | |
41 struct CallbackTraits<v8::Handle<v8::FunctionTemplate> > { | |
42 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | |
43 v8::Handle<v8::FunctionTemplate> templ) { | |
44 return templ; | |
45 } | |
46 }; | |
47 | |
48 } // namespace | |
49 | |
50 | |
16 // ObjectTemplateBuilder provides a handy interface to creating | 51 // ObjectTemplateBuilder provides a handy interface to creating |
17 // v8::ObjectTemplate instances with various sorts of properties. | 52 // v8::ObjectTemplate instances with various sorts of properties. |
18 class ObjectTemplateBuilder { | 53 class ObjectTemplateBuilder { |
19 public: | 54 public: |
20 explicit ObjectTemplateBuilder(v8::Isolate* isolate); | 55 explicit ObjectTemplateBuilder(v8::Isolate* isolate); |
21 ~ObjectTemplateBuilder(); | 56 ~ObjectTemplateBuilder(); |
22 | 57 |
23 // It's against Google C++ style to return a non-const ref, but we take some | 58 // It's against Google C++ style to return a non-const ref, but we take some |
24 // poetic license here in order that all calls to Set() can be via the '.' | 59 // poetic license here in order that all calls to Set() can be via the '.' |
25 // operator and line up nicely. | 60 // operator and line up nicely. |
26 template<typename T> | 61 template<typename T> |
27 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { | 62 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { |
28 return SetImpl(name, ConvertToV8(isolate_, val)); | 63 return SetImpl(name, ConvertToV8(isolate_, val)); |
29 } | 64 } |
30 | 65 |
31 template<typename T> | 66 // In the following methods, T and U can be either function pointer, member |
32 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, T val) { | 67 // function pointer, or v8::FunctionTemplate. Most clients will want to use |
33 return SetMethod(name, base::Bind(val)); | 68 // one of the first two options. See gin::CreateFunctionTemplate() for |
34 } | 69 // creating raw function templates. |
35 | |
36 template<typename T> | 70 template<typename T> |
37 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, | 71 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, |
38 const base::Callback<T>& callback) { | 72 const T& callback) { |
39 return SetImpl(name, CreateFunctionTemplate(isolate_, callback)); | 73 return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback)); |
74 } | |
75 template<typename T> | |
76 ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, | |
77 const T& getter) { | |
78 return SetPropertyImpl(name, | |
79 CallbackTraits<T>::CreateTemplate(isolate_, getter), | |
80 v8::Local<v8::FunctionTemplate>()); | |
81 } | |
82 template<typename T, typename U> | |
83 ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, | |
84 const T& getter, const U& setter) { | |
85 return SetPropertyImpl(name, | |
86 CallbackTraits<T>::CreateTemplate(isolate_, getter), | |
87 CallbackTraits<U>::CreateTemplate(isolate_, setter)); | |
40 } | 88 } |
41 | 89 |
42 v8::Local<v8::ObjectTemplate> Build(); | 90 v8::Local<v8::ObjectTemplate> Build(); |
43 | 91 |
44 private: | 92 private: |
45 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, | 93 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, |
46 v8::Handle<v8::Data> val); | 94 v8::Handle<v8::Data> val); |
95 ObjectTemplateBuilder& SetPropertyImpl( | |
96 const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, | |
97 v8::Handle<v8::FunctionTemplate> setter); | |
47 | 98 |
48 v8::Isolate* isolate_; | 99 v8::Isolate* isolate_; |
49 | 100 |
50 // ObjectTemplateBuilder should only be used on the stack. | 101 // ObjectTemplateBuilder should only be used on the stack. |
51 v8::Local<v8::ObjectTemplate> template_; | 102 v8::Local<v8::ObjectTemplate> template_; |
52 }; | 103 }; |
53 | 104 |
54 } // namespace gin | 105 } // namespace gin |
55 | 106 |
56 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ | 107 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |
OLD | NEW |