Chromium Code Reviews| 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" |
| 10 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 11 #include "tools/gn/err.h" | 13 #include "tools/gn/err.h" |
| 12 | 14 |
| 13 class ParseNode; | 15 class ParseNode; |
| 14 class Scope; | 16 class Scope; |
| 15 | 17 |
| 16 // Represents a variable value in the interpreter. | 18 // Represents a variable value in the interpreter. |
| 17 class Value { | 19 class Value { |
| 18 public: | 20 public: |
| 19 enum Type { | 21 enum Type { |
| 20 NONE = 0, | 22 NONE = 0, |
| 21 BOOLEAN, | 23 BOOLEAN, |
| 22 INTEGER, | 24 INTEGER, |
| 23 STRING, | 25 STRING, |
| 24 LIST, | 26 LIST, |
| 25 SCOPE | 27 |
| 28 // Scopes and dictionaries act equivalently from the calling code | |
| 29 // perspective, but are created differently. The scope type is used in | |
| 30 // templates to refer to the invoker. Dictionaries are used when reading | |
| 31 // scopes from files and we actually want to copy and own the values. Both | |
|
scottmg
2014/04/03 20:44:40
Adding a second almost-identical seems pretty awkw
brettw
2014/04/03 23:19:24
I went around and around on this and at times had
| |
| 32 // are read-only from script and are read using "value.key" syntax. | |
| 33 // | |
| 34 // GN specifically doesn't expose a general writable dictionary type to the | |
| 35 // script because of the additional complexity, lack of a strong use case, | |
| 36 // and potential for abuse. We can add this if there is a very strong use | |
| 37 // case and we can come up with syntax we like. | |
| 38 // | |
| 39 // If we wanted to use this more generally, an alternative approach would | |
| 40 // be to refcount the Scope object so value could own a reference to it, | |
| 41 // and then delete the dictionary type. | |
| 42 SCOPE, | |
| 43 DICT, | |
| 26 }; | 44 }; |
| 27 | 45 |
| 28 Value(); | 46 Value(); |
| 29 Value(const ParseNode* origin, Type t); | 47 Value(const ParseNode* origin, Type t); |
| 30 Value(const ParseNode* origin, bool bool_val); | 48 Value(const ParseNode* origin, bool bool_val); |
| 31 Value(const ParseNode* origin, int64 int_val); | 49 Value(const ParseNode* origin, int64 int_val); |
| 32 Value(const ParseNode* origin, std::string str_val); | 50 Value(const ParseNode* origin, std::string str_val); |
| 33 Value(const ParseNode* origin, const char* str_val); | 51 Value(const ParseNode* origin, const char* str_val); |
| 34 Value(const ParseNode* origin, Scope* scope); // Non-owning ptr. | 52 Value(const ParseNode* origin, Scope* scope); // Non-owning ptr. |
| 35 // (must outlive Value.) | 53 // (must outlive Value.) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 | 104 |
| 87 Scope* scope_value() { | 105 Scope* scope_value() { |
| 88 DCHECK(type_ == SCOPE); | 106 DCHECK(type_ == SCOPE); |
| 89 return scope_value_; | 107 return scope_value_; |
| 90 } | 108 } |
| 91 const Scope* scope_value() const { | 109 const Scope* scope_value() const { |
| 92 DCHECK(type_ == SCOPE); | 110 DCHECK(type_ == SCOPE); |
| 93 return scope_value_; | 111 return scope_value_; |
| 94 } | 112 } |
| 95 | 113 |
| 114 std::map<std::string, Value>& dict_value() { | |
| 115 DCHECK(type_ == DICT); | |
| 116 return dict_value_; | |
| 117 } | |
| 118 const std::map<std::string, Value>& dict_value() const { | |
| 119 DCHECK(type_ == DICT); | |
| 120 return dict_value_; | |
| 121 } | |
| 122 | |
| 96 // Converts the given value to a string. Returns true if strings should be | 123 // 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. | 124 // quoted or the ToString of a string should be the string itself. |
| 98 std::string ToString(bool quote_strings) const; | 125 std::string ToString(bool quote_strings) const; |
| 99 | 126 |
| 100 // Verifies that the value is of the given type. If it isn't, returns | 127 // Verifies that the value is of the given type. If it isn't, returns |
| 101 // false and sets the error. | 128 // false and sets the error. |
| 102 bool VerifyTypeIs(Type t, Err* err) const; | 129 bool VerifyTypeIs(Type t, Err* err) const; |
| 103 | 130 |
| 104 // Compares values. Only the "value" is compared, not the origin. | 131 // Compares values. Only the "value" is compared, not the origin. |
| 105 bool operator==(const Value& other) const; | 132 bool operator==(const Value& other) const; |
| 106 bool operator!=(const Value& other) const; | 133 bool operator!=(const Value& other) const; |
| 107 | 134 |
| 108 private: | 135 private: |
| 136 // This is a lot of objects associated with every Value that need | |
| 137 // initialization and tear down every time. It might be more efficient to | |
| 138 // create a union of ManualConstructor objects (see SmallMap) and only | |
| 139 // use the one we care about. | |
| 109 Type type_; | 140 Type type_; |
| 110 std::string string_value_; | 141 std::string string_value_; |
| 111 bool boolean_value_; | 142 bool boolean_value_; |
| 112 int64 int_value_; | 143 int64 int_value_; |
| 113 std::vector<Value> list_value_; | 144 std::vector<Value> list_value_; |
| 114 Scope* scope_value_; // Non-owning. | 145 Scope* scope_value_; // Non-owning. |
| 146 std::map<std::string, Value> dict_value_; | |
| 115 | 147 |
| 116 const ParseNode* origin_; | 148 const ParseNode* origin_; |
| 117 }; | 149 }; |
| 118 | 150 |
| 119 #endif // TOOLS_GN_VALUE_H_ | 151 #endif // TOOLS_GN_VALUE_H_ |
| OLD | NEW |