Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Unified Diff: gin/object_template_builder.h

Issue 93813002: Implement gin::ObjectTemplateBuilder (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: gin/object_template_builder.h
diff --git a/gin/object_template_builder.h b/gin/object_template_builder.h
new file mode 100644
index 0000000000000000000000000000000000000000..7a9dde483bc1d1b9a937ba85bdf0e760f541850c
--- /dev/null
+++ b/gin/object_template_builder.h
@@ -0,0 +1,56 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_
+#define GIN_OBJECT_TEMPLATE_BUILDER_H_
+
+#include "base/bind.h"
+#include "base/strings/string_piece.h"
+#include "gin/converter.h"
+#include "gin/function_template.h"
+#include "v8/include/v8.h"
+
+namespace gin {
+
+// ObjectTemplateBuilder provides a handy interface to creating
+// v8::ObjectTemplate instances with various sorts of properties.
+class ObjectTemplateBuilder {
+ public:
+ ObjectTemplateBuilder(v8::Isolate* isolate);
abarth-chromium 2013/11/28 06:27:09 Please mark one argument constructors explicit
Aaron Boodman 2013/11/28 18:25:22 Whoops, thanks.
+ ~ObjectTemplateBuilder();
+
+ // It's against Google C++ style to return a non-const ref, but we take some
+ // poetic license here in order that all calls to Set() can be via the '.'
+ // operator and line up nicely.
+ template<typename T>
+ ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) {
+ return SetImpl(name, ConvertToV8(isolate_, val));
+ }
+
+ template<typename T>
+ ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, T val) {
+ return SetMethod(name, base::Bind(val));
+ }
+
+ template<typename T>
+ ObjectTemplateBuilder& SetMethod(const base::StringPiece& name,
+ const base::Callback<T>& callback) {
+ return SetImpl(name, CreateFunctionTemplate(isolate_, callback));
+ }
+
+ v8::Local<v8::ObjectTemplate> Build();
+
+ private:
+ ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
+ v8::Handle<v8::Data> val);
+
+ v8::Isolate* isolate_;
+
+ // ObjectTemplateBuilder should only be used on the stack.
+ v8::Local<v8::ObjectTemplate> template_;
+};
+
+} // namespace gin
+
+#endif // GIN_OBJECT_TEMPLATE_BUILDER_H_

Powered by Google App Engine
This is Rietveld 408576698