Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2177)

Unified Diff: tools/gn/value.cc

Issue 21114002: Add initial prototype for the GN meta-buildsystem. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add owners and readme Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/value.h ('k') | tools/gn/value_extractors.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/value.cc
diff --git a/tools/gn/value.cc b/tools/gn/value.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cb78faadcfda8bea054f7497d877bdfbf4451b1d
--- /dev/null
+++ b/tools/gn/value.cc
@@ -0,0 +1,126 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "tools/gn/value.h"
+
+#include "base/strings/string_number_conversions.h"
+
+Value::Value()
+ : type_(NONE),
+ int_value_(0),
+ origin_(NULL) {
+}
+
+Value::Value(const ParseNode* origin, Type t)
+ : type_(t),
+ int_value_(0),
+ origin_(origin) {
+}
+
+Value::Value(const ParseNode* origin, int64 int_val)
+ : type_(INTEGER),
+ int_value_(int_val),
+ origin_(origin) {
+}
+
+Value::Value(const ParseNode* origin, const base::StringPiece& str_val)
+ : type_(STRING),
+ string_value_(str_val.as_string()),
+ int_value_(0),
+ origin_(origin) {
+}
+
+Value::~Value() {
+}
+
+// static
+const char* Value::DescribeType(Type t) {
+ switch (t) {
+ case NONE:
+ return "none";
+ case INTEGER:
+ return "integer";
+ case STRING:
+ return "string";
+ case LIST:
+ return "list";
+ default:
+ NOTREACHED();
+ return "UNKNOWN";
+ }
+}
+
+int64 Value::InterpretAsInt() const {
+ switch (type_) {
+ case NONE:
+ return 0;
+ case INTEGER:
+ return int_value_;
+ case STRING:
+ return string_value_.empty() ? 0 : 1;
+ case LIST:
+ return list_value_.empty() ? 0 : 1;
+ }
+ return 0;
+}
+
+std::string Value::ToString() const {
+ switch (type_) {
+ case NONE:
+ return "<void>";
+ case INTEGER:
+ return base::Int64ToString(int_value_);
+ case STRING:
+ return string_value_;
+ case LIST: {
+ std::string result = "[";
+ for (size_t i = 0; i < list_value_.size(); i++) {
+ if (i > 0)
+ result += ", ";
+ // TODO(brettw) maybe also want to escape quotes in the string.
+ if (list_value_[i].type() == STRING)
+ result += std::string("\"") + list_value_[i].ToString() + "\"";
+ else
+ result += list_value_[i].ToString();
+ }
+ result.push_back(']');
+ return result;
+ }
+ }
+ return std::string();
+}
+
+bool Value::VerifyTypeIs(Type t, Err* err) const {
+ if (type_ == t)
+ return true;
+
+ *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + ".");
+ return false;
+}
+
+bool Value::operator==(const Value& other) const {
+ if (type_ != other.type_)
+ return false;
+
+ switch (type_) {
+ case Value::INTEGER:
+ return int_value() == other.int_value();
+ case Value::STRING:
+ return string_value() == other.string_value();
+ case Value::LIST:
+ if (list_value().size() != other.list_value().size())
+ return false;
+ for (size_t i = 0; i < list_value().size(); i++) {
+ if (list_value()[i] != other.list_value()[i])
+ return false;
+ }
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool Value::operator!=(const Value& other) const {
+ return !operator==(other);
+}
« no previous file with comments | « tools/gn/value.h ('k') | tools/gn/value_extractors.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698