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),
16 Value::Value(const ParseNode* origin, Type t)
18 boolean_value_(false),
23 Value::Value(const ParseNode* origin, bool bool_val)
25 boolean_value_(bool_val),
30 Value::Value(const ParseNode* origin, int64 int_val)
32 boolean_value_(false),
37 Value::Value(const ParseNode* origin, std::string str_val)
40 boolean_value_(false),
43 string_value_.swap(str_val);
46 Value::Value(const ParseNode* origin, const char* str_val)
48 string_value_(str_val),
49 boolean_value_(false),
57 void Value::RecursivelySetOrigin(const ParseNode* origin) {
59 if (type_ == Value::LIST) {
60 for (size_t i = 0; i < list_value_.size(); i++)
61 list_value_[i].RecursivelySetOrigin(origin);
66 const char* Value::DescribeType(Type t) {
84 std::string Value::ToString(bool quote_string) const {
89 return boolean_value_ ? "true" : "false";
91 return base::Int64ToString(int_value_);
94 return "\"" + string_value_ + "\"";
97 std::string result = "[";
98 for (size_t i = 0; i < list_value_.size(); i++) {
101 result += list_value_[i].ToString(true);
103 result.push_back(']');
107 return std::string();
110 bool Value::VerifyTypeIs(Type t, Err* err) const {
114 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + ".");
118 bool Value::operator==(const Value& other) const {
119 if (type_ != other.type_)
124 return boolean_value() == other.boolean_value();
126 return int_value() == other.int_value();
128 return string_value() == other.string_value();
130 if (list_value().size() != other.list_value().size())
132 for (size_t i = 0; i < list_value().size(); i++) {
133 if (list_value()[i] != other.list_value()[i])
142 bool Value::operator!=(const Value& other) const {
143 return !operator==(other);