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

Side by Side Diff: base/values.h

Issue 2645073002: Inline FundamentalValue into base::Value (Closed)
Patch Set: Use literals for default initialization. Created 3 years, 11 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
« no previous file with comments | « no previous file | base/values.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 19 matching lines...) Expand all
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/macros.h" 32 #include "base/macros.h"
33 #include "base/strings/string16.h" 33 #include "base/strings/string16.h"
34 #include "base/strings/string_piece.h" 34 #include "base/strings/string_piece.h"
35 35
36 namespace base { 36 namespace base {
37 37
38 class BinaryValue; 38 class BinaryValue;
39 class DictionaryValue; 39 class DictionaryValue;
40 class FundamentalValue;
41 class ListValue; 40 class ListValue;
42 class StringValue; 41 class StringValue;
43 class Value; 42 class Value;
43 using FundamentalValue = Value;
44 44
45 // 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
46 // 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
47 // the subclasses. 47 // the subclasses.
48 // 48 //
49 // See the file-level comment above for more information. 49 // See the file-level comment above for more information.
50 class BASE_EXPORT Value { 50 class BASE_EXPORT Value {
51 public: 51 public:
52 enum class Type { 52 enum class Type {
53 NONE = 0, 53 NONE = 0,
54 BOOLEAN, 54 BOOLEAN,
55 INTEGER, 55 INTEGER,
56 DOUBLE, 56 DOUBLE,
57 STRING, 57 STRING,
58 BINARY, 58 BINARY,
59 DICTIONARY, 59 DICTIONARY,
60 LIST 60 LIST
61 // Note: Do not add more types. See the file-level comment above for why. 61 // Note: Do not add more types. See the file-level comment above for why.
62 }; 62 };
63 63
64 static std::unique_ptr<Value> CreateNullValue();
65
66 Value(const Value& that);
67 Value(Value&& that);
68 Value(); // A null value.
69 explicit Value(Type type);
70 explicit Value(bool in_bool);
71 explicit Value(int in_int);
72 explicit Value(double in_double);
73
74 Value& operator=(const Value& that);
75 Value& operator=(Value&& that);
76
64 virtual ~Value(); 77 virtual ~Value();
65 78
66 static std::unique_ptr<Value> CreateNullValue();
67
68 // Returns the name for a given |type|. 79 // Returns the name for a given |type|.
69 static const char* GetTypeName(Type type); 80 static const char* GetTypeName(Type type);
70 81
71 // Returns the type of the value stored by the current Value object. 82 // Returns the type of the value stored by the current Value object.
72 // Each type will be implemented by only one subclass of Value, so it's 83 // Each type will be implemented by only one subclass of Value, so it's
73 // safe to use the Type to determine whether you can cast from 84 // safe to use the Type to determine whether you can cast from
74 // Value* to (Implementing Class)*. Also, a Value object never changes 85 // Value* to (Implementing Class)*. Also, a Value object never changes
75 // its type after construction. 86 // its type after construction.
76 Type GetType() const { return type_; } 87 Type GetType() const { return type_; } // DEPRECATED, use type().
88 Type type() const { return type_; }
77 89
78 // Returns true if the current object represents a given type. 90 // Returns true if the current object represents a given type.
79 bool IsType(Type type) const { return type == type_; } 91 bool IsType(Type type) const { return type == type_; }
92 bool is_bool() const { return type() == Type::BOOLEAN; }
93 bool is_int() const { return type() == Type::INTEGER; }
94 bool is_double() const { return type() == Type::DOUBLE; }
95 bool is_string() const { return type() == Type::STRING; }
96 bool is_blob() const { return type() == Type::BINARY; }
97 bool is_dict() const { return type() == Type::DICTIONARY; }
98 bool is_list() const { return type() == Type::LIST; }
99
100 // These will all fatally assert if the type doesn't match.
101 bool GetBool() const;
102 int GetInt() const;
103 double GetDouble() const; // Implicitly converts from int if necessary.
80 104
81 // These methods allow the convenient retrieval of the contents of the Value. 105 // These methods allow the convenient retrieval of the contents of the Value.
82 // If the current object can be converted into the given type, the value is 106 // If the current object can be converted into the given type, the value is
83 // returned through the |out_value| parameter and true is returned; 107 // returned through the |out_value| parameter and true is returned;
84 // otherwise, false is returned and |out_value| is unchanged. 108 // otherwise, false is returned and |out_value| is unchanged.
85 virtual bool GetAsBoolean(bool* out_value) const; 109 virtual bool GetAsBoolean(bool* out_value) const;
86 virtual bool GetAsInteger(int* out_value) const; 110 virtual bool GetAsInteger(int* out_value) const;
87 virtual bool GetAsDouble(double* out_value) const; 111 virtual bool GetAsDouble(double* out_value) const;
88 virtual bool GetAsString(std::string* out_value) const; 112 virtual bool GetAsString(std::string* out_value) const;
89 virtual bool GetAsString(string16* out_value) const; 113 virtual bool GetAsString(string16* out_value) const;
90 virtual bool GetAsString(const StringValue** out_value) const; 114 virtual bool GetAsString(const StringValue** out_value) const;
91 virtual bool GetAsString(StringPiece* out_value) const; 115 virtual bool GetAsString(StringPiece* out_value) const;
92 virtual bool GetAsBinary(const BinaryValue** out_value) const; 116 virtual bool GetAsBinary(const BinaryValue** out_value) const;
93 // ListValue::From is the equivalent for std::unique_ptr conversions. 117 // ListValue::From is the equivalent for std::unique_ptr conversions.
94 virtual bool GetAsList(ListValue** out_value); 118 virtual bool GetAsList(ListValue** out_value);
95 virtual bool GetAsList(const ListValue** out_value) const; 119 virtual bool GetAsList(const ListValue** out_value) const;
96 // DictionaryValue::From is the equivalent for std::unique_ptr conversions. 120 // DictionaryValue::From is the equivalent for std::unique_ptr conversions.
97 virtual bool GetAsDictionary(DictionaryValue** out_value); 121 virtual bool GetAsDictionary(DictionaryValue** out_value);
98 virtual bool GetAsDictionary(const DictionaryValue** out_value) const; 122 virtual bool GetAsDictionary(const DictionaryValue** out_value) const;
99 // Note: Do not add more types. See the file-level comment above for why. 123 // Note: Do not add more types. See the file-level comment above for why.
100 124
101 // This creates a deep copy of the entire Value tree, and returns a pointer 125 // This creates a deep copy of the entire Value tree, and returns a pointer
102 // to the copy. The caller gets ownership of the copy, of course. 126 // to the copy. The caller gets ownership of the copy, of course.
103 //
104 // Subclasses return their own type directly in their overrides; 127 // Subclasses return their own type directly in their overrides;
105 // this works because C++ supports covariant return types. 128 // this works because C++ supports covariant return types.
106 virtual Value* DeepCopy() const; 129 virtual Value* DeepCopy() const;
107 // Preferred version of DeepCopy. TODO(estade): remove the above. 130 // Preferred version of DeepCopy. TODO(estade): remove the above.
108 std::unique_ptr<Value> CreateDeepCopy() const; 131 std::unique_ptr<Value> CreateDeepCopy() const;
109 132
110 // Compares if two Value objects have equal contents. 133 // Compares if two Value objects have equal contents.
111 virtual bool Equals(const Value* other) const; 134 virtual bool Equals(const Value* other) const;
112 135
113 // Compares if two Value objects have equal contents. Can handle NULLs. 136 // Compares if two Value objects have equal contents. Can handle NULLs.
114 // NULLs are considered equal but different from Value::CreateNullValue(). 137 // NULLs are considered equal but different from Value::CreateNullValue().
115 static bool Equals(const Value* a, const Value* b); 138 static bool Equals(const Value* a, const Value* b);
116 139
117 protected: 140 private:
118 // These aren't safe for end-users, but they are useful for subclasses. 141 void InternalCopyFrom(const Value& that);
119 explicit Value(Type type);
120 Value(const Value& that);
121 Value& operator=(const Value& that);
122 142
123 private:
124 Type type_; 143 Type type_;
125 };
126 144
127 // FundamentalValue represents the simple fundamental types of values.
128 class BASE_EXPORT FundamentalValue : public Value {
129 public:
130 explicit FundamentalValue(bool in_value);
131 explicit FundamentalValue(int in_value);
132 explicit FundamentalValue(double in_value);
133 ~FundamentalValue() override;
134
135 // Overridden from Value:
136 bool GetAsBoolean(bool* out_value) const override;
137 bool GetAsInteger(int* out_value) const override;
138 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
139 // doubles.
140 bool GetAsDouble(double* out_value) const override;
141 FundamentalValue* DeepCopy() const override;
142 bool Equals(const Value* other) const override;
143
144 private:
145 union { 145 union {
146 bool boolean_value_; 146 bool bool_value_;
147 int integer_value_; 147 int int_value_;
148 double double_value_; 148 double double_value_;
149 }; 149 };
150 }; 150 };
151 151
152 class BASE_EXPORT StringValue : public Value { 152 class BASE_EXPORT StringValue : public Value {
153 public: 153 public:
154 // Initializes a StringValue with a UTF-8 narrow character string. 154 // Initializes a StringValue with a UTF-8 narrow character string.
155 explicit StringValue(StringPiece in_value); 155 explicit StringValue(StringPiece in_value);
156 156
157 // Initializes a StringValue with a string16. 157 // Initializes a StringValue with a string16.
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 std::string* error_str) = 0; 538 std::string* error_str) = 0;
539 }; 539 };
540 540
541 // Stream operator so Values can be used in assertion statements. In order that 541 // Stream operator so Values can be used in assertion statements. In order that
542 // gtest uses this operator to print readable output on test failures, we must 542 // gtest uses this operator to print readable output on test failures, we must
543 // override each specific type. Otherwise, the default template implementation 543 // override each specific type. Otherwise, the default template implementation
544 // is preferred over an upcast. 544 // is preferred over an upcast.
545 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value); 545 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Value& value);
546 546
547 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 547 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
548 const FundamentalValue& value) {
549 return out << static_cast<const Value&>(value);
550 }
551
552 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
553 const StringValue& value) { 548 const StringValue& value) {
554 return out << static_cast<const Value&>(value); 549 return out << static_cast<const Value&>(value);
555 } 550 }
556 551
557 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 552 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
558 const DictionaryValue& value) { 553 const DictionaryValue& value) {
559 return out << static_cast<const Value&>(value); 554 return out << static_cast<const Value&>(value);
560 } 555 }
561 556
562 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out, 557 BASE_EXPORT inline std::ostream& operator<<(std::ostream& out,
563 const ListValue& value) { 558 const ListValue& value) {
564 return out << static_cast<const Value&>(value); 559 return out << static_cast<const Value&>(value);
565 } 560 }
566 561
567 // Stream operator so that enum class Types can be used in log statements. 562 // Stream operator so that enum class Types can be used in log statements.
568 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 563 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
569 const Value::Type& type); 564 const Value::Type& type);
570 565
571 } // namespace base 566 } // namespace base
572 567
573 #endif // BASE_VALUES_H_ 568 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « no previous file | base/values.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698