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 #include "tools/gn/value.h" | 5 #include "tools/gn/value.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 | 8 |
9 Value::Value() | 9 Value::Value() |
10 : type_(NONE), | 10 : type_(NONE), |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 case BOOLEAN: | 85 case BOOLEAN: |
86 return "boolean"; | 86 return "boolean"; |
87 case INTEGER: | 87 case INTEGER: |
88 return "integer"; | 88 return "integer"; |
89 case STRING: | 89 case STRING: |
90 return "string"; | 90 return "string"; |
91 case LIST: | 91 case LIST: |
92 return "list"; | 92 return "list"; |
93 case SCOPE: | 93 case SCOPE: |
94 return "scope"; | 94 return "scope"; |
| 95 case DICT: |
| 96 return "dict"; |
95 default: | 97 default: |
96 NOTREACHED(); | 98 NOTREACHED(); |
97 return "UNKNOWN"; | 99 return "UNKNOWN"; |
98 } | 100 } |
99 } | 101 } |
100 | 102 |
101 std::string Value::ToString(bool quote_string) const { | 103 std::string Value::ToString(bool quote_string) const { |
102 switch (type_) { | 104 switch (type_) { |
103 case NONE: | 105 case NONE: |
104 return "<void>"; | 106 return "<void>"; |
(...skipping 10 matching lines...) Expand all Loading... |
115 for (size_t i = 0; i < list_value_.size(); i++) { | 117 for (size_t i = 0; i < list_value_.size(); i++) { |
116 if (i > 0) | 118 if (i > 0) |
117 result += ", "; | 119 result += ", "; |
118 result += list_value_[i].ToString(true); | 120 result += list_value_[i].ToString(true); |
119 } | 121 } |
120 result.push_back(']'); | 122 result.push_back(']'); |
121 return result; | 123 return result; |
122 } | 124 } |
123 case SCOPE: | 125 case SCOPE: |
124 return std::string("<scope>"); | 126 return std::string("<scope>"); |
| 127 case DICT: { |
| 128 std::string result = "{\n"; |
| 129 for (std::map<std::string, Value>::const_iterator i = dict_value_.begin(); |
| 130 i != dict_value_.end(); ++i) { |
| 131 result += " " + i->first + " = " + i->second.ToString(true) + "\n"; |
| 132 } |
| 133 result += "}"; |
| 134 return result; |
| 135 } |
125 } | 136 } |
126 return std::string(); | 137 return std::string(); |
127 } | 138 } |
128 | 139 |
129 bool Value::VerifyTypeIs(Type t, Err* err) const { | 140 bool Value::VerifyTypeIs(Type t, Err* err) const { |
130 if (type_ == t) | 141 if (type_ == t) |
131 return true; | 142 return true; |
132 | 143 |
133 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + "."); | 144 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + "."); |
134 return false; | 145 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
147 return string_value() == other.string_value(); | 158 return string_value() == other.string_value(); |
148 case Value::LIST: | 159 case Value::LIST: |
149 if (list_value().size() != other.list_value().size()) | 160 if (list_value().size() != other.list_value().size()) |
150 return false; | 161 return false; |
151 for (size_t i = 0; i < list_value().size(); i++) { | 162 for (size_t i = 0; i < list_value().size(); i++) { |
152 if (list_value()[i] != other.list_value()[i]) | 163 if (list_value()[i] != other.list_value()[i]) |
153 return false; | 164 return false; |
154 } | 165 } |
155 return true; | 166 return true; |
156 case Value::SCOPE: | 167 case Value::SCOPE: |
157 // Its not clear what people mean when comparing scope values, so we test | 168 // Scopes are always considered not equal because there's currently |
158 // for scope identity and not contents equality. | 169 // no use case for comparing them, and it requires a bunch of complex |
159 return scope_value() == other.scope_value(); | 170 // iteration code. |
| 171 return false; |
| 172 case Value::DICT: |
| 173 return dict_value_.size() == other.dict_value_.size() && |
| 174 std::equal(dict_value_.begin(), dict_value_.end(), |
| 175 other.dict_value_.begin()); |
160 default: | 176 default: |
161 return false; | 177 return false; |
162 } | 178 } |
163 } | 179 } |
164 | 180 |
165 bool Value::operator!=(const Value& other) const { | 181 bool Value::operator!=(const Value& other) const { |
166 return !operator==(other); | 182 return !operator==(other); |
167 } | 183 } |
OLD | NEW |