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

Side by Side Diff: base/values.h

Issue 2807953002: Use base::flat_map for base::Value dictionary storage. (Closed)
Patch Set: Swap Created 3 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 specifies a recursive data storage class called Value intended for 5 // This file specifies a recursive data storage class called Value intended for
6 // storing settings and other persistable data. 6 // storing settings and other persistable data.
7 // 7 //
8 // A Value represents something that can be stored in JSON or passed to/from 8 // A Value represents something that can be stored in JSON or passed to/from
9 // JavaScript. As such, it is NOT a generalized variant type, since only the 9 // JavaScript. As such, it is NOT a generalized variant type, since only the
10 // types supported by JavaScript/JSON are supported. 10 // types supported by JavaScript/JSON are supported.
(...skipping 11 matching lines...) Expand all
22 22
23 #include <iosfwd> 23 #include <iosfwd>
24 #include <map> 24 #include <map>
25 #include <memory> 25 #include <memory>
26 #include <string> 26 #include <string>
27 #include <utility> 27 #include <utility>
28 #include <vector> 28 #include <vector>
29 29
30 #include "base/base_export.h" 30 #include "base/base_export.h"
31 #include "base/compiler_specific.h" 31 #include "base/compiler_specific.h"
32 #include "base/containers/flat_map.h"
32 #include "base/macros.h" 33 #include "base/macros.h"
33 #include "base/memory/manual_constructor.h" 34 #include "base/memory/manual_constructor.h"
34 #include "base/strings/string16.h" 35 #include "base/strings/string16.h"
35 #include "base/strings/string_piece.h" 36 #include "base/strings/string_piece.h"
36 37
37 namespace base { 38 namespace base {
38 39
39 class DictionaryValue; 40 class DictionaryValue;
40 class ListValue; 41 class ListValue;
41 class Value; 42 class Value;
42 using BinaryValue = Value; 43 using BinaryValue = Value;
43 44
44 // The Value class is the base class for Values. A Value can be instantiated 45 // The Value class is the base class for Values. A Value can be instantiated
45 // via the Create*Value() factory methods, or by directly creating instances of 46 // via the Create*Value() factory methods, or by directly creating instances of
46 // the subclasses. 47 // the subclasses.
47 // 48 //
48 // See the file-level comment above for more information. 49 // See the file-level comment above for more information.
49 class BASE_EXPORT Value { 50 class BASE_EXPORT Value {
50 public: 51 public:
51 using DictStorage = std::map<std::string, std::unique_ptr<Value>>; 52 using DictStorage = base::flat_map<std::string, std::unique_ptr<Value>>;
52 using ListStorage = std::vector<std::unique_ptr<Value>>; 53 using ListStorage = std::vector<std::unique_ptr<Value>>;
53 54
54 enum class Type { 55 enum class Type {
55 NONE = 0, 56 NONE = 0,
56 BOOLEAN, 57 BOOLEAN,
57 INTEGER, 58 INTEGER,
58 DOUBLE, 59 DOUBLE,
59 STRING, 60 STRING,
60 BINARY, 61 BINARY,
61 DICTIONARY, 62 DICTIONARY,
(...skipping 23 matching lines...) Expand all
85 // Value(std::string&&) allow for efficient move construction. 86 // Value(std::string&&) allow for efficient move construction.
86 // Value(StringPiece) exists due to many callsites passing StringPieces as 87 // Value(StringPiece) exists due to many callsites passing StringPieces as
87 // arguments. 88 // arguments.
88 explicit Value(const char* in_string); 89 explicit Value(const char* in_string);
89 explicit Value(const std::string& in_string); 90 explicit Value(const std::string& in_string);
90 explicit Value(std::string&& in_string) noexcept; 91 explicit Value(std::string&& in_string) noexcept;
91 explicit Value(const char16* in_string); 92 explicit Value(const char16* in_string);
92 explicit Value(const string16& in_string); 93 explicit Value(const string16& in_string);
93 explicit Value(StringPiece in_string); 94 explicit Value(StringPiece in_string);
94 95
96 explicit Value(DictStorage&& in_dict) noexcept;
jdoerrie 2017/04/10 11:44:57 nit: Consider moving this constructor down after t
97
95 explicit Value(const std::vector<char>& in_blob); 98 explicit Value(const std::vector<char>& in_blob);
96 explicit Value(std::vector<char>&& in_blob) noexcept; 99 explicit Value(std::vector<char>&& in_blob) noexcept;
97 100
98 Value& operator=(const Value& that); 101 Value& operator=(const Value& that);
99 Value& operator=(Value&& that) noexcept; 102 Value& operator=(Value&& that) noexcept;
100 103
101 ~Value(); 104 ~Value();
102 105
103 // Returns the name for a given |type|. 106 // Returns the name for a given |type|.
104 static const char* GetTypeName(Type type); 107 static const char* GetTypeName(Type type);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 191
189 union { 192 union {
190 bool bool_value_; 193 bool bool_value_;
191 int int_value_; 194 int int_value_;
192 double double_value_; 195 double double_value_;
193 ManualConstructor<std::string> string_value_; 196 ManualConstructor<std::string> string_value_;
194 ManualConstructor<std::vector<char>> binary_value_; 197 ManualConstructor<std::vector<char>> binary_value_;
195 // For current gcc and clang sizeof(DictStorage) = 48, which would result 198 // For current gcc and clang sizeof(DictStorage) = 48, which would result
196 // in sizeof(Value) = 56 if DictStorage was stack allocated. Allocating it 199 // in sizeof(Value) = 56 if DictStorage was stack allocated. Allocating it
197 // on the heap results in sizeof(Value) = 40 for all of gcc, clang and MSVC. 200 // on the heap results in sizeof(Value) = 40 for all of gcc, clang and MSVC.
198 ManualConstructor<std::unique_ptr<DictStorage>> dict_ptr_; 201 ManualConstructor<std::unique_ptr<DictStorage>> dict_ptr_;
jdoerrie 2017/04/10 11:44:57 Now that DictStorage is effectively a vector there
199 ManualConstructor<ListStorage> list_; 202 ManualConstructor<ListStorage> list_;
200 }; 203 };
201 204
202 private: 205 private:
203 void InternalCopyFundamentalValue(const Value& that); 206 void InternalCopyFundamentalValue(const Value& that);
204 void InternalCopyConstructFrom(const Value& that); 207 void InternalCopyConstructFrom(const Value& that);
205 void InternalMoveConstructFrom(Value&& that); 208 void InternalMoveConstructFrom(Value&& that);
206 void InternalCopyAssignFromSameType(const Value& that); 209 void InternalCopyAssignFromSameType(const Value& that);
207 void InternalCleanup(); 210 void InternalCleanup();
208 }; 211 };
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 return out << static_cast<const Value&>(value); 533 return out << static_cast<const Value&>(value);
531 } 534 }
532 535
533 // Stream operator so that enum class Types can be used in log statements. 536 // Stream operator so that enum class Types can be used in log statements.
534 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 537 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
535 const Value::Type& type); 538 const Value::Type& type);
536 539
537 } // namespace base 540 } // namespace base
538 541
539 #endif // BASE_VALUES_H_ 542 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « base/json/json_parser.cc ('k') | base/values.cc » ('j') | base/values.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698