| OLD | NEW |
| (Empty) |
| 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 int_value_(0), | |
| 12 origin_(NULL) { | |
| 13 } | |
| 14 | |
| 15 Value::Value(const ParseNode* origin, Type t) | |
| 16 : type_(t), | |
| 17 int_value_(0), | |
| 18 origin_(origin) { | |
| 19 } | |
| 20 | |
| 21 Value::Value(const ParseNode* origin, int64 int_val) | |
| 22 : type_(INTEGER), | |
| 23 int_value_(int_val), | |
| 24 origin_(origin) { | |
| 25 } | |
| 26 | |
| 27 Value::Value(const ParseNode* origin, const base::StringPiece& str_val) | |
| 28 : type_(STRING), | |
| 29 string_value_(str_val.as_string()), | |
| 30 int_value_(0), | |
| 31 origin_(origin) { | |
| 32 } | |
| 33 | |
| 34 Value::~Value() { | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 const char* Value::DescribeType(Type t) { | |
| 39 switch (t) { | |
| 40 case NONE: | |
| 41 return "none"; | |
| 42 case INTEGER: | |
| 43 return "integer"; | |
| 44 case STRING: | |
| 45 return "string"; | |
| 46 case LIST: | |
| 47 return "list"; | |
| 48 default: | |
| 49 NOTREACHED(); | |
| 50 return "UNKNOWN"; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 int64 Value::InterpretAsInt() const { | |
| 55 switch (type_) { | |
| 56 case NONE: | |
| 57 return 0; | |
| 58 case INTEGER: | |
| 59 return int_value_; | |
| 60 case STRING: | |
| 61 return string_value_.empty() ? 0 : 1; | |
| 62 case LIST: | |
| 63 return list_value_.empty() ? 0 : 1; | |
| 64 } | |
| 65 return 0; | |
| 66 } | |
| 67 | |
| 68 std::string Value::ToString() const { | |
| 69 switch (type_) { | |
| 70 case NONE: | |
| 71 return "<void>"; | |
| 72 case INTEGER: | |
| 73 return base::Int64ToString(int_value_); | |
| 74 case STRING: | |
| 75 return string_value_; | |
| 76 case LIST: { | |
| 77 std::string result = "["; | |
| 78 for (size_t i = 0; i < list_value_.size(); i++) { | |
| 79 if (i > 0) | |
| 80 result += ", "; | |
| 81 // TODO(brettw) maybe also want to escape quotes in the string. | |
| 82 if (list_value_[i].type() == STRING) | |
| 83 result += std::string("\"") + list_value_[i].ToString() + "\""; | |
| 84 else | |
| 85 result += list_value_[i].ToString(); | |
| 86 } | |
| 87 result.push_back(']'); | |
| 88 return result; | |
| 89 } | |
| 90 } | |
| 91 return std::string(); | |
| 92 } | |
| 93 | |
| 94 bool Value::VerifyTypeIs(Type t, Err* err) const { | |
| 95 if (type_ == t) | |
| 96 return true; | |
| 97 | |
| 98 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + "."); | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 bool Value::operator==(const Value& other) const { | |
| 103 if (type_ != other.type_) | |
| 104 return false; | |
| 105 | |
| 106 switch (type_) { | |
| 107 case Value::INTEGER: | |
| 108 return int_value() == other.int_value(); | |
| 109 case Value::STRING: | |
| 110 return string_value() == other.string_value(); | |
| 111 case Value::LIST: | |
| 112 if (list_value().size() != other.list_value().size()) | |
| 113 return false; | |
| 114 for (size_t i = 0; i < list_value().size(); i++) { | |
| 115 if (list_value()[i] != other.list_value()[i]) | |
| 116 return false; | |
| 117 } | |
| 118 return true; | |
| 119 default: | |
| 120 return false; | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 bool Value::operator!=(const Value& other) const { | |
| 125 return !operator==(other); | |
| 126 } | |
| OLD | NEW |