OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #ifndef TOOLS_GN_VALUE_H_ | 5 #ifndef TOOLS_GN_VALUE_H_ |
6 #define TOOLS_GN_VALUE_H_ | 6 #define TOOLS_GN_VALUE_H_ |
7 | 7 |
| 8 #include <map> |
| 9 |
8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
11 #include "tools/gn/err.h" | 14 #include "tools/gn/err.h" |
12 | 15 |
13 class ParseNode; | 16 class ParseNode; |
14 class Scope; | 17 class Scope; |
15 | 18 |
16 // Represents a variable value in the interpreter. | 19 // Represents a variable value in the interpreter. |
17 class Value { | 20 class Value { |
18 public: | 21 public: |
19 enum Type { | 22 enum Type { |
20 NONE = 0, | 23 NONE = 0, |
21 BOOLEAN, | 24 BOOLEAN, |
22 INTEGER, | 25 INTEGER, |
23 STRING, | 26 STRING, |
24 LIST, | 27 LIST, |
25 SCOPE | 28 SCOPE, |
26 }; | 29 }; |
27 | 30 |
28 Value(); | 31 Value(); |
29 Value(const ParseNode* origin, Type t); | 32 Value(const ParseNode* origin, Type t); |
30 Value(const ParseNode* origin, bool bool_val); | 33 Value(const ParseNode* origin, bool bool_val); |
31 Value(const ParseNode* origin, int64 int_val); | 34 Value(const ParseNode* origin, int64 int_val); |
32 Value(const ParseNode* origin, std::string str_val); | 35 Value(const ParseNode* origin, std::string str_val); |
33 Value(const ParseNode* origin, const char* str_val); | 36 Value(const ParseNode* origin, const char* str_val); |
34 Value(const ParseNode* origin, Scope* scope); // Non-owning ptr. | 37 // Values "shouldn't" have null scopes when type == Scope, so be sure to |
35 // (must outlive Value.) | 38 // always set one. However, this is not asserted since there are some |
| 39 // use-cases for creating values and immediately setting the scope on it. So |
| 40 // you can pass a null scope here if you promise to set it before any other |
| 41 // code gets it (code will generally assume the scope is not null). |
| 42 Value(const ParseNode* origin, scoped_ptr<Scope> scope); |
| 43 |
| 44 Value(const Value& other); |
36 ~Value(); | 45 ~Value(); |
37 | 46 |
| 47 Value& operator=(const Value& other); |
| 48 |
38 Type type() const { return type_; } | 49 Type type() const { return type_; } |
39 | 50 |
40 // Returns a string describing the given type. | 51 // Returns a string describing the given type. |
41 static const char* DescribeType(Type t); | 52 static const char* DescribeType(Type t); |
42 | 53 |
43 // Returns the node that made this. May be NULL. | 54 // Returns the node that made this. May be NULL. |
44 const ParseNode* origin() const { return origin_; } | 55 const ParseNode* origin() const { return origin_; } |
45 void set_origin(const ParseNode* o) { origin_ = o; } | 56 void set_origin(const ParseNode* o) { origin_ = o; } |
46 | 57 |
47 // Sets the origin of this value, recursively going into list child | 58 // Sets the origin of this value, recursively going into list child |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 DCHECK(type_ == LIST); | 90 DCHECK(type_ == LIST); |
80 return list_value_; | 91 return list_value_; |
81 } | 92 } |
82 const std::vector<Value>& list_value() const { | 93 const std::vector<Value>& list_value() const { |
83 DCHECK(type_ == LIST); | 94 DCHECK(type_ == LIST); |
84 return list_value_; | 95 return list_value_; |
85 } | 96 } |
86 | 97 |
87 Scope* scope_value() { | 98 Scope* scope_value() { |
88 DCHECK(type_ == SCOPE); | 99 DCHECK(type_ == SCOPE); |
89 return scope_value_; | 100 return scope_value_.get(); |
90 } | 101 } |
91 const Scope* scope_value() const { | 102 const Scope* scope_value() const { |
92 DCHECK(type_ == SCOPE); | 103 DCHECK(type_ == SCOPE); |
93 return scope_value_; | 104 return scope_value_.get(); |
94 } | 105 } |
| 106 void SetScopeValue(scoped_ptr<Scope> scope); |
95 | 107 |
96 // Converts the given value to a string. Returns true if strings should be | 108 // Converts the given value to a string. Returns true if strings should be |
97 // quoted or the ToString of a string should be the string itself. | 109 // quoted or the ToString of a string should be the string itself. |
98 std::string ToString(bool quote_strings) const; | 110 std::string ToString(bool quote_strings) const; |
99 | 111 |
100 // Verifies that the value is of the given type. If it isn't, returns | 112 // Verifies that the value is of the given type. If it isn't, returns |
101 // false and sets the error. | 113 // false and sets the error. |
102 bool VerifyTypeIs(Type t, Err* err) const; | 114 bool VerifyTypeIs(Type t, Err* err) const; |
103 | 115 |
104 // Compares values. Only the "value" is compared, not the origin. | 116 // Compares values. Only the "value" is compared, not the origin. |
105 bool operator==(const Value& other) const; | 117 bool operator==(const Value& other) const; |
106 bool operator!=(const Value& other) const; | 118 bool operator!=(const Value& other) const; |
107 | 119 |
108 private: | 120 private: |
| 121 // This are a lot of objects associated with every Value that need |
| 122 // initialization and tear down every time. It might be more efficient to |
| 123 // create a union of ManualConstructor objects (see SmallMap) and only |
| 124 // use the one we care about. |
109 Type type_; | 125 Type type_; |
110 std::string string_value_; | 126 std::string string_value_; |
111 bool boolean_value_; | 127 bool boolean_value_; |
112 int64 int_value_; | 128 int64 int_value_; |
113 std::vector<Value> list_value_; | 129 std::vector<Value> list_value_; |
114 Scope* scope_value_; // Non-owning. | 130 scoped_ptr<Scope> scope_value_; |
115 | 131 |
116 const ParseNode* origin_; | 132 const ParseNode* origin_; |
117 }; | 133 }; |
118 | 134 |
119 #endif // TOOLS_GN_VALUE_H_ | 135 #endif // TOOLS_GN_VALUE_H_ |
OLD | NEW |