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 // This file provides a builders for DictionaryValue and ListValue. These | 5 // This file provides a builders for DictionaryValue and ListValue. These |
6 // aren't specific to extensions and could move up to base/ if there's interest | 6 // aren't specific to extensions and could move up to base/ if there's interest |
7 // from other sub-projects. | 7 // from other sub-projects. |
8 // | 8 // |
9 // The general pattern is to write: | 9 // The pattern is to write: |
10 // | 10 // |
11 // scoped_ptr<BuiltType> result(FooBuilder() | 11 // scoped_ptr<BuiltType> result(FooBuilder() |
12 // .Set(args) | 12 // .Set(args) |
13 // .Set(args) | 13 // .Set(args) |
14 // .Build()); | 14 // .Build()); |
15 // | 15 // |
16 // For methods that take other built types, you can pass the builder directly | |
17 // to the setter without calling Build(): | |
18 // | |
19 // DictionaryBuilder().Set("key", std::move(ListBuilder() | |
20 // .Append("foo").Append("bar")) /* No .Build() */); | |
21 // | |
22 // The Build() method invalidates its builder, and returns ownership of the | 16 // The Build() method invalidates its builder, and returns ownership of the |
23 // built value. | 17 // built value. |
24 // | 18 // |
25 // These objects are intended to be used as temporaries rather than stored | 19 // These objects are intended to be used as temporaries rather than stored |
26 // anywhere, so the use of non-const reference parameters is likely to cause | 20 // anywhere, so the use of non-const reference parameters is likely to cause |
27 // less confusion than usual. | 21 // less confusion than usual. |
28 | 22 |
29 #ifndef EXTENSIONS_COMMON_VALUE_BUILDER_H_ | 23 #ifndef EXTENSIONS_COMMON_VALUE_BUILDER_H_ |
30 #define EXTENSIONS_COMMON_VALUE_BUILDER_H_ | 24 #define EXTENSIONS_COMMON_VALUE_BUILDER_H_ |
31 | 25 |
(...skipping 13 matching lines...) Expand all Loading... |
45 namespace extensions { | 39 namespace extensions { |
46 | 40 |
47 class ListBuilder; | 41 class ListBuilder; |
48 | 42 |
49 class DictionaryBuilder { | 43 class DictionaryBuilder { |
50 public: | 44 public: |
51 DictionaryBuilder(); | 45 DictionaryBuilder(); |
52 explicit DictionaryBuilder(const base::DictionaryValue& init); | 46 explicit DictionaryBuilder(const base::DictionaryValue& init); |
53 ~DictionaryBuilder(); | 47 ~DictionaryBuilder(); |
54 | 48 |
55 // Move constructor and operator=. | |
56 DictionaryBuilder(DictionaryBuilder&& other); | |
57 DictionaryBuilder& operator=(DictionaryBuilder&& other); | |
58 | |
59 // Can only be called once, after which it's invalid to use the builder. | 49 // Can only be called once, after which it's invalid to use the builder. |
60 scoped_ptr<base::DictionaryValue> Build() { return std::move(dict_); } | 50 scoped_ptr<base::DictionaryValue> Build() { return std::move(dict_); } |
61 | 51 |
62 // Immediately serializes the current state to JSON. Can be called as many | 52 // Immediately serializes the current state to JSON. Can be called as many |
63 // times as you like. | 53 // times as you like. |
64 std::string ToJSON() const; | 54 std::string ToJSON() const; |
65 | 55 |
66 DictionaryBuilder& Set(const std::string& path, int in_value); | 56 DictionaryBuilder& Set(const std::string& path, int in_value); |
67 DictionaryBuilder& Set(const std::string& path, double in_value); | 57 DictionaryBuilder& Set(const std::string& path, double in_value); |
68 DictionaryBuilder& Set(const std::string& path, const std::string& in_value); | 58 DictionaryBuilder& Set(const std::string& path, const std::string& in_value); |
69 DictionaryBuilder& Set(const std::string& path, | 59 DictionaryBuilder& Set(const std::string& path, |
70 const base::string16& in_value); | 60 const base::string16& in_value); |
71 DictionaryBuilder& Set(const std::string& path, DictionaryBuilder in_value); | |
72 DictionaryBuilder& Set(const std::string& path, ListBuilder in_value); | |
73 DictionaryBuilder& Set(const std::string& path, | 61 DictionaryBuilder& Set(const std::string& path, |
74 scoped_ptr<base::Value> in_value); | 62 scoped_ptr<base::Value> in_value); |
75 | 63 |
76 // Named differently because overload resolution is too eager to | 64 // Named differently because overload resolution is too eager to |
77 // convert implicitly to bool. | 65 // convert implicitly to bool. |
78 DictionaryBuilder& SetBoolean(const std::string& path, bool in_value); | 66 DictionaryBuilder& SetBoolean(const std::string& path, bool in_value); |
79 | 67 |
80 private: | 68 private: |
81 scoped_ptr<base::DictionaryValue> dict_; | 69 scoped_ptr<base::DictionaryValue> dict_; |
82 }; | 70 }; |
83 | 71 |
84 class ListBuilder { | 72 class ListBuilder { |
85 public: | 73 public: |
86 ListBuilder(); | 74 ListBuilder(); |
87 explicit ListBuilder(const base::ListValue& init); | 75 explicit ListBuilder(const base::ListValue& init); |
88 ~ListBuilder(); | 76 ~ListBuilder(); |
89 | 77 |
90 // Move constructor and operator=. | |
91 ListBuilder(ListBuilder&& other); | |
92 ListBuilder& operator=(ListBuilder&& other); | |
93 | |
94 // Can only be called once, after which it's invalid to use the builder. | 78 // Can only be called once, after which it's invalid to use the builder. |
95 scoped_ptr<base::ListValue> Build() { return std::move(list_); } | 79 scoped_ptr<base::ListValue> Build() { return std::move(list_); } |
96 | 80 |
97 ListBuilder& Append(int in_value); | 81 ListBuilder& Append(int in_value); |
98 ListBuilder& Append(double in_value); | 82 ListBuilder& Append(double in_value); |
99 ListBuilder& Append(const std::string& in_value); | 83 ListBuilder& Append(const std::string& in_value); |
100 ListBuilder& Append(const base::string16& in_value); | 84 ListBuilder& Append(const base::string16& in_value); |
101 ListBuilder& Append(DictionaryBuilder in_value); | 85 ListBuilder& Append(scoped_ptr<base::Value> in_value); |
102 ListBuilder& Append(ListBuilder in_value); | |
103 | 86 |
104 // Named differently because overload resolution is too eager to | 87 // Named differently because overload resolution is too eager to |
105 // convert implicitly to bool. | 88 // convert implicitly to bool. |
106 ListBuilder& AppendBoolean(bool in_value); | 89 ListBuilder& AppendBoolean(bool in_value); |
107 | 90 |
108 private: | 91 private: |
109 scoped_ptr<base::ListValue> list_; | 92 scoped_ptr<base::ListValue> list_; |
110 | 93 |
111 DISALLOW_COPY_AND_ASSIGN(ListBuilder); | 94 DISALLOW_COPY_AND_ASSIGN(ListBuilder); |
112 }; | 95 }; |
113 | 96 |
114 } // namespace extensions | 97 } // namespace extensions |
115 | 98 |
116 #endif // EXTENSIONS_COMMON_VALUE_BUILDER_H_ | 99 #endif // EXTENSIONS_COMMON_VALUE_BUILDER_H_ |
OLD | NEW |