Schnek
variables.hpp
1 /*
2  * variables.hpp
3  *
4  * Created on: 1 Jul 2011
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_VARIABLES_HPP_
28 #define SCHNEK_VARIABLES_HPP_
29 
30 #include "types.hpp"
31 #include "../exception.hpp"
32 #include "../util/unique.hpp"
33 
34 #include <list>
35 #include <map>
36 #include <string>
37 #include <memory>
38 
39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41 
42 #include <boost/variant.hpp>
43 
44 #pragma GCC diagnostic pop
45 
46 namespace schnek {
47 
48 class BlockVariables;
49 typedef std::shared_ptr<BlockVariables> pBlockVariables;
50 typedef std::list<pBlockVariables> BlockVariablesList;
51 
58 class Variable
59 {
60  public:
62  enum VariableTypeInfo {int_type, float_type, string_type};
63 
64  typedef ExpressionVariant ExpressionType;
65 
66  private:
68  ExpressionType expression;
70  ValueVariant var;
72  VariableTypeInfo type;
74  bool fixed;
76  bool initialised;
78  bool readonly;
80  std::shared_ptr< Unique<Variable> > uniqueId;
81  public:
83  Variable(int value, bool initialised_ = true, bool readonly_ = false);
85  Variable(double value, bool initialised_ = true, bool readonly_ = false);
87  Variable(std::string value, bool initialised_ = true, bool readonly_ = false);
89  Variable(pIntExpression expr, bool initialised_ = true, bool readonly_ = false);
91  Variable(pFloatExpression expr, bool initialised_ = true, bool readonly_ = false);
93  Variable(pStringExpression expr, bool initialised_ = true, bool readonly_ = false);
94 
98  Variable(const Variable &var);
100  Variable &operator=(const Variable &var);
101 
103  VariableTypeInfo getType() {return type;}
105  ValueVariant getValue() {return var;}
107  const ExpressionVariant &getExpression() {return expression;}
109  const ValueVariant &evaluateExpression();
110 
112  bool isConstant() {return fixed;}
113 
115  bool isReadOnly() {return readonly;}
116 
118  bool isInitialised() {return initialised;}
119 
120  /* Returns the id of the variable
121  *
122  * A unique id is created when a Variable is created with a value or an expression.
123  * When one Variable is copied to another, either by the copy constructor or the assignement operator,
124  * the id is copied as well.
125  *
126  * Thus, the id is not unique across all variables but is an id that identifies <strong>different</strong>
127  * variables.
128  */
129  long getId() { return uniqueId->getId(); }
130 
132  const ValueVariant &evaluate() { if (!isConstant()) evaluateExpression(); return var; }
133 };
134 
135 typedef std::shared_ptr<Variable> pVariable;
136 typedef std::list<pVariable> VariableList;
137 typedef std::map<std::string, pVariable> VariableMap;
138 
148 {
149  private:
151  std::string name;
153  std::string className;
155  pBlockVariables parent;
157  BlockVariablesList children;
158 
160  VariableMap vars;
161 
162  typedef std::map<std::string, pBlockVariables> BlockVariablesMap;
163  typedef std::map<std::string, BlockVariablesList> BlockVariablesListMap;
164  BlockVariablesMap childrenByName;
165  BlockVariablesListMap childrenByClassName;
166 
170  bool exists(std::list<std::string> path, bool upward);
171 
178  pVariable getVariable(std::list<std::string> path, bool upward);
179  public:
181  BlockVariables(std::string name_, std::string className_, pBlockVariables parent_)
182  : name(name_), className(className_), parent(parent_)
183  {}
184 
186  bool exists(std::string name);
187 
193  pVariable getVariable(std::string name);
194 
198  bool addVariable(std::string name, pVariable variable);
199 
203  bool addChild(pBlockVariables child);
204 
206  std::string getBlockName() { return name; }
208  std::string getClassName() { return className; }
210  pBlockVariables getParent() { return parent; }
212  const BlockVariablesList& getChildren() { return children; }
213 
215  const VariableMap& getVariables() { return vars; }
216 };
217 
223 {
224  private:
226  pBlockVariables root;
228  pBlockVariables cursor;
229  public:
230  VariableStorage() {}
232  VariableStorage(std::string name, std::string classname);
233  VariableStorage(const VariableStorage& storage)
234  : root(storage.root), cursor(storage.cursor) {}
235 
237  void resetCursor();
238 
242  void cursorUp();
243 
249  void addVariable(std::string name, pVariable variable);
250 
257  pBlockVariables createBlock(std::string name, std::string classname);
258 
260  pBlockVariables getRootBlock() { return root; }
262  pBlockVariables getCurrentBlock() { return cursor; }
263 };
264 
265 } // namespace
266 
267 #endif // SCHNEK_VARIABLES_HPP_
VariableTypeInfo getType()
returns the type of the variable
Definition: variables.hpp:103
const BlockVariablesList & getChildren()
returns the children of the block
Definition: variables.hpp:212
ValueVariant getValue()
returns the fixed value of the variable
Definition: variables.hpp:105
Variable & operator=(const Variable &var)
assignment operator
Definition: variables.cpp:106
const ExpressionVariant & getExpression()
returns the expression kept in the variable
Definition: variables.hpp:107
std::string getBlockName()
returns the block&#39;s name
Definition: variables.hpp:206
Variable(int value, bool initialised_=true, bool readonly_=false)
construct with an integer
Definition: variables.cpp:42
std::string getClassName()
returns the block&#39;s class name
Definition: variables.hpp:208
BlockVariables(std::string name_, std::string className_, pBlockVariables parent_)
Costruct using the block&#39;s name and class name.
Definition: variables.hpp:181
Definition: algo.hpp:30
bool isConstant()
returns true if the value of the variable is constant and does not depend on non-constant variables ...
Definition: variables.hpp:112
pBlockVariables getParent()
returns the parent (enclosing) block of this block
Definition: variables.hpp:210
VariableTypeInfo
Enum specifying which type the variable returns.
Definition: variables.hpp:62
const VariableMap & getVariables()
returns the variables stored in the block
Definition: variables.hpp:215
bool isInitialised()
returns true if the value of the variable has been initialised
Definition: variables.hpp:118
pBlockVariables getCurrentBlock()
Returns the current block given by the cursor.
Definition: variables.hpp:262
bool isReadOnly()
returns true if the value of the variable is a read only variable
Definition: variables.hpp:115
pBlockVariables getRootBlock()
Returns the root block.
Definition: variables.hpp:260
const ValueVariant & evaluateExpression()
evaluetes the expression kept in the variable and returns the value
Definition: variables.cpp:119
const ValueVariant & evaluate()
returns the value of the variable
Definition: variables.hpp:132
Definition: variables.hpp:147
Definition: variables.hpp:222
Definition: variables.hpp:58