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"
12 boolean_value_(false),
17 Value::Value(const ParseNode
* origin
, Type t
)
19 boolean_value_(false),
24 Value::Value(const ParseNode
* origin
, bool bool_val
)
26 boolean_value_(bool_val
),
31 Value::Value(const ParseNode
* origin
, int64 int_val
)
33 boolean_value_(false),
38 Value::Value(const ParseNode
* origin
, std::string str_val
)
41 boolean_value_(false),
44 string_value_
.swap(str_val
);
47 Value::Value(const ParseNode
* origin
, const char* str_val
)
49 string_value_(str_val
),
50 boolean_value_(false),
55 Value::Value(const ParseNode
* origin
, scoped_ptr
<Scope
> scope
)
58 boolean_value_(false),
60 scope_value_(scope
.Pass()),
64 Value::Value(const Value
& other
)
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();
78 Value
& Value::operator=(const Value
& other
) {
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_
;
90 void Value::RecursivelySetOrigin(const ParseNode
* origin
) {
92 if (type_
== Value::LIST
) {
93 for (size_t i
= 0; i
< list_value_
.size(); i
++)
94 list_value_
[i
].RecursivelySetOrigin(origin
);
99 const char* Value::DescribeType(Type t
) {
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 {
129 return boolean_value_
? "true" : "false";
131 return base::Int64ToString(int_value_
);
134 return "\"" + string_value_
+ "\"";
135 return string_value_
;
137 std::string result
= "[";
138 for (size_t i
= 0; i
< list_value_
.size(); i
++) {
141 result
+= list_value_
[i
].ToString(true);
143 result
.push_back(']');
147 return std::string("<scope>");
149 return std::string();
152 bool Value::VerifyTypeIs(Type t
, Err
* err
) const {
156 *err
= Err(origin(), std::string("This is not a ") + DescribeType(t
) + ".");
160 bool Value::operator==(const Value
& other
) const {
161 if (type_
!= other
.type_
)
166 return boolean_value() == other
.boolean_value();
168 return int_value() == other
.int_value();
170 return string_value() == other
.string_value();
172 if (list_value().size() != other
.list_value().size())
174 for (size_t i
= 0; i
< list_value().size(); i
++) {
175 if (list_value()[i
] != other
.list_value()[i
])
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
189 bool Value::operator!=(const Value
& other
) const {
190 return !operator==(other
);