Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / tools / gn / value.cc
blob1fb7155dc1b609d410f884d5f3163b7b016ca760
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"
8 #include "tools/gn/scope.h"
10 Value::Value()
11 : type_(NONE),
12 boolean_value_(false),
13 int_value_(0),
14 origin_(NULL) {
17 Value::Value(const ParseNode* origin, Type t)
18 : type_(t),
19 boolean_value_(false),
20 int_value_(0),
21 origin_(origin) {
24 Value::Value(const ParseNode* origin, bool bool_val)
25 : type_(BOOLEAN),
26 boolean_value_(bool_val),
27 int_value_(0),
28 origin_(origin) {
31 Value::Value(const ParseNode* origin, int64 int_val)
32 : type_(INTEGER),
33 boolean_value_(false),
34 int_value_(int_val),
35 origin_(origin) {
38 Value::Value(const ParseNode* origin, std::string str_val)
39 : type_(STRING),
40 string_value_(),
41 boolean_value_(false),
42 int_value_(0),
43 origin_(origin) {
44 string_value_.swap(str_val);
47 Value::Value(const ParseNode* origin, const char* str_val)
48 : type_(STRING),
49 string_value_(str_val),
50 boolean_value_(false),
51 int_value_(0),
52 origin_(origin) {
55 Value::Value(const ParseNode* origin, scoped_ptr<Scope> scope)
56 : type_(SCOPE),
57 string_value_(),
58 boolean_value_(false),
59 int_value_(0),
60 scope_value_(scope.Pass()),
61 origin_(origin) {
64 Value::Value(const Value& other)
65 : type_(other.type_),
66 string_value_(other.string_value_),
67 boolean_value_(other.boolean_value_),
68 int_value_(other.int_value_),
69 list_value_(other.list_value_),
70 origin_(other.origin_) {
71 if (type() == SCOPE && other.scope_value_.get())
72 scope_value_ = other.scope_value_->MakeClosure();
75 Value::~Value() {
78 Value& Value::operator=(const Value& other) {
79 type_ = other.type_;
80 string_value_ = other.string_value_;
81 boolean_value_ = other.boolean_value_;
82 int_value_ = other.int_value_;
83 list_value_ = other.list_value_;
84 if (type() == SCOPE && other.scope_value_.get())
85 scope_value_ = other.scope_value_->MakeClosure();
86 origin_ = other.origin_;
87 return *this;
90 void Value::RecursivelySetOrigin(const ParseNode* origin) {
91 set_origin(origin);
92 if (type_ == Value::LIST) {
93 for (size_t i = 0; i < list_value_.size(); i++)
94 list_value_[i].RecursivelySetOrigin(origin);
98 // static
99 const char* Value::DescribeType(Type t) {
100 switch (t) {
101 case NONE:
102 return "none";
103 case BOOLEAN:
104 return "boolean";
105 case INTEGER:
106 return "integer";
107 case STRING:
108 return "string";
109 case LIST:
110 return "list";
111 case SCOPE:
112 return "scope";
113 default:
114 NOTREACHED();
115 return "UNKNOWN";
119 void Value::SetScopeValue(scoped_ptr<Scope> scope) {
120 DCHECK(type_ == SCOPE);
121 scope_value_ = scope.Pass();
124 std::string Value::ToString(bool quote_string) const {
125 switch (type_) {
126 case NONE:
127 return "<void>";
128 case BOOLEAN:
129 return boolean_value_ ? "true" : "false";
130 case INTEGER:
131 return base::Int64ToString(int_value_);
132 case STRING:
133 if (quote_string)
134 return "\"" + string_value_ + "\"";
135 return string_value_;
136 case LIST: {
137 std::string result = "[";
138 for (size_t i = 0; i < list_value_.size(); i++) {
139 if (i > 0)
140 result += ", ";
141 result += list_value_[i].ToString(true);
143 result.push_back(']');
144 return result;
146 case SCOPE:
147 return std::string("<scope>");
149 return std::string();
152 bool Value::VerifyTypeIs(Type t, Err* err) const {
153 if (type_ == t)
154 return true;
156 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + ".");
157 return false;
160 bool Value::operator==(const Value& other) const {
161 if (type_ != other.type_)
162 return false;
164 switch (type_) {
165 case Value::BOOLEAN:
166 return boolean_value() == other.boolean_value();
167 case Value::INTEGER:
168 return int_value() == other.int_value();
169 case Value::STRING:
170 return string_value() == other.string_value();
171 case Value::LIST:
172 if (list_value().size() != other.list_value().size())
173 return false;
174 for (size_t i = 0; i < list_value().size(); i++) {
175 if (list_value()[i] != other.list_value()[i])
176 return false;
178 return true;
179 case Value::SCOPE:
180 // Scopes are always considered not equal because there's currently
181 // no use case for comparing them, and it requires a bunch of complex
182 // iteration code.
183 return false;
184 default:
185 return false;
189 bool Value::operator!=(const Value& other) const {
190 return !operator==(other);