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