| 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 pattern is to write: | 9 // The pattern is to write: |
| 10 // | 10 // |
| 11 // scoped_ptr<BuiltType> result(FooBuilder() | 11 // std::unique_ptr<BuiltType> result(FooBuilder() |
| 12 // .Set(args) | 12 // .Set(args) |
| 13 // .Set(args) | 13 // .Set(args) |
| 14 // .Build()); | 14 // .Build()); |
| 15 // | 15 // |
| 16 // The Build() method invalidates its builder, and returns ownership of the | 16 // The Build() method invalidates its builder, and returns ownership of the |
| 17 // built value. | 17 // built value. |
| 18 // | 18 // |
| 19 // 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 |
| 20 // 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 |
| 21 // less confusion than usual. | 21 // less confusion than usual. |
| 22 | 22 |
| 23 #ifndef EXTENSIONS_COMMON_VALUE_BUILDER_H_ | 23 #ifndef EXTENSIONS_COMMON_VALUE_BUILDER_H_ |
| 24 #define EXTENSIONS_COMMON_VALUE_BUILDER_H_ | 24 #define EXTENSIONS_COMMON_VALUE_BUILDER_H_ |
| 25 | 25 |
| 26 #include <memory> |
| 26 #include <string> | 27 #include <string> |
| 27 #include <utility> | 28 #include <utility> |
| 28 | 29 |
| 29 #include "base/macros.h" | 30 #include "base/macros.h" |
| 30 #include "base/memory/scoped_ptr.h" | |
| 31 #include "base/strings/string16.h" | 31 #include "base/strings/string16.h" |
| 32 | 32 |
| 33 namespace base { | 33 namespace base { |
| 34 class DictionaryValue; | 34 class DictionaryValue; |
| 35 class ListValue; | 35 class ListValue; |
| 36 class Value; | 36 class Value; |
| 37 } | 37 } |
| 38 | 38 |
| 39 namespace extensions { | 39 namespace extensions { |
| 40 | 40 |
| 41 class ListBuilder; | 41 class ListBuilder; |
| 42 | 42 |
| 43 class DictionaryBuilder { | 43 class DictionaryBuilder { |
| 44 public: | 44 public: |
| 45 DictionaryBuilder(); | 45 DictionaryBuilder(); |
| 46 explicit DictionaryBuilder(const base::DictionaryValue& init); | 46 explicit DictionaryBuilder(const base::DictionaryValue& init); |
| 47 ~DictionaryBuilder(); | 47 ~DictionaryBuilder(); |
| 48 | 48 |
| 49 // 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. |
| 50 scoped_ptr<base::DictionaryValue> Build() { return std::move(dict_); } | 50 std::unique_ptr<base::DictionaryValue> Build() { return std::move(dict_); } |
| 51 | 51 |
| 52 // 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 |
| 53 // times as you like. | 53 // times as you like. |
| 54 std::string ToJSON() const; | 54 std::string ToJSON() const; |
| 55 | 55 |
| 56 DictionaryBuilder& Set(const std::string& path, int in_value); | 56 DictionaryBuilder& Set(const std::string& path, int in_value); |
| 57 DictionaryBuilder& Set(const std::string& path, double in_value); | 57 DictionaryBuilder& Set(const std::string& path, double in_value); |
| 58 DictionaryBuilder& Set(const std::string& path, const std::string& in_value); | 58 DictionaryBuilder& Set(const std::string& path, const std::string& in_value); |
| 59 DictionaryBuilder& Set(const std::string& path, | 59 DictionaryBuilder& Set(const std::string& path, |
| 60 const base::string16& in_value); | 60 const base::string16& in_value); |
| 61 DictionaryBuilder& Set(const std::string& path, | 61 DictionaryBuilder& Set(const std::string& path, |
| 62 scoped_ptr<base::Value> in_value); | 62 std::unique_ptr<base::Value> in_value); |
| 63 | 63 |
| 64 // Named differently because overload resolution is too eager to | 64 // Named differently because overload resolution is too eager to |
| 65 // convert implicitly to bool. | 65 // convert implicitly to bool. |
| 66 DictionaryBuilder& SetBoolean(const std::string& path, bool in_value); | 66 DictionaryBuilder& SetBoolean(const std::string& path, bool in_value); |
| 67 | 67 |
| 68 private: | 68 private: |
| 69 scoped_ptr<base::DictionaryValue> dict_; | 69 std::unique_ptr<base::DictionaryValue> dict_; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 class ListBuilder { | 72 class ListBuilder { |
| 73 public: | 73 public: |
| 74 ListBuilder(); | 74 ListBuilder(); |
| 75 explicit ListBuilder(const base::ListValue& init); | 75 explicit ListBuilder(const base::ListValue& init); |
| 76 ~ListBuilder(); | 76 ~ListBuilder(); |
| 77 | 77 |
| 78 // 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. |
| 79 scoped_ptr<base::ListValue> Build() { return std::move(list_); } | 79 std::unique_ptr<base::ListValue> Build() { return std::move(list_); } |
| 80 | 80 |
| 81 ListBuilder& Append(int in_value); | 81 ListBuilder& Append(int in_value); |
| 82 ListBuilder& Append(double in_value); | 82 ListBuilder& Append(double in_value); |
| 83 ListBuilder& Append(const std::string& in_value); | 83 ListBuilder& Append(const std::string& in_value); |
| 84 ListBuilder& Append(const base::string16& in_value); | 84 ListBuilder& Append(const base::string16& in_value); |
| 85 ListBuilder& Append(scoped_ptr<base::Value> in_value); | 85 ListBuilder& Append(std::unique_ptr<base::Value> in_value); |
| 86 | 86 |
| 87 // Named differently because overload resolution is too eager to | 87 // Named differently because overload resolution is too eager to |
| 88 // convert implicitly to bool. | 88 // convert implicitly to bool. |
| 89 ListBuilder& AppendBoolean(bool in_value); | 89 ListBuilder& AppendBoolean(bool in_value); |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 scoped_ptr<base::ListValue> list_; | 92 std::unique_ptr<base::ListValue> list_; |
| 93 | 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(ListBuilder); | 94 DISALLOW_COPY_AND_ASSIGN(ListBuilder); |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 } // namespace extensions | 97 } // namespace extensions |
| 98 | 98 |
| 99 #endif // EXTENSIONS_COMMON_VALUE_BUILDER_H_ | 99 #endif // EXTENSIONS_COMMON_VALUE_BUILDER_H_ |
| OLD | NEW |