DiaDes  0.1
DIAgnosis of Discrete-Event System
Value.hh
Go to the documentation of this file.
1 #ifndef __DIADES__ALTARICA__VALUE__HH
2 #define __DIADES__ALTARICA__VALUE__HH
3 
7 #include<string>
8 #include<unordered_map>
9 #include<limits>
10 #include<vector>
11 #include<set>
12 #include<utils/Exceptions.hh>
13 #include<utils/Assertion.hh>
14 
15 #include"Type.hh"
16 
17 
18 using namespace std;
19 
20 namespace Diades
21 {
22  namespace Altarica
23  {
24 
29  typedef vector<int>::size_type ValueCode;
30 
31 
32  class Value;
33  class ValueFactory;
34 
39  typedef enum{Eq,Neq,Gt,Geq,Lt,Leq} ConstraintOperator;
40 
44  typedef map<Identifier,Value> StructureValueFields;
45 
46 
50  typedef vector<Value> ArrayValueFields;
51 
56  class ValueData
57  {
58  private:
60  ValueCode _code;
61  union
62  {
63  bool _boolean;
64  int _integer;
66  StructureValueFields * _structurePointer;
67  ArrayValueFields * _arrayPointer;
68  } _value;
69  ValueData():_type(),_code(0),_value() { _value._boolean = false; }
70  ~ValueData();
71 
72  friend class ValueFactory;
73  friend class Value;
74  friend ostream & operator<<(ostream & os, const Value & value);
75  };
76 
77 
78 
79 
80 
85  class Value
86  {
87  public:
89  static string typeName() { return "Altarica::Value"; }
90 
91  private:
93  Value(ValueData * data):_data(data){}
94  bool isEqualStructure(const Value & value) const;
95  bool isLessStructure(const Value & value) const;
96  bool isEqualArray(const Value & value) const;
97  bool isLessArray(const Value & value) const;
98 
99 
100  public:
104  Value():_data(0){}
105 
109  Value(const ArrayValueFields & arrayValueFields);
113  Value(const StructureValueFields & structureValueFields);
117  Value(const Value & value):_data(value._data){}
121  Value & operator=(const Value & value) {
122  if(this != &value)
123  {
124  _data = value._data;
125  }
126  return *this;
127  }
131  bool valid() const {
132  return _data!=0 && _data->_type.valid()
133  && (!_data->_type.isIdentifier() || _data->_value._identifierPointer != 0)
134  && (!_data->_type.isArray() || _data->_value._arrayPointer != 0)
135  && (!_data->_type.isStructure() || _data->_value._structurePointer != 0);
136  }
141  int integer() const
142  {
143  require(Exception, valid(),"integer(): invalid value");
144  require(Exception, type().isInteger(), "integer(): this value is not an integer");
145  if(valid() && type().isInteger())
146  {
147  return _data->_value._integer;
148  }
149  return 0;
150  }
155  bool boolean() const
156  {
157  require(Exception, valid(),"boolean(): invalid value");
158  require(Exception, type().isBoolean(), "boolean(): this value is not a boolean");
159  if(valid() && type().isBoolean())
160  {
161  return _data->_value._boolean;
162  }
163  return false;
164  }
165 
171  {
172  require(Exception, valid(),"identifier(): invalid value");
173  require(Exception, type().isIdentifier(), "identifier(): this value is not an indentifier");
174  if(valid() && type().isIdentifier())
175  {
176  return *(_data->_value._identifierPointer);
177  }
178  return "";
179  }
180 
185  unsigned size() const;
186 
192  Value getField(unsigned index) const;
193 
194 
200  Value getField(const Identifier & fieldName) const;
201 
205  Type type() const
206  {
207  if(valid())
208  {
209  return _data->_type;
210  }
211  return Type();
212  }
213 
218  int code() const
219  {
220  require(Exception,valid(),"code(): invalid value");
221  if(valid())
222  {
223  return _data->_code;
224  }
225  return -1;
226  }
227 
231  string textify() const;
232 
243  bool operator==(const Value & value) const;
244 
248  bool operator!=(const Value & value) const { return !(*this == value); }
252  bool operator<(const Value & value) const;
256  bool operator>(const Value & value) const { return (value < *this); }
260  bool operator<=(const Value & value) const { return (*this < value) || (*this == value); }
264  bool operator>=(const Value & value) const { return (*this > value) || (*this == value); }
265 
272  bool constraint(ConstraintOperator op, const Value & value) const
273  {
274  switch(op)
275  {
276  case Eq:
277  {
278  return (*this) == value;
279  }
280 
281  case Neq:
282  {
283  return (*this) != value;
284  }
285  case Gt:
286  {
287  return (*this) > value;
288  }
289  case Geq:
290  {
291  return (*this) >= value;
292  }
293  case Lt:
294  {
295  return (*this) < value;
296  }
297  case Leq:
298  {
299  return (*this) <= value;
300  }
301  default:
302  {}
303  }
304  return false;
305  }
306 
307 
308  friend ostream & operator<<(ostream & os, const Value & value);
309  friend class ValueFactory;
310  };
311 
316  typedef set<Value> ValueSet;
317 
318  /****************************************************************************/
319 
325  {
326  public:
328  static string typeName() { return "Altarica::ValueFactory"; }
329 
330  public:
331  static ValueFactory * factory();
339  Value getValue(const Identifier & value);
344  Value getValue(bool value);
349  Value getValue(int value);
354  Value getValue(const ArrayValueFields & arrayValueFields);
359  Value getValue(const StructureValueFields & structureValueFields);
360  ~ValueFactory();
361 
362  private:
366  unordered_map<Identifier,Value> _identifierMapping;
367  unordered_map<int,Value> _integerMapping;
368  vector<Value> _identifierValues;
369  vector<Value> _structuredValues;
370  vector<Value> _arrayedValues;
371  ValueFactory();
372 
373  friend class AltaricaModel;
374  friend class Value;
375  };
376 
377  /****************************************************************************/
378 
384  {
385  Identifier operator()(const Value & value)
386  {
387  return value.identifier();
388  }
389  };
390 
391 
397  {
398  int operator()(const Value & value)
399  {
400  return value.integer();
401  }
402  };
403 
409  {
410  bool operator()(const Value & value)
411  {
412  return value.boolean();
413  }
414  };
415 
416 
424  Value getValue(const Identifier & value);
425 
426 
431  Value getValue(bool value);
436  Value getValue(int value);
441  Value getValue(const ArrayValueFields & arrayValueFields);
446  Value getValue(const StructureValueFields & structureValueFields);
447 
448 
449 
450 
451 
452 
453  };
454 };
455 
456 
457 
458 
459 
460 
461 
462 
463 #endif
static ValueFactory * _valueFactory
Definition: Value.hh:363
bool operator<=(const Value &value) const
Definition: Value.hh:260
bool valid() const
Definition: Value.hh:131
bool constraint(ConstraintOperator op, const Value &value) const
Definition: Value.hh:272
vector< Value > _structuredValues
Definition: Value.hh:369
vector< Value > _arrayedValues
Definition: Value.hh:370
vector< Value > ArrayValueFields
Definition: Value.hh:50
vector< int >::size_type ValueCode
Definition: Value.hh:29
unordered_map< Identifier, Value > _identifierMapping
Definition: Value.hh:366
STL namespace.
Identifier * _identifierPointer
Definition: Value.hh:65
Utils::Exception< ValueFactory > Exception
Definition: Value.hh:327
bool operator>=(const Value &value) const
Definition: Value.hh:264
bool operator>(const Value &value) const
Definition: Value.hh:256
static string typeName()
Definition: Value.hh:328
union Diades::Altarica::ValueData::@5 _value
unordered_map< int, Value > _integerMapping
Definition: Value.hh:367
Utils::Exception< Value > Exception
Definition: Value.hh:88
Value & operator=(const Value &value)
Definition: Value.hh:121
std::string Identifier
Definition: Type.hh:24
bool isStructure() const
Definition: Type.hh:237
Type type() const
Definition: Value.hh:205
Value(const Value &value)
Definition: Value.hh:117
StructureValueFields * _structurePointer
Definition: Value.hh:66
#define require(Exception, expr, message)
Definition: Assertion.hh:56
Namespace of the Diades project.
Identifier identifier() const
Definition: Value.hh:170
int operator()(const Value &value)
Definition: Value.hh:398
int integer() const
Definition: Value.hh:141
Value(ValueData *data)
Definition: Value.hh:93
set< Value > ValueSet
set of Value elements
Definition: Value.hh:316
std::ostream & operator<<(std::ostream &os, const Identifier &identifier)
Definition: Identifier.hh:210
map< Identifier, Value > StructureValueFields
Definition: Value.hh:44
bool operator!=(const Value &value) const
Definition: Value.hh:248
static string typeName()
Definition: Value.hh:89
bool valid() const
Definition: Type.hh:156
vector< Value > _identifierValues
Definition: Value.hh:368
bool operator()(const Value &value)
Definition: Value.hh:410
Value getValue(const StructureValueFields &structureValueFields)
bool boolean() const
Definition: Value.hh:155
ArrayValueFields * _arrayPointer
Definition: Value.hh:67
bool isArray() const
Definition: Type.hh:227
ValueData * _data
Definition: Value.hh:92
bool isIdentifier() const
Definition: Type.hh:217
Identifier operator()(const Value &value)
Definition: Value.hh:385
int code() const
Definition: Value.hh:218