OLD | NEW |
---|---|
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 using BinaryValue = Value; | 42 using BinaryValue = Value; |
43 | 43 |
44 // The Value class is the base class for Values. A Value can be instantiated | 44 // 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 | 45 // via the Create*Value() factory methods, or by directly creating instances of |
46 // the subclasses. | 46 // the subclasses. |
47 // | 47 // |
48 // See the file-level comment above for more information. | 48 // See the file-level comment above for more information. |
49 class BASE_EXPORT Value { | 49 class BASE_EXPORT Value { |
50 public: | 50 public: |
51 using DictStorage = std::map<std::string, std::unique_ptr<Value>>; | 51 using DictStorage = std::map<std::string, std::unique_ptr<Value>>; |
52 using ListStorage = std::vector<std::unique_ptr<Value>>; | 52 using ListStorage = std::vector<Value>; |
53 | 53 |
54 enum class Type { | 54 enum class Type { |
55 NONE = 0, | 55 NONE = 0, |
56 BOOLEAN, | 56 BOOLEAN, |
57 INTEGER, | 57 INTEGER, |
58 DOUBLE, | 58 DOUBLE, |
59 STRING, | 59 STRING, |
60 BINARY, | 60 BINARY, |
61 DICTIONARY, | 61 DICTIONARY, |
62 LIST | 62 LIST |
63 // Note: Do not add more types. See the file-level comment above for why. | 63 // Note: Do not add more types. See the file-level comment above for why. |
64 }; | 64 }; |
65 | 65 |
66 static std::unique_ptr<Value> CreateNullValue(); | 66 static std::unique_ptr<Value> CreateNullValue(); |
67 | 67 |
68 // For situations where you want to keep ownership of your buffer, this | 68 // For situations where you want to keep ownership of your buffer, this |
69 // factory method creates a new BinaryValue by copying the contents of the | 69 // factory method creates a new BinaryValue by copying the contents of the |
70 // buffer that's passed in. | 70 // buffer that's passed in. |
71 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead. | 71 // DEPRECATED, use MakeUnique<Value>(const std::vector<char>&) instead. |
72 // TODO(crbug.com/646113): Delete this and migrate callsites. | 72 // TODO(crbug.com/646113): Delete this and migrate callsites. |
73 static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer, | 73 static std::unique_ptr<BinaryValue> CreateWithCopiedBuffer(const char* buffer, |
74 size_t size); | 74 size_t size); |
75 | 75 |
76 Value(const Value& that); | 76 Value(const Value& that); |
77 Value(Value&& that); | 77 Value(Value&& that); |
jdoerrie
2017/03/15 10:10:41
Given the crash in components/browser_sync/profile
brettw
2017/03/17 05:39:19
Yeah, any move that we can write noexcept for we s
| |
78 Value(); // A null value. | 78 Value(); // A null value. |
79 explicit Value(Type type); | 79 explicit Value(Type type); |
80 explicit Value(bool in_bool); | 80 explicit Value(bool in_bool); |
81 explicit Value(int in_int); | 81 explicit Value(int in_int); |
82 explicit Value(double in_double); | 82 explicit Value(double in_double); |
83 | 83 |
84 // Value(const char*) and Value(const char16*) are required despite | 84 // Value(const char*) and Value(const char16*) are required despite |
85 // Value(const std::string&) and Value(const string16&) because otherwise the | 85 // Value(const std::string&) and Value(const string16&) because otherwise the |
86 // compiler will choose the Value(bool) constructor for these arguments. | 86 // compiler will choose the Value(bool) constructor for these arguments. |
87 // Value(std::string&&) allow for efficient move construction. | 87 // Value(std::string&&) allow for efficient move construction. |
88 // Value(StringPiece) exists due to many callsites passing StringPieces as | 88 // Value(StringPiece) exists due to many callsites passing StringPieces as |
89 // arguments. | 89 // arguments. |
90 explicit Value(const char* in_string); | 90 explicit Value(const char* in_string); |
91 explicit Value(const std::string& in_string); | 91 explicit Value(const std::string& in_string); |
92 explicit Value(std::string&& in_string); | 92 explicit Value(std::string&& in_string); |
93 explicit Value(const char16* in_string); | 93 explicit Value(const char16* in_string); |
94 explicit Value(const string16& in_string); | 94 explicit Value(const string16& in_string); |
95 explicit Value(StringPiece in_string); | 95 explicit Value(StringPiece in_string); |
96 | 96 |
97 explicit Value(const std::vector<char>& in_blob); | 97 explicit Value(const std::vector<char>& in_blob); |
98 explicit Value(std::vector<char>&& in_blob); | 98 explicit Value(std::vector<char>&& in_blob); |
99 | 99 |
100 explicit Value(const ListStorage& in_list); | |
Devlin
2017/03/15 15:25:03
Not my area, so no need to wait for my signoff, bu
jdoerrie
2017/03/16 19:01:56
Currently this is only used within |Value::DeepCop
| |
101 explicit Value(ListStorage&& in_list); | |
102 | |
100 Value& operator=(const Value& that); | 103 Value& operator=(const Value& that); |
101 Value& operator=(Value&& that); | 104 Value& operator=(Value&& that); |
102 | 105 |
103 ~Value(); | 106 ~Value(); |
104 | 107 |
105 // Returns the name for a given |type|. | 108 // Returns the name for a given |type|. |
106 static const char* GetTypeName(Type type); | 109 static const char* GetTypeName(Type type); |
107 | 110 |
108 // Returns the type of the value stored by the current Value object. | 111 // Returns the type of the value stored by the current Value object. |
109 // Each type will be implemented by only one subclass of Value, so it's | 112 // Each type will be implemented by only one subclass of Value, so it's |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); | 373 static std::unique_ptr<ListValue> From(std::unique_ptr<Value> value); |
371 | 374 |
372 ListValue(); | 375 ListValue(); |
373 | 376 |
374 // Clears the contents of this ListValue | 377 // Clears the contents of this ListValue |
375 void Clear(); | 378 void Clear(); |
376 | 379 |
377 // Returns the number of Values in this list. | 380 // Returns the number of Values in this list. |
378 size_t GetSize() const { return list_->size(); } | 381 size_t GetSize() const { return list_->size(); } |
379 | 382 |
383 // Returns the capacity of storage for Values in this list. | |
384 size_t capacity() const { return list_->capacity(); } | |
385 | |
380 // Returns whether the list is empty. | 386 // Returns whether the list is empty. |
381 bool empty() const { return list_->empty(); } | 387 bool empty() const { return list_->empty(); } |
382 | 388 |
389 // Reserves storage for at least |n| values. | |
390 void Reserve(size_t n); | |
jdoerrie
2017/03/15 10:10:41
I tried to minimize API changes, but these were st
| |
391 | |
383 // Sets the list item at the given index to be the Value specified by | 392 // Sets the list item at the given index to be the Value specified by |
384 // the value given. If the index beyond the current end of the list, null | 393 // the value given. If the index beyond the current end of the list, null |
385 // Values will be used to pad out the list. | 394 // Values will be used to pad out the list. |
386 // Returns true if successful, or false if the index was negative or | 395 // Returns true if successful, or false if the index was negative or |
387 // the value is a null pointer. | 396 // the value is a null pointer. |
388 bool Set(size_t index, Value* in_value); | 397 bool Set(size_t index, Value* in_value); |
389 // Preferred version of the above. TODO(estade): remove the above. | 398 // Preferred version of the above. TODO(estade): remove the above. |
390 bool Set(size_t index, std::unique_ptr<Value> in_value); | 399 bool Set(size_t index, std::unique_ptr<Value> in_value); |
391 | 400 |
392 // Gets the Value at the given index. Modifies |out_value| (and returns true) | 401 // Gets the Value at the given index. Modifies |out_value| (and returns true) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 // if not found. | 435 // if not found. |
427 bool Remove(const Value& value, size_t* index); | 436 bool Remove(const Value& value, size_t* index); |
428 | 437 |
429 // Removes the element at |iter|. If |out_value| is NULL, the value will be | 438 // Removes the element at |iter|. If |out_value| is NULL, the value will be |
430 // deleted, otherwise ownership of the value is passed back to the caller. | 439 // deleted, otherwise ownership of the value is passed back to the caller. |
431 // Returns an iterator pointing to the location of the element that | 440 // Returns an iterator pointing to the location of the element that |
432 // followed the erased element. | 441 // followed the erased element. |
433 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); | 442 iterator Erase(iterator iter, std::unique_ptr<Value>* out_value); |
434 | 443 |
435 // Appends a Value to the end of the list. | 444 // Appends a Value to the end of the list. |
436 void Append(std::unique_ptr<Value> in_value); | 445 void Append(std::unique_ptr<Value> in_value); |
jdoerrie
2017/03/15 10:10:41
Given how cumbersome and error-prone fixing |chrom
brettw
2017/03/15 22:14:14
STL doesn't return an iterator from vector::push_b
jdoerrie
2017/03/16 19:01:56
Acknowledged.
| |
437 #if !defined(OS_LINUX) | 446 #if !defined(OS_LINUX) |
438 // Deprecated version of the above. TODO(estade): remove. | 447 // Deprecated version of the above. TODO(estade): remove. |
439 void Append(Value* in_value); | 448 void Append(Value* in_value); |
440 #endif | 449 #endif |
441 | 450 |
442 // Convenience forms of Append. | 451 // Convenience forms of Append. |
443 void AppendBoolean(bool in_value); | 452 void AppendBoolean(bool in_value); |
444 void AppendInteger(int in_value); | 453 void AppendInteger(int in_value); |
445 void AppendDouble(double in_value); | 454 void AppendDouble(double in_value); |
446 void AppendString(StringPiece in_value); | 455 void AppendString(StringPiece in_value); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 return out << static_cast<const Value&>(value); | 526 return out << static_cast<const Value&>(value); |
518 } | 527 } |
519 | 528 |
520 // Stream operator so that enum class Types can be used in log statements. | 529 // Stream operator so that enum class Types can be used in log statements. |
521 BASE_EXPORT std::ostream& operator<<(std::ostream& out, | 530 BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
522 const Value::Type& type); | 531 const Value::Type& type); |
523 | 532 |
524 } // namespace base | 533 } // namespace base |
525 | 534 |
526 #endif // BASE_VALUES_H_ | 535 #endif // BASE_VALUES_H_ |
OLD | NEW |