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 <boost/shared_ptr.hpp>
38 #include <boost/variant.hpp>
39 
40 
41 namespace schnek {
42 
43 class BlockVariables;
44 typedef boost::shared_ptr<BlockVariables> pBlockVariables;
45 typedef std::list<pBlockVariables> BlockVariablesList;
46 
53 class Variable
54 {
55  public:
57  enum VariableTypeInfo {int_type, float_type, string_type};
58 
59  typedef ExpressionVariant ExpressionType;
60 
61  private:
63  ExpressionType expression;
65  ValueVariant var;
67  VariableTypeInfo type;
69  bool fixed;
71  bool initialised;
73  bool readonly;
75  boost::shared_ptr< Unique<Variable> > uniqueId;
76  public:
78  Variable(int value, bool initialised_ = true, bool readonly_ = false);
80  Variable(double value, bool initialised_ = true, bool readonly_ = false);
82  Variable(std::string value, bool initialised_ = true, bool readonly_ = false);
84  Variable(pIntExpression expr, bool initialised_ = true, bool readonly_ = false);
86  Variable(pFloatExpression expr, bool initialised_ = true, bool readonly_ = false);
88  Variable(pStringExpression expr, bool initialised_ = true, bool readonly_ = false);
89 
93  Variable(const Variable &var);
95  Variable &operator=(const Variable &var);
96 
98  VariableTypeInfo getType() {return type;}
100  ValueVariant getValue() {return var;}
102  const ExpressionVariant &getExpression() {return expression;}
104  const ValueVariant &evaluateExpression();
105 
107  bool isConstant() {return fixed;}
108 
110  bool isReadOnly() {return readonly;}
111 
113  bool isInitialised() {return initialised;}
114 
115  /* Returns the id of the variable
116  *
117  * A unique id is created when a Variable is created with a value or an expression.
118  * When one Variable is copied to another, either by the copy constructor or the assignement operator,
119  * the id is copied as well.
120  *
121  * Thus, the id is not unique across all variables but is an id that identifies <strong>different</strong>
122  * variables.
123  */
124  long getId() { return uniqueId->getId(); }
125 
127  const ValueVariant &evaluate() { if (!isConstant()) evaluateExpression(); return var; }
128 };
129 
130 typedef boost::shared_ptr<Variable> pVariable;
131 typedef std::list<pVariable> VariableList;
132 typedef std::map<std::string, pVariable> VariableMap;
133 
143 {
144  private:
146  std::string name;
148  std::string className;
150  pBlockVariables parent;
152  BlockVariablesList children;
153 
155  VariableMap vars;
156 
157  typedef std::map<std::string, pBlockVariables> BlockVariablesMap;
158  typedef std::map<std::string, BlockVariablesList> BlockVariablesListMap;
159  BlockVariablesMap childrenByName;
160  BlockVariablesListMap childrenByClassName;
161 
165  bool exists(std::list<std::string> path, bool upward);
166 
173  pVariable getVariable(std::list<std::string> path, bool upward);
174  public:
176  BlockVariables(std::string name_, std::string className_, pBlockVariables parent_)
177  : name(name_), className(className_), parent(parent_)
178  {}
179 
181  bool exists(std::string name);
182 
188  pVariable getVariable(std::string name);
189 
193  bool addVariable(std::string name, pVariable variable);
194 
198  bool addChild(pBlockVariables child);
199 
201  std::string getBlockName() { return name; }
203  std::string getClassName() { return className; }
205  pBlockVariables getParent() { return parent; }
207  const BlockVariablesList& getChildren() { return children; }
208 
210  const VariableMap& getVariables() { return vars; }
211 };
212 
218 {
219  private:
221  pBlockVariables root;
223  pBlockVariables cursor;
224  public:
225  VariableStorage() {}
227  VariableStorage(std::string name, std::string classname);
228  VariableStorage(const VariableStorage& storage)
229  : root(storage.root), cursor(storage.cursor) {}
230 
232  void resetCursor();
233 
237  void cursorUp();
238 
244  void addVariable(std::string name, pVariable variable);
245 
252  pBlockVariables createBlock(std::string name, std::string classname);
253 
255  pBlockVariables getRootBlock() { return root; }
257  pBlockVariables getCurrentBlock() { return cursor; }
258 };
259 
260 } // namespace
261 
262 #endif // SCHNEK_VARIABLES_HPP_
VariableTypeInfo getType()
returns the type of the variable
Definition: variables.hpp:98
const BlockVariablesList & getChildren()
returns the children of the block
Definition: variables.hpp:207
ValueVariant getValue()
returns the fixed value of the variable
Definition: variables.hpp:100
Variable & operator=(const Variable &var)
assignment operator
Definition: variables.cpp:107
const ExpressionVariant & getExpression()
returns the expression kept in the variable
Definition: variables.hpp:102
std::string getBlockName()
returns the block&#39;s name
Definition: variables.hpp:201
Variable(int value, bool initialised_=true, bool readonly_=false)
construct with an integer
Definition: variables.cpp:43
std::string getClassName()
returns the block&#39;s class name
Definition: variables.hpp:203
BlockVariables(std::string name_, std::string className_, pBlockVariables parent_)
Costruct using the block&#39;s name and class name.
Definition: variables.hpp:176
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:107
pBlockVariables getParent()
returns the parent (enclosing) block of this block
Definition: variables.hpp:205
VariableTypeInfo
Enum specifying which type the variable returns.
Definition: variables.hpp:57
const VariableMap & getVariables()
returns the variables stored in the block
Definition: variables.hpp:210
bool isInitialised()
returns true if the value of the variable has been initialised
Definition: variables.hpp:113
pBlockVariables getCurrentBlock()
Returns the current block given by the cursor.
Definition: variables.hpp:257
bool isReadOnly()
returns true if the value of the variable is a read only variable
Definition: variables.hpp:110
pBlockVariables getRootBlock()
Returns the root block.
Definition: variables.hpp:255
const ValueVariant & evaluateExpression()
evaluetes the expression kept in the variable and returns the value
Definition: variables.cpp:120
const ValueVariant & evaluate()
returns the value of the variable
Definition: variables.hpp:127
Definition: variables.hpp:142
Definition: variables.hpp:217
Definition: variables.hpp:53