OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| 6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/strings/string_piece.h" |
| 10 #include "gin/converter.h" |
| 11 #include "gin/function_template.h" |
| 12 #include "v8/include/v8.h" |
| 13 |
| 14 namespace gin { |
| 15 |
| 16 // ObjectTemplateBuilder provides a handy interface to creating |
| 17 // v8::ObjectTemplate instances with various sorts of properties. |
| 18 class ObjectTemplateBuilder { |
| 19 public: |
| 20 explicit ObjectTemplateBuilder(v8::Isolate* isolate); |
| 21 ~ObjectTemplateBuilder(); |
| 22 |
| 23 // 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 '.' |
| 25 // operator and line up nicely. |
| 26 template<typename T> |
| 27 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { |
| 28 return SetImpl(name, ConvertToV8(isolate_, val)); |
| 29 } |
| 30 |
| 31 template<typename T> |
| 32 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, T val) { |
| 33 return SetMethod(name, base::Bind(val)); |
| 34 } |
| 35 |
| 36 template<typename T> |
| 37 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, |
| 38 const base::Callback<T>& callback) { |
| 39 return SetImpl(name, CreateFunctionTemplate(isolate_, callback)); |
| 40 } |
| 41 |
| 42 v8::Local<v8::ObjectTemplate> Build(); |
| 43 |
| 44 private: |
| 45 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, |
| 46 v8::Handle<v8::Data> val); |
| 47 |
| 48 v8::Isolate* isolate_; |
| 49 |
| 50 // ObjectTemplateBuilder should only be used on the stack. |
| 51 v8::Local<v8::ObjectTemplate> template_; |
| 52 }; |
| 53 |
| 54 } // namespace gin |
| 55 |
| 56 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |
OLD | NEW |