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
3 // found in the LICENSE file.
5 #include "tools/gn/value.h"
7 #include "base/strings/string_number_conversions.h"
11 boolean_value_(false),
17 Value::Value(const ParseNode* origin, Type t)
19 boolean_value_(false),
25 Value::Value(const ParseNode* origin, bool bool_val)
27 boolean_value_(bool_val),
33 Value::Value(const ParseNode* origin, int64 int_val)
35 boolean_value_(false),
41 Value::Value(const ParseNode* origin, std::string str_val)
44 boolean_value_(false),
48 string_value_.swap(str_val);
51 Value::Value(const ParseNode* origin, const char* str_val)
53 string_value_(str_val),
54 boolean_value_(false),
60 Value::Value(const ParseNode* origin, Scope* scope)
63 boolean_value_(false),
72 void Value::RecursivelySetOrigin(const ParseNode* origin) {
74 if (type_ == Value::LIST) {
75 for (size_t i = 0; i < list_value_.size(); i++)
76 list_value_[i].RecursivelySetOrigin(origin);
81 const char* Value::DescribeType(Type t) {
101 std::string Value::ToString(bool quote_string) const {
106 return boolean_value_ ? "true" : "false";
108 return base::Int64ToString(int_value_);
111 return "\"" + string_value_ + "\"";
112 return string_value_;
114 std::string result = "[";
115 for (size_t i = 0; i < list_value_.size(); i++) {
118 result += list_value_[i].ToString(true);
120 result.push_back(']');
124 return std::string("<scope>");
126 return std::string();
129 bool Value::VerifyTypeIs(Type t, Err* err) const {
133 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + ".");
137 bool Value::operator==(const Value& other) const {
138 if (type_ != other.type_)
143 return boolean_value() == other.boolean_value();
145 return int_value() == other.int_value();
147 return string_value() == other.string_value();
149 if (list_value().size() != other.list_value().size())
151 for (size_t i = 0; i < list_value().size(); i++) {
152 if (list_value()[i] != other.list_value()[i])
157 // Its not clear what people mean when comparing scope values, so we test
158 // for scope identity and not contents equality.
159 return scope_value() == other.scope_value();
165 bool Value::operator!=(const Value& other) const {
166 return !operator==(other);