Upstream version 7.36.149.0
[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 <map>
9
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_piece.h"
14 #include "tools/gn/err.h"
15
16 class ParseNode;
17 class Scope;
18
19 // Represents a variable value in the interpreter.
20 class Value {
21  public:
22   enum Type {
23     NONE = 0,
24     BOOLEAN,
25     INTEGER,
26     STRING,
27     LIST,
28     SCOPE,
29   };
30
31   Value();
32   Value(const ParseNode* origin, Type t);
33   Value(const ParseNode* origin, bool bool_val);
34   Value(const ParseNode* origin, int64 int_val);
35   Value(const ParseNode* origin, std::string str_val);
36   Value(const ParseNode* origin, const char* str_val);
37   // Values "shouldn't" have null scopes when type == Scope, so be sure to
38   // always set one. However, this is not asserted since there are some
39   // use-cases for creating values and immediately setting the scope on it. So
40   // you can pass a null scope here if you promise to set it before any other
41   // code gets it (code will generally assume the scope is not null).
42   Value(const ParseNode* origin, scoped_ptr<Scope> scope);
43
44   Value(const Value& other);
45   ~Value();
46
47   Value& operator=(const Value& other);
48
49   Type type() const { return type_; }
50
51   // Returns a string describing the given type.
52   static const char* DescribeType(Type t);
53
54   // Returns the node that made this. May be NULL.
55   const ParseNode* origin() const { return origin_; }
56   void set_origin(const ParseNode* o) { origin_ = o; }
57
58   // Sets the origin of this value, recursively going into list child
59   // values and also setting their origins.
60   void RecursivelySetOrigin(const ParseNode* o);
61
62   bool& boolean_value() {
63     DCHECK(type_ == BOOLEAN);
64     return boolean_value_;
65   }
66   const bool& boolean_value() const {
67     DCHECK(type_ == BOOLEAN);
68     return boolean_value_;
69   }
70
71   int64& int_value() {
72     DCHECK(type_ == INTEGER);
73     return int_value_;
74   }
75   const int64& int_value() const {
76     DCHECK(type_ == INTEGER);
77     return int_value_;
78   }
79
80   std::string& string_value() {
81     DCHECK(type_ == STRING);
82     return string_value_;
83   }
84   const std::string& string_value() const {
85     DCHECK(type_ == STRING);
86     return string_value_;
87   }
88
89   std::vector<Value>& list_value() {
90     DCHECK(type_ == LIST);
91     return list_value_;
92   }
93   const std::vector<Value>& list_value() const {
94     DCHECK(type_ == LIST);
95     return list_value_;
96   }
97
98   Scope* scope_value() {
99     DCHECK(type_ == SCOPE);
100     return scope_value_.get();
101   }
102   const Scope* scope_value() const {
103     DCHECK(type_ == SCOPE);
104     return scope_value_.get();
105   }
106   void SetScopeValue(scoped_ptr<Scope> scope);
107
108   // Converts the given value to a string. Returns true if strings should be
109   // quoted or the ToString of a string should be the string itself. If the
110   // string is quoted, it will also enable escaping.
111   std::string ToString(bool quote_strings) const;
112
113   // Verifies that the value is of the given type. If it isn't, returns
114   // false and sets the error.
115   bool VerifyTypeIs(Type t, Err* err) const;
116
117   // Compares values. Only the "value" is compared, not the origin.
118   bool operator==(const Value& other) const;
119   bool operator!=(const Value& other) const;
120
121  private:
122   // This are a lot of objects associated with every Value that need
123   // initialization and tear down every time. It might be more efficient to
124   // create a union of ManualConstructor objects (see SmallMap) and only
125   // use the one we care about.
126   Type type_;
127   std::string string_value_;
128   bool boolean_value_;
129   int64 int_value_;
130   std::vector<Value> list_value_;
131   scoped_ptr<Scope> scope_value_;
132
133   const ParseNode* origin_;
134 };
135
136 #endif  // TOOLS_GN_VALUE_H_