Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / value_builder.h
blob2ca7693a61347e540487e742744ee1e7b305d562
1 // Copyright (c) 2012 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.
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:
11 // scoped_ptr<BuiltType> result(FooBuilder()
12 // .Set(args)
13 // .Set(args)
14 // .Build());
16 // For methods that take other built types, you can pass the builder directly
17 // to the setter without calling Build():
19 // DictionaryBuilder().Set("key", ListBuilder()
20 // .Append("foo").Append("bar") /* No .Build() */);
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 without setting at
24 // least 1 field.
26 // The Build() method invalidates its builder, and returns ownership of the
27 // built value.
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.
33 #ifndef CHROME_COMMON_EXTENSIONS_VALUE_BUILDER_H_
34 #define CHROME_COMMON_EXTENSIONS_VALUE_BUILDER_H_
36 #include <string>
38 #include "base/memory/scoped_ptr.h"
39 #include "base/string16.h"
40 #include "base/values.h"
42 namespace extensions {
44 class ListBuilder;
46 class DictionaryBuilder {
47 public:
48 DictionaryBuilder();
49 explicit DictionaryBuilder(const base::DictionaryValue& init);
50 ~DictionaryBuilder();
52 // Can only be called once, after which it's invalid to use the builder.
53 scoped_ptr<base::DictionaryValue> Build() { return dict_.Pass(); }
55 DictionaryBuilder& Set(const std::string& path, int in_value);
56 DictionaryBuilder& Set(const std::string& path, double in_value);
57 DictionaryBuilder& Set(const std::string& path, const std::string& in_value);
58 DictionaryBuilder& Set(const std::string& path, const string16& in_value);
59 DictionaryBuilder& Set(const std::string& path, DictionaryBuilder& in_value);
60 DictionaryBuilder& Set(const std::string& path, ListBuilder& in_value);
62 // Named differently because overload resolution is too eager to
63 // convert implicitly to bool.
64 DictionaryBuilder& SetBoolean(const std::string& path, bool in_value);
66 private:
67 scoped_ptr<base::DictionaryValue> dict_;
70 class ListBuilder {
71 public:
72 ListBuilder();
73 explicit ListBuilder(const base::ListValue& init);
74 ~ListBuilder();
76 // Can only be called once, after which it's invalid to use the builder.
77 scoped_ptr<base::ListValue> Build() { return list_.Pass(); }
79 ListBuilder& Append(int in_value);
80 ListBuilder& Append(double in_value);
81 ListBuilder& Append(const std::string& in_value);
82 ListBuilder& Append(const string16& in_value);
83 ListBuilder& Append(DictionaryBuilder& in_value);
84 ListBuilder& Append(ListBuilder& in_value);
86 // Named differently because overload resolution is too eager to
87 // convert implicitly to bool.
88 ListBuilder& AppendBoolean(bool in_value);
90 private:
91 scoped_ptr<base::ListValue> list_;
94 } // namespace extensions
96 #endif // CHROME_COMMON_EXTENSIONS_VALUE_BUILDER_H_