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 <stdint.h> | 8 #include <stdint.h> |
| 9 |
9 #include <map> | 10 #include <map> |
| 11 #include <memory> |
10 | 12 |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "tools/gn/err.h" | 15 #include "tools/gn/err.h" |
15 | 16 |
16 class ParseNode; | 17 class ParseNode; |
17 class Scope; | 18 class Scope; |
18 | 19 |
19 // Represents a variable value in the interpreter. | 20 // Represents a variable value in the interpreter. |
20 class Value { | 21 class Value { |
21 public: | 22 public: |
22 enum Type { | 23 enum Type { |
23 NONE = 0, | 24 NONE = 0, |
24 BOOLEAN, | 25 BOOLEAN, |
25 INTEGER, | 26 INTEGER, |
26 STRING, | 27 STRING, |
27 LIST, | 28 LIST, |
28 SCOPE, | 29 SCOPE, |
29 }; | 30 }; |
30 | 31 |
31 Value(); | 32 Value(); |
32 Value(const ParseNode* origin, Type t); | 33 Value(const ParseNode* origin, Type t); |
33 Value(const ParseNode* origin, bool bool_val); | 34 Value(const ParseNode* origin, bool bool_val); |
34 Value(const ParseNode* origin, int64_t int_val); | 35 Value(const ParseNode* origin, int64_t int_val); |
35 Value(const ParseNode* origin, std::string str_val); | 36 Value(const ParseNode* origin, std::string str_val); |
36 Value(const ParseNode* origin, const char* str_val); | 37 Value(const ParseNode* origin, const char* str_val); |
37 // Values "shouldn't" have null scopes when type == Scope, so be sure to | 38 // Values "shouldn't" have null scopes when type == Scope, so be sure to |
38 // always set one. However, this is not asserted since there are some | 39 // 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 // 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 // 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 // code gets it (code will generally assume the scope is not null). |
42 Value(const ParseNode* origin, scoped_ptr<Scope> scope); | 43 Value(const ParseNode* origin, std::unique_ptr<Scope> scope); |
43 | 44 |
44 Value(const Value& other); | 45 Value(const Value& other); |
45 ~Value(); | 46 ~Value(); |
46 | 47 |
47 Value& operator=(const Value& other); | 48 Value& operator=(const Value& other); |
48 | 49 |
49 Type type() const { return type_; } | 50 Type type() const { return type_; } |
50 | 51 |
51 // Returns a string describing the given type. | 52 // Returns a string describing the given type. |
52 static const char* DescribeType(Type t); | 53 static const char* DescribeType(Type t); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 93 } |
93 | 94 |
94 Scope* scope_value() { | 95 Scope* scope_value() { |
95 DCHECK(type_ == SCOPE); | 96 DCHECK(type_ == SCOPE); |
96 return scope_value_.get(); | 97 return scope_value_.get(); |
97 } | 98 } |
98 const Scope* scope_value() const { | 99 const Scope* scope_value() const { |
99 DCHECK(type_ == SCOPE); | 100 DCHECK(type_ == SCOPE); |
100 return scope_value_.get(); | 101 return scope_value_.get(); |
101 } | 102 } |
102 void SetScopeValue(scoped_ptr<Scope> scope); | 103 void SetScopeValue(std::unique_ptr<Scope> scope); |
103 | 104 |
104 // Converts the given value to a string. Returns true if strings should be | 105 // Converts the given value to a string. Returns true if strings should be |
105 // quoted or the ToString of a string should be the string itself. If the | 106 // quoted or the ToString of a string should be the string itself. If the |
106 // string is quoted, it will also enable escaping. | 107 // string is quoted, it will also enable escaping. |
107 std::string ToString(bool quote_strings) const; | 108 std::string ToString(bool quote_strings) const; |
108 | 109 |
109 // Verifies that the value is of the given type. If it isn't, returns | 110 // Verifies that the value is of the given type. If it isn't, returns |
110 // false and sets the error. | 111 // false and sets the error. |
111 bool VerifyTypeIs(Type t, Err* err) const; | 112 bool VerifyTypeIs(Type t, Err* err) const; |
112 | 113 |
113 // Compares values. Only the "value" is compared, not the origin. | 114 // Compares values. Only the "value" is compared, not the origin. |
114 bool operator==(const Value& other) const; | 115 bool operator==(const Value& other) const; |
115 bool operator!=(const Value& other) const; | 116 bool operator!=(const Value& other) const; |
116 | 117 |
117 private: | 118 private: |
118 // This are a lot of objects associated with every Value that need | 119 // This are a lot of objects associated with every Value that need |
119 // initialization and tear down every time. It might be more efficient to | 120 // initialization and tear down every time. It might be more efficient to |
120 // create a union of ManualConstructor objects (see SmallMap) and only | 121 // create a union of ManualConstructor objects (see SmallMap) and only |
121 // use the one we care about. | 122 // use the one we care about. |
122 Type type_; | 123 Type type_; |
123 std::string string_value_; | 124 std::string string_value_; |
124 bool boolean_value_; | 125 bool boolean_value_; |
125 int64_t int_value_; | 126 int64_t int_value_; |
126 std::vector<Value> list_value_; | 127 std::vector<Value> list_value_; |
127 scoped_ptr<Scope> scope_value_; | 128 std::unique_ptr<Scope> scope_value_; |
128 | 129 |
129 const ParseNode* origin_; | 130 const ParseNode* origin_; |
130 }; | 131 }; |
131 | 132 |
132 #endif // TOOLS_GN_VALUE_H_ | 133 #endif // TOOLS_GN_VALUE_H_ |
OLD | NEW |