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 // 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 7 // from other sub-projects. 8 // 9 // The general pattern is to write: 10 // 11 // scoped_ptr<BuiltType> result(FooBuilder() 12 // .Set(args) 13 // .Set(args) 14 // .Build()); 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", ListBuilder() 20 // .Append("foo").Append("bar") /* No .Build() */); 21 // 22 // Because of limitations in C++03, and to avoid extra copies, you can't pass a 23 // just-constructed Builder into another Builder's method directly. Use the 24 // Pass() method. 25 // 26 // The Build() method invalidates its builder, and returns ownership of the 27 // built value. 28 // 29 // These objects are intended to be used as temporaries rather than stored 30 // anywhere, so the use of non-const reference parameters is likely to cause 31 // less confusion than usual. 32 33 #ifndef EXTENSIONS_COMMON_VALUE_BUILDER_H_ 34 #define EXTENSIONS_COMMON_VALUE_BUILDER_H_ 35 36 #include <string> 37 38 #include "base/memory/scoped_ptr.h" 39 #include "base/strings/string16.h" 40 #include "base/values.h" 41 42 namespace extensions { 43 44 class ListBuilder; 45 46 class DictionaryBuilder { 47 public: 48 DictionaryBuilder(); 49 explicit DictionaryBuilder(const base::DictionaryValue& init); 50 ~DictionaryBuilder(); 51 52 // Workaround to allow you to pass rvalue ExtensionBuilders by reference to 53 // other functions. Pass()54 DictionaryBuilder& Pass() { return *this; } 55 56 // Can only be called once, after which it's invalid to use the builder. Build()57 scoped_ptr<base::DictionaryValue> Build() { return dict_.Pass(); } 58 59 DictionaryBuilder& Set(const std::string& path, int in_value); 60 DictionaryBuilder& Set(const std::string& path, double in_value); 61 DictionaryBuilder& Set(const std::string& path, const std::string& in_value); 62 DictionaryBuilder& Set(const std::string& path, 63 const base::string16& in_value); 64 DictionaryBuilder& Set(const std::string& path, DictionaryBuilder& in_value); 65 DictionaryBuilder& Set(const std::string& path, ListBuilder& in_value); 66 67 // Named differently because overload resolution is too eager to 68 // convert implicitly to bool. 69 DictionaryBuilder& SetBoolean(const std::string& path, bool in_value); 70 71 private: 72 scoped_ptr<base::DictionaryValue> dict_; 73 }; 74 75 class ListBuilder { 76 public: 77 ListBuilder(); 78 explicit ListBuilder(const base::ListValue& init); 79 ~ListBuilder(); 80 81 // Workaround to allow you to pass rvalue ExtensionBuilders by reference to 82 // other functions. Pass()83 ListBuilder& Pass() { return *this; } 84 85 // Can only be called once, after which it's invalid to use the builder. Build()86 scoped_ptr<base::ListValue> Build() { return list_.Pass(); } 87 88 ListBuilder& Append(int in_value); 89 ListBuilder& Append(double in_value); 90 ListBuilder& Append(const std::string& in_value); 91 ListBuilder& Append(const base::string16& in_value); 92 ListBuilder& Append(DictionaryBuilder& in_value); 93 ListBuilder& Append(ListBuilder& in_value); 94 95 // Named differently because overload resolution is too eager to 96 // convert implicitly to bool. 97 ListBuilder& AppendBoolean(bool in_value); 98 99 private: 100 scoped_ptr<base::ListValue> list_; 101 }; 102 103 } // namespace extensions 104 105 #endif // EXTENSIONS_COMMON_VALUE_BUILDER_H_ 106