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.
5 #ifndef TOOLS_GN_VALUE_H_
6 #define TOOLS_GN_VALUE_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/strings/string_piece.h"
11 #include "tools/gn/err.h"
15 // Represents a variable value in the interpreter.
27 Value(const ParseNode
* origin
, Type t
);
28 Value(const ParseNode
* origin
, bool bool_val
);
29 Value(const ParseNode
* origin
, int64 int_val
);
30 Value(const ParseNode
* origin
, std::string str_val
);
31 Value(const ParseNode
* origin
, const char* str_val
);
34 Type
type() const { return type_
; }
36 // Returns a string describing the given type.
37 static const char* DescribeType(Type t
);
39 // Returns the node that made this. May be NULL.
40 const ParseNode
* origin() const { return origin_
; }
41 void set_origin(const ParseNode
* o
) { origin_
= o
; }
43 bool& boolean_value() {
44 DCHECK(type_
== BOOLEAN
);
45 return boolean_value_
;
47 const bool& boolean_value() const {
48 DCHECK(type_
== BOOLEAN
);
49 return boolean_value_
;
53 DCHECK(type_
== INTEGER
);
56 const int64
& int_value() const {
57 DCHECK(type_
== INTEGER
);
61 std::string
& string_value() {
62 DCHECK(type_
== STRING
);
65 const std::string
& string_value() const {
66 DCHECK(type_
== STRING
);
70 std::vector
<Value
>& list_value() {
71 DCHECK(type_
== LIST
);
74 const std::vector
<Value
>& list_value() const {
75 DCHECK(type_
== LIST
);
79 // Converts the given value to a string. Returns true if strings should be
80 // quoted or the ToString of a string should be the string itself.
81 std::string
ToString(bool quote_strings
) const;
83 // Verifies that the value is of the given type. If it isn't, returns
84 // false and sets the error.
85 bool VerifyTypeIs(Type t
, Err
* err
) const;
87 // Compares values. Only the "value" is compared, not the origin.
88 bool operator==(const Value
& other
) const;
89 bool operator!=(const Value
& other
) const;
93 std::string string_value_
;
96 std::vector
<Value
> list_value_
;
97 const ParseNode
* origin_
;
100 #endif // TOOLS_GN_VALUE_H_