OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/value.h" | 5 #include "tools/gn/value.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "tools/gn/scope.h" |
8 | 9 |
9 Value::Value() | 10 Value::Value() |
10 : type_(NONE), | 11 : type_(NONE), |
11 boolean_value_(false), | 12 boolean_value_(false), |
12 int_value_(0), | 13 int_value_(0), |
13 scope_value_(NULL), | |
14 origin_(NULL) { | 14 origin_(NULL) { |
15 } | 15 } |
16 | 16 |
17 Value::Value(const ParseNode* origin, Type t) | 17 Value::Value(const ParseNode* origin, Type t) |
18 : type_(t), | 18 : type_(t), |
19 boolean_value_(false), | 19 boolean_value_(false), |
20 int_value_(0), | 20 int_value_(0), |
21 scope_value_(NULL), | |
22 origin_(origin) { | 21 origin_(origin) { |
23 } | 22 } |
24 | 23 |
25 Value::Value(const ParseNode* origin, bool bool_val) | 24 Value::Value(const ParseNode* origin, bool bool_val) |
26 : type_(BOOLEAN), | 25 : type_(BOOLEAN), |
27 boolean_value_(bool_val), | 26 boolean_value_(bool_val), |
28 int_value_(0), | 27 int_value_(0), |
29 scope_value_(NULL), | |
30 origin_(origin) { | 28 origin_(origin) { |
31 } | 29 } |
32 | 30 |
33 Value::Value(const ParseNode* origin, int64 int_val) | 31 Value::Value(const ParseNode* origin, int64 int_val) |
34 : type_(INTEGER), | 32 : type_(INTEGER), |
35 boolean_value_(false), | 33 boolean_value_(false), |
36 int_value_(int_val), | 34 int_value_(int_val), |
37 scope_value_(NULL), | |
38 origin_(origin) { | 35 origin_(origin) { |
39 } | 36 } |
40 | 37 |
41 Value::Value(const ParseNode* origin, std::string str_val) | 38 Value::Value(const ParseNode* origin, std::string str_val) |
42 : type_(STRING), | 39 : type_(STRING), |
43 string_value_(), | 40 string_value_(), |
44 boolean_value_(false), | 41 boolean_value_(false), |
45 int_value_(0), | 42 int_value_(0), |
46 scope_value_(NULL), | |
47 origin_(origin) { | 43 origin_(origin) { |
48 string_value_.swap(str_val); | 44 string_value_.swap(str_val); |
49 } | 45 } |
50 | 46 |
51 Value::Value(const ParseNode* origin, const char* str_val) | 47 Value::Value(const ParseNode* origin, const char* str_val) |
52 : type_(STRING), | 48 : type_(STRING), |
53 string_value_(str_val), | 49 string_value_(str_val), |
54 boolean_value_(false), | 50 boolean_value_(false), |
55 int_value_(0), | 51 int_value_(0), |
56 scope_value_(NULL), | |
57 origin_(origin) { | 52 origin_(origin) { |
58 } | 53 } |
59 | 54 |
60 Value::Value(const ParseNode* origin, Scope* scope) | 55 Value::Value(const ParseNode* origin, scoped_ptr<Scope> scope) |
61 : type_(SCOPE), | 56 : type_(SCOPE), |
62 string_value_(), | 57 string_value_(), |
63 boolean_value_(false), | 58 boolean_value_(false), |
64 int_value_(0), | 59 int_value_(0), |
65 scope_value_(scope), | 60 scope_value_(scope.Pass()), |
66 origin_(origin) { | 61 origin_(origin) { |
67 } | 62 } |
68 | 63 |
| 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(); |
| 73 } |
| 74 |
69 Value::~Value() { | 75 Value::~Value() { |
70 } | 76 } |
71 | 77 |
| 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; |
| 88 } |
| 89 |
72 void Value::RecursivelySetOrigin(const ParseNode* origin) { | 90 void Value::RecursivelySetOrigin(const ParseNode* origin) { |
73 set_origin(origin); | 91 set_origin(origin); |
74 if (type_ == Value::LIST) { | 92 if (type_ == Value::LIST) { |
75 for (size_t i = 0; i < list_value_.size(); i++) | 93 for (size_t i = 0; i < list_value_.size(); i++) |
76 list_value_[i].RecursivelySetOrigin(origin); | 94 list_value_[i].RecursivelySetOrigin(origin); |
77 } | 95 } |
78 } | 96 } |
79 | 97 |
80 // static | 98 // static |
81 const char* Value::DescribeType(Type t) { | 99 const char* Value::DescribeType(Type t) { |
82 switch (t) { | 100 switch (t) { |
83 case NONE: | 101 case NONE: |
84 return "none"; | 102 return "none"; |
85 case BOOLEAN: | 103 case BOOLEAN: |
86 return "boolean"; | 104 return "boolean"; |
87 case INTEGER: | 105 case INTEGER: |
88 return "integer"; | 106 return "integer"; |
89 case STRING: | 107 case STRING: |
90 return "string"; | 108 return "string"; |
91 case LIST: | 109 case LIST: |
92 return "list"; | 110 return "list"; |
93 case SCOPE: | 111 case SCOPE: |
94 return "scope"; | 112 return "scope"; |
95 default: | 113 default: |
96 NOTREACHED(); | 114 NOTREACHED(); |
97 return "UNKNOWN"; | 115 return "UNKNOWN"; |
98 } | 116 } |
99 } | 117 } |
100 | 118 |
| 119 void Value::SetScopeValue(scoped_ptr<Scope> scope) { |
| 120 DCHECK(type_ == SCOPE); |
| 121 scope_value_ = scope.Pass(); |
| 122 } |
| 123 |
101 std::string Value::ToString(bool quote_string) const { | 124 std::string Value::ToString(bool quote_string) const { |
102 switch (type_) { | 125 switch (type_) { |
103 case NONE: | 126 case NONE: |
104 return "<void>"; | 127 return "<void>"; |
105 case BOOLEAN: | 128 case BOOLEAN: |
106 return boolean_value_ ? "true" : "false"; | 129 return boolean_value_ ? "true" : "false"; |
107 case INTEGER: | 130 case INTEGER: |
108 return base::Int64ToString(int_value_); | 131 return base::Int64ToString(int_value_); |
109 case STRING: | 132 case STRING: |
110 if (quote_string) | 133 if (quote_string) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 return string_value() == other.string_value(); | 170 return string_value() == other.string_value(); |
148 case Value::LIST: | 171 case Value::LIST: |
149 if (list_value().size() != other.list_value().size()) | 172 if (list_value().size() != other.list_value().size()) |
150 return false; | 173 return false; |
151 for (size_t i = 0; i < list_value().size(); i++) { | 174 for (size_t i = 0; i < list_value().size(); i++) { |
152 if (list_value()[i] != other.list_value()[i]) | 175 if (list_value()[i] != other.list_value()[i]) |
153 return false; | 176 return false; |
154 } | 177 } |
155 return true; | 178 return true; |
156 case Value::SCOPE: | 179 case Value::SCOPE: |
157 // Its not clear what people mean when comparing scope values, so we test | 180 // Scopes are always considered not equal because there's currently |
158 // for scope identity and not contents equality. | 181 // no use case for comparing them, and it requires a bunch of complex |
159 return scope_value() == other.scope_value(); | 182 // iteration code. |
| 183 return false; |
160 default: | 184 default: |
161 return false; | 185 return false; |
162 } | 186 } |
163 } | 187 } |
164 | 188 |
165 bool Value::operator!=(const Value& other) const { | 189 bool Value::operator!=(const Value& other) const { |
166 return !operator==(other); | 190 return !operator==(other); |
167 } | 191 } |
OLD | NEW |