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

Side by Side Diff: base/values.h

Issue 2558023002: Inline FundamentalValue into base::Value (Closed)
Patch Set: Value(Type) initializes with the default value Created 3 years, 12 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') | base/values.cc » ('J')
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 Value(); // A null value.
65
64 virtual ~Value(); 66 virtual ~Value();
65 67
66 static std::unique_ptr<Value> CreateNullValue(); 68 static std::unique_ptr<Value> CreateNullValue();
67 69
68 // Returns the name for a given |type|. 70 // Returns the name for a given |type|.
69 static const char* GetTypeName(Type type); 71 static const char* GetTypeName(Type type);
70 72
71 // Returns the type of the value stored by the current Value object. 73 // 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 74 // 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 75 // safe to use the Type to determine whether you can cast from
74 // Value* to (Implementing Class)*. Also, a Value object never changes 76 // Value* to (Implementing Class)*. Also, a Value object never changes
75 // its type after construction. 77 // its type after construction.
76 Type GetType() const { return type_; } 78 Type GetType() const { return type_; } // DEPRECATED, use type().
79 Type type() const { return type_; }
77 80
78 // Returns true if the current object represents a given type. 81 // Returns true if the current object represents a given type.
79 bool IsType(Type type) const { return type == type_; } 82 bool IsType(Type type) const { return type == type_; }
83 bool is_bool() const { return type() == Type::BOOLEAN; }
84 bool is_int() const { return type() == Type::INTEGER; }
85 bool is_double() const { return type() == Type::DOUBLE; }
86 bool is_string() const { return type() == Type::STRING; }
87 bool is_blob() const { return type() == Type::BINARY; }
88 bool is_dict() const { return type() == Type::DICTIONARY; }
89 bool is_list() const { return type() == Type::LIST; }
90
91 // These will all fatally assert if the type doesn't match.
92 bool GetBool() const;
93 int GetInt() const;
94 double GetDouble() const; // Implicitly converts from int if necessary.
80 95
81 // These methods allow the convenient retrieval of the contents of the Value. 96 // 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 97 // 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; 98 // returned through the |out_value| parameter and true is returned;
84 // otherwise, false is returned and |out_value| is unchanged. 99 // otherwise, false is returned and |out_value| is unchanged.
85 virtual bool GetAsBoolean(bool* out_value) const; 100 virtual bool GetAsBoolean(bool* out_value) const;
86 virtual bool GetAsInteger(int* out_value) const; 101 virtual bool GetAsInteger(int* out_value) const;
87 virtual bool GetAsDouble(double* out_value) const; 102 virtual bool GetAsDouble(double* out_value) const;
88 virtual bool GetAsString(std::string* out_value) const; 103 virtual bool GetAsString(std::string* out_value) const;
89 virtual bool GetAsString(string16* out_value) const; 104 virtual bool GetAsString(string16* out_value) const;
(...skipping 21 matching lines...) Expand all
111 virtual bool Equals(const Value* other) const; 126 virtual bool Equals(const Value* other) const;
112 127
113 // Compares if two Value objects have equal contents. Can handle NULLs. 128 // Compares if two Value objects have equal contents. Can handle NULLs.
114 // NULLs are considered equal but different from Value::CreateNullValue(). 129 // NULLs are considered equal but different from Value::CreateNullValue().
115 static bool Equals(const Value* a, const Value* b); 130 static bool Equals(const Value* a, const Value* b);
116 131
117 protected: 132 protected:
118 // These aren't safe for end-users, but they are useful for subclasses. 133 // These aren't safe for end-users, but they are useful for subclasses.
119 explicit Value(Type type); 134 explicit Value(Type type);
120 Value(const Value& that); 135 Value(const Value& that);
136 Value(Value&& that);
121 Value& operator=(const Value& that); 137 Value& operator=(const Value& that);
138 Value& operator=(Value&& that);
139
140 // TODO(brettw) move to public when FundamentalValue is gone.
141 explicit Value(bool in_bool);
142 explicit Value(int in_int);
143 explicit Value(double in_double);
122 144
123 private: 145 private:
146 void InternalCopyFrom(const Value& that);
147
124 Type type_; 148 Type type_;
149
150 union {
151 bool bool_value_;
152 int int_value_;
153 double double_value_;
154 };
125 }; 155 };
126 156
127 // FundamentalValue represents the simple fundamental types of values. 157 // FundamentalValue represents the simple fundamental types of values.
158 // TODO(brettw) remove when callers are updated to use raw Value.
128 class BASE_EXPORT FundamentalValue : public Value { 159 class BASE_EXPORT FundamentalValue : public Value {
129 public: 160 public:
130 explicit FundamentalValue(bool in_value); 161 explicit FundamentalValue(bool in_value);
131 explicit FundamentalValue(int in_value); 162 explicit FundamentalValue(int in_value);
132 explicit FundamentalValue(double in_value); 163 explicit FundamentalValue(double in_value);
133 ~FundamentalValue() override; 164 ~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 {
146 bool boolean_value_;
147 int integer_value_;
148 double double_value_;
149 };
150 }; 165 };
151 166
152 class BASE_EXPORT StringValue : public Value { 167 class BASE_EXPORT StringValue : public Value {
153 public: 168 public:
154 // Initializes a StringValue with a UTF-8 narrow character string. 169 // Initializes a StringValue with a UTF-8 narrow character string.
155 explicit StringValue(StringPiece in_value); 170 explicit StringValue(StringPiece in_value);
156 171
157 // Initializes a StringValue with a string16. 172 // Initializes a StringValue with a string16.
158 explicit StringValue(const string16& in_value); 173 explicit StringValue(const string16& in_value);
159 174
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 return out << static_cast<const Value&>(value); 579 return out << static_cast<const Value&>(value);
565 } 580 }
566 581
567 // Stream operator so that enum class Types can be used in log statements. 582 // Stream operator so that enum class Types can be used in log statements.
568 BASE_EXPORT std::ostream& operator<<(std::ostream& out, 583 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
569 const Value::Type& type); 584 const Value::Type& type);
570 585
571 } // namespace base 586 } // namespace base
572 587
573 #endif // BASE_VALUES_H_ 588 #endif // BASE_VALUES_H_
OLDNEW
« no previous file with comments | « no previous file | base/values.cc » ('j') | base/values.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698