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

Side by Side Diff: gin/data_object_builder.h

Issue 2845463002: gin: Create a DataObjectBuilder class to help create simple data objects. (Closed)
Patch Set: devlin, jochen Created 3 years, 7 months 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 unified diff | Download patch
« no previous file with comments | « gin/BUILD.gn ('k') | gin/data_object_builder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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_DATA_OBJECT_BUILDER_H_
6 #define GIN_DATA_OBJECT_BUILDER_H_
7
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/strings/string_piece.h"
13 #include "gin/converter.h"
14 #include "gin/gin_export.h"
15 #include "v8/include/v8.h"
16
17 namespace gin {
18
19 // Constructs a JavaScript object with a series of data properties.
20 // (As with default data properties in JavaScript, these properties are
21 // configurable, writable and enumerable.)
22 //
23 // Values are automatically converted using gin::Converter, though if
24 // using a type where the conversion may fail, callers must convert ahead of
25 // time.
26 //
27 // This class avoids the pitfall of using v8::Object::Set, which may invoke
28 // setters on the object prototype.
29 //
30 // Expected usage:
31 // v8::Local<v8::Object> object = gin::DataObjectBuilder(isolate)
32 // .Set("boolean", true)
33 // .Set("integer", 42)
34 // .Build();
35 //
36 // Because this builder class contains local handles, callers must ensure it
37 // does not outlive the scope in which it is created.
38 class GIN_EXPORT DataObjectBuilder {
39 public:
40 explicit DataObjectBuilder(v8::Isolate* isolate);
41
42 template <typename T>
43 DataObjectBuilder& Set(base::StringPiece key, T&& value) {
44 DCHECK(!object_.IsEmpty());
45 v8::Local<v8::String> v8_key = StringToSymbol(isolate_, key);
46 v8::Local<v8::Value> v8_value =
47 ConvertToV8(isolate_, std::forward<T>(value));
48 CHECK(object_->CreateDataProperty(context_, v8_key, v8_value).ToChecked());
49 return *this;
50 }
51
52 v8::Local<v8::Object> Build() {
53 DCHECK(!object_.IsEmpty());
54 v8::Local<v8::Object> result = object_;
55 object_.Clear();
56 return result;
57 }
58
59 private:
60 v8::Isolate* isolate_;
61 v8::Local<v8::Context> context_;
62 v8::Local<v8::Object> object_;
63
64 DISALLOW_COPY_AND_ASSIGN(DataObjectBuilder);
65 };
66
67 } // namespace gin
68
69 #endif // GIN_DATA_OBJECT_BUILDER_H_
OLDNEW
« no previous file with comments | « gin/BUILD.gn ('k') | gin/data_object_builder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698