| 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/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/template_util.h" | 11 #include "base/template_util.h" |
| 12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
| 13 #include "gin/function_template.h" | 13 #include "gin/function_template.h" |
| 14 #include "gin/gin_export.h" |
| 14 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 15 | 16 |
| 16 namespace gin { | 17 namespace gin { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Base template - used only for non-member function pointers. Other types | 21 // Base template - used only for non-member function pointers. Other types |
| 21 // either go to one of the below specializations, or go here and fail to compile | 22 // either go to one of the below specializations, or go here and fail to compile |
| 22 // because of base::Bind(). | 23 // because of base::Bind(). |
| 23 template<typename T, typename Enable = void> | 24 template<typename T, typename Enable = void> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 47 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | 48 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, |
| 48 T callback) { | 49 T callback) { |
| 49 return CreateFunctionTemplate(isolate, base::Bind(callback), | 50 return CreateFunctionTemplate(isolate, base::Bind(callback), |
| 50 HolderIsFirstArgument); | 51 HolderIsFirstArgument); |
| 51 } | 52 } |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 // This specialization allows people to construct function templates directly if | 55 // This specialization allows people to construct function templates directly if |
| 55 // they need to do fancier stuff. | 56 // they need to do fancier stuff. |
| 56 template<> | 57 template<> |
| 57 struct CallbackTraits<v8::Handle<v8::FunctionTemplate> > { | 58 struct GIN_EXPORT CallbackTraits<v8::Handle<v8::FunctionTemplate> > { |
| 58 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | 59 static v8::Handle<v8::FunctionTemplate> CreateTemplate( |
| 59 v8::Handle<v8::FunctionTemplate> templ) { | 60 v8::Handle<v8::FunctionTemplate> templ) { |
| 60 return templ; | 61 return templ; |
| 61 } | 62 } |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 } // namespace | 65 } // namespace |
| 65 | 66 |
| 66 | 67 |
| 67 // ObjectTemplateBuilder provides a handy interface to creating | 68 // ObjectTemplateBuilder provides a handy interface to creating |
| 68 // v8::ObjectTemplate instances with various sorts of properties. | 69 // v8::ObjectTemplate instances with various sorts of properties. |
| 69 class ObjectTemplateBuilder { | 70 class GIN_EXPORT ObjectTemplateBuilder { |
| 70 public: | 71 public: |
| 71 explicit ObjectTemplateBuilder(v8::Isolate* isolate); | 72 explicit ObjectTemplateBuilder(v8::Isolate* isolate); |
| 72 ~ObjectTemplateBuilder(); | 73 ~ObjectTemplateBuilder(); |
| 73 | 74 |
| 74 // It's against Google C++ style to return a non-const ref, but we take some | 75 // It's against Google C++ style to return a non-const ref, but we take some |
| 75 // poetic license here in order that all calls to Set() can be via the '.' | 76 // poetic license here in order that all calls to Set() can be via the '.' |
| 76 // operator and line up nicely. | 77 // operator and line up nicely. |
| 77 template<typename T> | 78 template<typename T> |
| 78 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { | 79 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { |
| 79 return SetImpl(name, ConvertToV8(isolate_, val)); | 80 return SetImpl(name, ConvertToV8(isolate_, val)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 115 |
| 115 v8::Isolate* isolate_; | 116 v8::Isolate* isolate_; |
| 116 | 117 |
| 117 // ObjectTemplateBuilder should only be used on the stack. | 118 // ObjectTemplateBuilder should only be used on the stack. |
| 118 v8::Local<v8::ObjectTemplate> template_; | 119 v8::Local<v8::ObjectTemplate> template_; |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 } // namespace gin | 122 } // namespace gin |
| 122 | 123 |
| 123 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ | 124 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| OLD | NEW |