- add sources.
[platform/framework/web/crosswalk.git] / src / tools / gn / value.h
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 #ifndef TOOLS_GN_VALUE_H_
6 #define TOOLS_GN_VALUE_H_
7
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "base/strings/string_piece.h"
11 #include "tools/gn/err.h"
12
13 class ParseNode;
14
15 // Represents a variable value in the interpreter.
16 class Value {
17  public:
18   enum Type {
19     NONE = 0,
20     BOOLEAN,
21     INTEGER,
22     STRING,
23     LIST
24   };
25
26   Value();
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);
32   ~Value();
33
34   Type type() const { return type_; }
35
36   // Returns a string describing the given type.
37   static const char* DescribeType(Type t);
38
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; }
42
43   bool& boolean_value() {
44     DCHECK(type_ == BOOLEAN);
45     return boolean_value_;
46   }
47   const bool& boolean_value() const {
48     DCHECK(type_ == BOOLEAN);
49     return boolean_value_;
50   }
51
52   int64& int_value() {
53     DCHECK(type_ == INTEGER);
54     return int_value_;
55   }
56   const int64& int_value() const {
57     DCHECK(type_ == INTEGER);
58     return int_value_;
59   }
60
61   std::string& string_value() {
62     DCHECK(type_ == STRING);
63     return string_value_;
64   }
65   const std::string& string_value() const {
66     DCHECK(type_ == STRING);
67     return string_value_;
68   }
69
70   std::vector<Value>& list_value() {
71     DCHECK(type_ == LIST);
72     return list_value_;
73   }
74   const std::vector<Value>& list_value() const {
75     DCHECK(type_ == LIST);
76     return list_value_;
77   }
78
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;
82
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;
86
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;
90
91  private:
92   Type type_;
93   std::string string_value_;
94   bool boolean_value_;
95   int64 int_value_;
96   std::vector<Value> list_value_;
97   const ParseNode* origin_;
98 };
99
100 #endif  // TOOLS_GN_VALUE_H_