Home | History | Annotate | Download | only in gn
      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     INTEGER,
     21     STRING,
     22     LIST
     23   };
     24 
     25   Value();
     26   Value(const ParseNode* origin, Type t);
     27   Value(const ParseNode* origin, int64 int_val);
     28   Value(const ParseNode* origin, const base::StringPiece& str_val);
     29   ~Value();
     30 
     31   Type type() const { return type_; }
     32 
     33   // Returns a string describing the given type.
     34   static const char* DescribeType(Type t);
     35 
     36   // Returns the node that made this. May be NULL.
     37   const ParseNode* origin() const { return origin_; }
     38   void set_origin(const ParseNode* o) { origin_ = o; }
     39 
     40   int64& int_value() {
     41     DCHECK(type_ == INTEGER);
     42     return int_value_;
     43   }
     44   const int64& int_value() const {
     45     DCHECK(type_ == INTEGER);
     46     return int_value_;
     47   }
     48 
     49   std::string& string_value() {
     50     DCHECK(type_ == STRING);
     51     return string_value_;
     52   }
     53   const std::string& string_value() const {
     54     DCHECK(type_ == STRING);
     55     return string_value_;
     56   }
     57 
     58   std::vector<Value>& list_value() {
     59     DCHECK(type_ == LIST);
     60     return list_value_;
     61   }
     62   const std::vector<Value>& list_value() const {
     63     DCHECK(type_ == LIST);
     64     return list_value_;
     65   }
     66 
     67   // Returns the current value converted to an int, normally used for
     68   // boolean operations. Undefined variables, empty lists, and empty strings
     69   // are all interpreted as 0, otherwise 1.
     70   int64 InterpretAsInt() const;
     71 
     72   // Converts the given value to a string.
     73   std::string ToString() const;
     74 
     75   // Verifies that the value is of the given type. If it isn't, returns
     76   // false and sets the error.
     77   bool VerifyTypeIs(Type t, Err* err) const;
     78 
     79   // Compares values. Only the "value" is compared, not the origin.
     80   bool operator==(const Value& other) const;
     81   bool operator!=(const Value& other) const;
     82 
     83  private:
     84   Type type_;
     85   std::string string_value_;
     86   int64 int_value_;
     87   std::vector<Value> list_value_;
     88   const ParseNode* origin_;
     89 };
     90 
     91 #endif  // TOOLS_GN_VALUE_H_
     92