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