DiaDes  0.1
DIAgnosis of Discrete-Event System
Variable.hh
Go to the documentation of this file.
1 #ifndef __DIADES__SDMDL__VARIABLE__HH
2 #define __DIADES__SDMDL__VARIABLE__HH
3 
4 #include<vector>
5 #include<ostream>
7 #include<diades/sdmdl/Value.hh>
8 
9 
10 namespace Diades
11 {
12  namespace Sdmdl
13  {
14 
15  class VariableFactory;
16 
24  class Variable
25  {
26  public:
28  static string typeName() { return "Diades::Sdmdl::Variable"; }
29 
30  public:
31  typedef std::reference_wrapper<Variable const> ConstReference;
32  typedef std::reference_wrapper<Variable> Reference;
33  typedef std::vector<Reference> VariableVector;
34  typedef Variable * Pointer;
35 
36  private:
40  string _name;
41 
45  unsigned _id;
46 
47 
51  unsigned _index;
52 
53 
54 
59 
64 
65 
66 
71 
76 
81 
85  bool _observable;
86 
87 
88  private:
89 
95  Variable(VariableFactory & owner):_name("__null__diades__sdmdl_variable"),_id(0),_index(0),_owner(owner),_values(),_domain(),_default(),_current(),_observable(false) {}
96 
97 
105  Variable(const string & name, unsigned id, VariableFactory & owner):_name(name),_id(id),_index(0),_owner(owner),_values(),_domain(),_default(),_current(),_observable(false){}
106 
107 
108 
109  public:
110 
115 
116 
117 
124  const string & name() const
125  {
126  return _name;
127  }
128 
129 
136  unsigned id() const
137  {
138  return _id;
139  }
140 
141 
148  const VariableFactory & owner() const
149  {
150  return _owner;
151  }
152 
160  {
161  return _owner;
162  }
163 
164 
171  bool isNull() const;
172 
173 
181  bool operator==(const Variable & variable) const
182  {
183  return this == &variable;
184  }
185 
186 
187 
195  bool operator!=(const Variable & variable) const
196  {
197  return !(*this == variable);
198  }
199 
200 
201 
208  const ValueVector & domain() const
209  {
210  return _domain;
211  }
212 
213 
214 
215 
221  void setIndex(unsigned index)
222  {
223  _index = index;
224  }
225 
232  unsigned index() const
233  {
234  return _index;
235  }
236 
237 
247  const Value & addValue(const string & label);
248 
249 
257  bool containsValueOfLabel(const string & label) const;
258 
259 
267  bool contains(const Value & val) const;
268 
269 
270 
278  void setDefault(const Value & val)
279  {
280  require(Exception,!val.isNull(),"setDefault: the Value is null");
281  require(Exception,contains(val),
282  Utils::Msg("setDefault: the Value %1% does not belong to the domain of the Variable %2%")
283  % val % name());
284  _default.clear();
285  _default.push_back(val);
286  }
287 
288 
289 
290 
291 
297  {
298  require(Exception,!isNull(),"setObservable, null variable");
299  _observable = true;
300  }
301 
302 
303 
310  {
311  require(Exception,!isNull(),"setObservable, null variable");
312  _observable = false;
313  }
314 
315 
316 
317 
324  bool isObservable() const
325  {
326  return _observable;
327  }
328 
329 
338  const Value & getValue(const string & label) const;
339 
347  const Value & getValue(unsigned id) const;
348 
356  const Value & defaultValue() const;
357 
358 
364  const Value & value() const;
365 
371  void assign(const Value & value)
372  {
373  require(Exception,contains(value),
374  Utils::Msg("assign: the Value %1% is not contained in the Variable %2%")
375  % value % name());
376  _current.clear();
377  _current.push_back(value);
378  }
379 
380 
381 
382 
391  friend ostream & operator<<(ostream & os, const Variable & variable);
392 
393 
394  friend class VariableFactory;
395 
396  };
397 
398 
399 
404 
405 
406 
408  {
409 
410  public:
411  static std::string typeName() { return "Diades::Sdmdl::VariableFactory"; }
413 
414 
415  private:
419  std::unordered_map<std::string,Variable::Pointer> _dictionary;
420 
424  VariableVector _nullVariable;
425 
426 
427 
428  public:
429 
434  VariableFactory():_dictionary(),_nullVariable()
435  {
436  reset();
437  }
438 
439 
440 
446  {
447  clear();
448  }
449 
450 
455  void reset();
456 
457 
462  void clear();
463 
464 
472  bool operator==(const VariableFactory & factory) const
473  {
474  return this == &factory;
475  }
476 
482  {
483  return _dictionary.size();
484  }
485 
493  Variable & newVariable(const std::string & label);
494 
502  Variable & getVariable(const std::string & label);
503 
510  const Variable & nullVariable() const
511  {
512  return _nullVariable[0];
513  }
514 
515 
523  {
524  return _nullVariable[0];
525  }
526 
527  };
528 
529 
530 
531 
532  };
533 };
534 
535 #endif
const string & name() const
Definition: Variable.hh:124
const Value & getValue(const string &label) const
bool operator!=(const Variable &variable) const
Definition: Variable.hh:195
unsigned index() const
Definition: Variable.hh:232
Variable(const string &name, unsigned id, VariableFactory &owner)
Definition: Variable.hh:105
void assign(const Value &value)
Definition: Variable.hh:371
const Value & defaultValue() const
VariableVector _nullVariable
Definition: Variable.hh:424
const VariableFactory & owner() const
Definition: Variable.hh:148
unsigned id() const
Definition: Variable.hh:136
bool operator==(const VariableFactory &factory) const
Definition: Variable.hh:472
std::unordered_map< std::string, Variable::Pointer > _dictionary
Definition: Variable.hh:419
bool isNull() const
VariableFactory & _owner
Definition: Variable.hh:58
bool contains(const Value &val) const
VariableFactory & owner()
Definition: Variable.hh:159
Diades::Utils::Exception< VariableFactory > Exception
Definition: Variable.hh:412
Variable(VariableFactory &owner)
Definition: Variable.hh:95
std::vector< Reference > VariableVector
Definition: Variable.hh:33
Utils::Exception< Variable > Exception
Definition: Variable.hh:27
static string typeName()
Definition: Variable.hh:28
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
static std::string typeName()
Definition: Variable.hh:411
void setIndex(unsigned index)
Definition: Variable.hh:221
bool operator==(const Variable &variable) const
Definition: Variable.hh:181
Value::ValueVector ValueVector
Definition: Value.hh:187
const Value & value() const
const Value & addValue(const string &label)
const ValueVector & domain() const
Definition: Variable.hh:208
std::reference_wrapper< Variable const > ConstReference
Definition: Variable.hh:31
std::reference_wrapper< Variable > Reference
Definition: Variable.hh:32
bool containsValueOfLabel(const string &label) const
bool isObservable() const
Definition: Variable.hh:324
ValueVector _default
Definition: Variable.hh:75
ValueVector _current
Definition: Variable.hh:80
boost::format Msg
Definition: Verbose.hh:42
void setDefault(const Value &val)
Definition: Variable.hh:278
ValueFactory _values
Definition: Variable.hh:63
friend ostream & operator<<(ostream &os, const Variable &variable)
ValueVector _domain
Definition: Variable.hh:70
size_t numberOfAllocatedVariables() const
Definition: Variable.hh:481
const Variable & nullVariable() const
Definition: Variable.hh:510