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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "tools/gn/err.h" | 11 #include "tools/gn/err.h" |
12 | 12 |
13 class ParseNode; | 13 class ParseNode; |
| 14 class Scope; |
14 | 15 |
15 // Represents a variable value in the interpreter. | 16 // Represents a variable value in the interpreter. |
16 class Value { | 17 class Value { |
17 public: | 18 public: |
18 enum Type { | 19 enum Type { |
19 NONE = 0, | 20 NONE = 0, |
20 BOOLEAN, | 21 BOOLEAN, |
21 INTEGER, | 22 INTEGER, |
22 STRING, | 23 STRING, |
23 LIST | 24 LIST, |
| 25 SCOPE |
24 }; | 26 }; |
25 | 27 |
26 Value(); | 28 Value(); |
27 Value(const ParseNode* origin, Type t); | 29 Value(const ParseNode* origin, Type t); |
28 Value(const ParseNode* origin, bool bool_val); | 30 Value(const ParseNode* origin, bool bool_val); |
29 Value(const ParseNode* origin, int64 int_val); | 31 Value(const ParseNode* origin, int64 int_val); |
30 Value(const ParseNode* origin, std::string str_val); | 32 Value(const ParseNode* origin, std::string str_val); |
31 Value(const ParseNode* origin, const char* str_val); | 33 Value(const ParseNode* origin, const char* str_val); |
| 34 Value(const ParseNode* origin, Scope* scope); // Non-owning ptr. |
| 35 // (must outlive Value.) |
32 ~Value(); | 36 ~Value(); |
33 | 37 |
34 Type type() const { return type_; } | 38 Type type() const { return type_; } |
35 | 39 |
36 // Returns a string describing the given type. | 40 // Returns a string describing the given type. |
37 static const char* DescribeType(Type t); | 41 static const char* DescribeType(Type t); |
38 | 42 |
39 // Returns the node that made this. May be NULL. | 43 // Returns the node that made this. May be NULL. |
40 const ParseNode* origin() const { return origin_; } | 44 const ParseNode* origin() const { return origin_; } |
41 void set_origin(const ParseNode* o) { origin_ = o; } | 45 void set_origin(const ParseNode* o) { origin_ = o; } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 77 |
74 std::vector<Value>& list_value() { | 78 std::vector<Value>& list_value() { |
75 DCHECK(type_ == LIST); | 79 DCHECK(type_ == LIST); |
76 return list_value_; | 80 return list_value_; |
77 } | 81 } |
78 const std::vector<Value>& list_value() const { | 82 const std::vector<Value>& list_value() const { |
79 DCHECK(type_ == LIST); | 83 DCHECK(type_ == LIST); |
80 return list_value_; | 84 return list_value_; |
81 } | 85 } |
82 | 86 |
| 87 Scope* scope_value() { |
| 88 DCHECK(type_ == SCOPE); |
| 89 return scope_value_; |
| 90 } |
| 91 const Scope* scope_value() const { |
| 92 DCHECK(type_ == SCOPE); |
| 93 return scope_value_; |
| 94 } |
| 95 |
83 // Converts the given value to a string. Returns true if strings should be | 96 // Converts the given value to a string. Returns true if strings should be |
84 // quoted or the ToString of a string should be the string itself. | 97 // quoted or the ToString of a string should be the string itself. |
85 std::string ToString(bool quote_strings) const; | 98 std::string ToString(bool quote_strings) const; |
86 | 99 |
87 // Verifies that the value is of the given type. If it isn't, returns | 100 // Verifies that the value is of the given type. If it isn't, returns |
88 // false and sets the error. | 101 // false and sets the error. |
89 bool VerifyTypeIs(Type t, Err* err) const; | 102 bool VerifyTypeIs(Type t, Err* err) const; |
90 | 103 |
91 // Compares values. Only the "value" is compared, not the origin. | 104 // Compares values. Only the "value" is compared, not the origin. |
92 bool operator==(const Value& other) const; | 105 bool operator==(const Value& other) const; |
93 bool operator!=(const Value& other) const; | 106 bool operator!=(const Value& other) const; |
94 | 107 |
95 private: | 108 private: |
96 Type type_; | 109 Type type_; |
97 std::string string_value_; | 110 std::string string_value_; |
98 bool boolean_value_; | 111 bool boolean_value_; |
99 int64 int_value_; | 112 int64 int_value_; |
100 std::vector<Value> list_value_; | 113 std::vector<Value> list_value_; |
| 114 Scope* scope_value_; // Non-owning. |
| 115 |
101 const ParseNode* origin_; | 116 const ParseNode* origin_; |
102 }; | 117 }; |
103 | 118 |
104 #endif // TOOLS_GN_VALUE_H_ | 119 #endif // TOOLS_GN_VALUE_H_ |
OLD | NEW |