DiaDes  0.1
DIAgnosisofDiscrete-EventSystem
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>
14 
16 
17 
18 using namespace std;
19 
20 namespace Diades
21 {
22  namespace Altarica
23  {
24  using Diades::Utils::Msg;
25 
26 
27 
28  class Value;
29  class ValueFactory;
30  class AltaricaModel;
31  class AltaricaModelData;
32 
37  class Value
38  {
39  public:
40 
45  typedef enum{OrOp=0,AndOp=1,ImplyOp=2,EqOp=3,NeqOp=4,GtOp=5,GeqOp=6,LtOp=7,LeqOp=8,AddOp=9,SubOp=10,MulOp=11,DivOp=12,ModOp=13} BinaryOperator;
50  typedef enum{NotOp,NegOp,IdentityOp} UnaryOperator;
51 
52 
57  typedef vector<int>::size_type Code;
58  typedef reference_wrapper<Value> Reference;
59  typedef reference_wrapper<Value const> ConstReference;
60  typedef Value * Pointer;
64  typedef map<Identifier,ConstReference> StructureFields;
65 
69  typedef vector<ConstReference> ArrayFields;
70 
71 
72 
73 
74  public:
76  static string typeName() { return "Altarica::Value"; }
77 
78  private:
81  Code _code;
82  bool _boolean;
83  int _integer;
85  StructureFields _structure;
86  ArrayFields _array;
87 
88 
94  Value(ValueFactory & factory);
95  bool isEqualStructure(const Value & value) const;
96  bool isLessStructure(const Value & value) const;
97  bool isEqualArray(const Value & value) const;
98  bool isLessArray(const Value & value) const;
99 
100  friend class ValueFactory;
101  public:
102 
106  bool isNull() const;
111  int integer() const
112  {
113  always_require(Exception, !isNull(),"integer(): invalid value");
114  always_require(Exception, type().isInteger(), "integer(): this value is not an integer");
115  return _integer;
116  }
121  bool boolean() const
122  {
123  always_require(Exception, !isNull(),"boolean(): invalid value");
124  always_require(Exception, type().isBoolean(), "boolean(): this value is not a boolean");
125  return _boolean;
126  }
127 
132  const Identifier & identifier() const
133  {
134  always_require(Exception, !isNull(),"identifier(): invalid value");
135  always_require(Exception, type().isIdentifier(), "identifier(): this value is not an indentifier");
136  return _identifier;
137  }
138 
143  unsigned size() const;
144 
150  const Value & getField(unsigned index) const;
151 
152 
158  const Value & getField(const Identifier & fieldName) const;
159 
163  const Type & type() const
164  {
165  return _type;
166  }
167 
172  Code code() const
173  {
174  always_require(Exception,!isNull(),"code(): invalid value");
175  return _code;
176  }
177 
181  string textify() const;
182 
193  bool operator==(const Value & value) const;
194 
198  bool operator!=(const Value & value) const { return !(*this == value); }
202  bool operator<(const Value & value) const;
206  bool operator>(const Value & value) const { return (value < *this); }
210  bool operator<=(const Value & value) const { return (*this < value) || (*this == value); }
214  bool operator>=(const Value & value) const { return (*this > value) || (*this == value); }
215 
222  bool constraint(BinaryOperator op, const Value & value) const;
223 
230  const AltaricaModel & owner() const;
231 
232 
233  const Value & apply(BinaryOperator op, const Value & value) const;
234  const Value & apply(UnaryOperator op) const;
235 
236 
237  friend ostream & operator<<(ostream & os, const Value & value);
238  };
239 
244  typedef vector<Value::ConstReference> ValueEnumeration;
245 
246 
247  template<typename T>
248  struct EqualRef
249  {
250  bool operator()(const reference_wrapper<T> & ref1, const reference_wrapper<T> & ref2)
251  {
252  return ref1.get() == ref2.get();
253  }
254  };
255 
256 
257 
258 
259  template<typename T>
260  bool isEqual(const vector< reference_wrapper<T> > & vect1, const vector< reference_wrapper<T> > & vect2)
261  {
262  bool result = vect1.size() == vect2.size();
263  unsigned index = 0;
264  while(result && (index < vect1.size()))
265  {
266  result = EqualRef<T>()(vect1[index],vect2[index]);
267  ++index;
268  }
269  return result;
270  }
271 
272  template<typename T1, typename T2>
273  bool isEqual(const map< T1, reference_wrapper<T2> > & map1, const map< T1, reference_wrapper<T2> > & map2)
274  {
275  bool result = map1.size() == map2.size();
276  typename map< T1, reference_wrapper<T2> >::const_iterator it1 = map1.begin();
277  typename map< T1, reference_wrapper<T2> >::const_iterator it2 = map2.begin();
278  while(result && (it1 != map1.end()))
279  {
280  result = (it1->first == it2->first) && EqualRef<T2>()(it1->second,it2->second);
281  ++it1;++it2;
282  }
283  return result;
284  }
285 
286 
287 
288  /****************************************************************************/
289 
295  {
296  public:
298  static string typeName() { return "Diades::Altarica::ValueFactory"; }
299 
300  public:
301 
309  const Value & getValue(const Identifier & value);
314  const Value & getValue(bool value);
319  const Value & getValue(int value);
320 
325  const Value & getValue(const Value::ArrayFields & arrayValueFields);
326 
331  const Value & getValue(const Value::StructureFields & structureValueFields);
332 
333 
334 
340  const Value & nullValue() const
341  {
342  return *_basicValues[0];
343  }
344 
345 
346  ~ValueFactory();
347 
348 
349 
350  ValueFactory(AltaricaModel & model);
351 
352  void init();
353 
354  void clear();
355 
356  const AltaricaModel & owner() const;
357  AltaricaModel & owner();
358  private:
360  vector<Value::Pointer> _basicValues;
361  unordered_map<Identifier,unsigned> _identifierMapping;
362  unordered_map<int,Value::Pointer> _integerMapping;
363  vector<Value::Pointer> _identifierValues;
364  unordered_map<string,unsigned> _structureMapping;
365  vector<Value::Pointer> _structuredValues;
366  unordered_map<string,unsigned> _arrayMapping;
367  vector<Value::Pointer> _arrayedValues;
368 
369 
370  friend class AltaricaModel;
371  friend class Value;
372  };
373 
374  /****************************************************************************/
375 
381  {
382  Identifier operator()(const Value & value)
383  {
384  return value.identifier();
385  }
386  };
387 
388 
394  {
395  int operator()(const Value & value)
396  {
397  return value.integer();
398  }
399  };
400 
406  {
407  bool operator()(const Value & value)
408  {
409  return value.boolean();
410  }
411  };
412 
413 
414  template <Value::BinaryOperator OP>
416  {
418  static string typeName() { return "Diades::Altarica::BinaryOperator"; }
419  const Value & operator() (const Value & value1, const Value & value2) const
420  {
421  try
422  {
423  return value1.apply(OP,value2);
424  }
425  catch(exception & e)
426  {
427  throw(Exception(Msg("Failure to apply a binary operator between '%1%' and '%2%'.")
428  % value1 % value2,e));
429  }
430  return value1;
431  }
432  };
433 
434  template <Value::UnaryOperator OP>
436  {
438  static string typeName() { return "Diades::Altarica::UnaryOperator"; }
439  const Value & operator() (const Value & value) const
440  {
441  try
442  {
443  return value.apply(OP);
444  }
445  catch(exception & e)
446  {
447  throw(Exception(Msg("Failure to apply a unary operator on value '%1%'.")
448  % value,e));
449  }
450  return value;
451  }
452  };
453 
454 
455 
456 
457 
458 
459 
460  };
461 };
462 
463 
464 
465 
466 
467 
468 
469 
470 #endif
Identifier _identifier
Definition: Value.hh:84
vector< Value::Pointer > _identifierValues
Definition: Value.hh:363
const Identifier & identifier() const
Definition: Value.hh:132
bool operator()(const reference_wrapper< T > &ref1, const reference_wrapper< T > &ref2)
Definition: Value.hh:250
Utils::Exception< UnaryOperator< OP > > Exception
Definition: Value.hh:437
An Identifier is a reference to a string (IdentifierData) that only contains alpha-numeric characters...
Definition: Identifier.hh:121
#define always_require(Exception, expr, message)
Definition: Assertion.hh:121
unordered_map< string, unsigned > _structureMapping
Definition: Value.hh:364
bool operator>=(const Value &value) const
Definition: Value.hh:214
Type::ConstReference _type
Definition: Value.hh:80
STL namespace.
vector< int >::size_type Code
Definition: Value.hh:57
const Value & nullValue() const
Definition: Value.hh:340
Utils::Exception< ValueFactory > Exception
Definition: Value.hh:297
vector< ConstReference > ArrayFields
Definition: Value.hh:69
map< Identifier, ConstReference > StructureFields
Definition: Value.hh:64
static string typeName()
Definition: Value.hh:298
ValueFactory & _factory
Definition: Value.hh:79
ArrayFields _array
Definition: Value.hh:86
Utils::Exception< Value > Exception
Definition: Value.hh:75
reference_wrapper< Type const > ConstReference
Definition: Type.hh:62
reference_wrapper< Value const > ConstReference
Definition: Value.hh:59
const Value & apply(BinaryOperator op, const Value &value) const
Utils::Exception< BinaryOperator< OP > > Exception
Definition: Value.hh:417
static string typeName()
Definition: Value.hh:438
Namespace of the Diades project.
StructureFields _structure
Definition: Value.hh:85
int operator()(const Value &value)
Definition: Value.hh:395
unordered_map< Identifier, unsigned > _identifierMapping
Definition: Value.hh:361
reference_wrapper< Value > Reference
Definition: Value.hh:58
bool boolean() const
Definition: Value.hh:121
unordered_map< int, Value::Pointer > _integerMapping
Definition: Value.hh:362
vector< Value::Pointer > _arrayedValues
Definition: Value.hh:367
ostream & operator<<(ostream &os, const CarriedValue &value)
bool isEqual(const map< T1, reference_wrapper< T2 > > &map1, const map< T1, reference_wrapper< T2 > > &map2)
Definition: Value.hh:273
static string typeName()
Definition: Value.hh:76
vector< Value::ConstReference > ValueEnumeration
enumeration of Value elements (unicity and sorting is in charge of the user)
Definition: Value.hh:244
vector< Value::Pointer > _structuredValues
Definition: Value.hh:365
unordered_map< string, unsigned > _arrayMapping
Definition: Value.hh:366
bool operator()(const Value &value)
Definition: Value.hh:407
static string typeName()
Definition: Value.hh:418
int integer() const
Definition: Value.hh:111
boost::format Msg
Definition: Verbose.hh:42
AltaricaModel & _owner
Definition: Value.hh:359
bool operator>(const Value &value) const
Definition: Value.hh:206
vector< Value::Pointer > _basicValues
Definition: Value.hh:360
bool operator!=(const Value &value) const
Definition: Value.hh:198
Identifier operator()(const Value &value)
Definition: Value.hh:382
Code code() const
Definition: Value.hh:172
const Type & type() const
Definition: Value.hh:163
bool operator<=(const Value &value) const
Definition: Value.hh:210