Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / tools / gn / value.cc
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.
4
5 #include "tools/gn/value.h"
6
7 #include "base/strings/string_number_conversions.h"
8
9 Value::Value()
10     : type_(NONE),
11       boolean_value_(false),
12       int_value_(0),
13       scope_value_(NULL),
14       origin_(NULL) {
15 }
16
17 Value::Value(const ParseNode* origin, Type t)
18     : type_(t),
19       boolean_value_(false),
20       int_value_(0),
21       scope_value_(NULL),
22       origin_(origin) {
23 }
24
25 Value::Value(const ParseNode* origin, bool bool_val)
26     : type_(BOOLEAN),
27       boolean_value_(bool_val),
28       int_value_(0),
29       scope_value_(NULL),
30       origin_(origin) {
31 }
32
33 Value::Value(const ParseNode* origin, int64 int_val)
34     : type_(INTEGER),
35       boolean_value_(false),
36       int_value_(int_val),
37       scope_value_(NULL),
38       origin_(origin) {
39 }
40
41 Value::Value(const ParseNode* origin, std::string str_val)
42     : type_(STRING),
43       string_value_(),
44       boolean_value_(false),
45       int_value_(0),
46       scope_value_(NULL),
47       origin_(origin) {
48   string_value_.swap(str_val);
49 }
50
51 Value::Value(const ParseNode* origin, const char* str_val)
52     : type_(STRING),
53       string_value_(str_val),
54       boolean_value_(false),
55       int_value_(0),
56       scope_value_(NULL),
57       origin_(origin) {
58 }
59
60 Value::Value(const ParseNode* origin, Scope* scope)
61     : type_(SCOPE),
62       string_value_(),
63       boolean_value_(false),
64       int_value_(0),
65       scope_value_(scope),
66       origin_(origin) {
67 }
68
69 Value::~Value() {
70 }
71
72 void Value::RecursivelySetOrigin(const ParseNode* origin) {
73   set_origin(origin);
74   if (type_ == Value::LIST) {
75     for (size_t i = 0; i < list_value_.size(); i++)
76       list_value_[i].RecursivelySetOrigin(origin);
77   }
78 }
79
80 // static
81 const char* Value::DescribeType(Type t) {
82   switch (t) {
83     case NONE:
84       return "none";
85     case BOOLEAN:
86       return "boolean";
87     case INTEGER:
88       return "integer";
89     case STRING:
90       return "string";
91     case LIST:
92       return "list";
93     case SCOPE:
94       return "scope";
95     default:
96       NOTREACHED();
97       return "UNKNOWN";
98   }
99 }
100
101 std::string Value::ToString(bool quote_string) const {
102   switch (type_) {
103     case NONE:
104       return "<void>";
105     case BOOLEAN:
106       return boolean_value_ ? "true" : "false";
107     case INTEGER:
108       return base::Int64ToString(int_value_);
109     case STRING:
110       if (quote_string)
111         return "\"" + string_value_ + "\"";
112       return string_value_;
113     case LIST: {
114       std::string result = "[";
115       for (size_t i = 0; i < list_value_.size(); i++) {
116         if (i > 0)
117           result += ", ";
118         result += list_value_[i].ToString(true);
119       }
120       result.push_back(']');
121       return result;
122     }
123     case SCOPE:
124       return std::string("<scope>");
125   }
126   return std::string();
127 }
128
129 bool Value::VerifyTypeIs(Type t, Err* err) const {
130   if (type_ == t)
131     return true;
132
133   *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + ".");
134   return false;
135 }
136
137 bool Value::operator==(const Value& other) const {
138   if (type_ != other.type_)
139     return false;
140
141   switch (type_) {
142     case Value::BOOLEAN:
143       return boolean_value() == other.boolean_value();
144     case Value::INTEGER:
145       return int_value() == other.int_value();
146     case Value::STRING:
147       return string_value() == other.string_value();
148     case Value::LIST:
149       if (list_value().size() != other.list_value().size())
150         return false;
151       for (size_t i = 0; i < list_value().size(); i++) {
152         if (list_value()[i] != other.list_value()[i])
153           return false;
154       }
155       return true;
156     case Value::SCOPE:
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();
160     default:
161       return false;
162   }
163 }
164
165 bool Value::operator!=(const Value& other) const {
166   return !operator==(other);
167 }