DiaDes  0.1
DIAgnosis of Discrete-Event System
Assignment.hh
Go to the documentation of this file.
1 
9 #ifndef _DIADES__SDMDL_EXPERIMENTAL__ASSIGNMENT__HH__
10 #define _DIADES__SDMDL_EXPERIMENTAL__ASSIGNMENT__HH__
11 
13 
14 namespace Diades
15 {
16  namespace Sdmdl
17  {
18  namespace Experimental
19  {
20 
30  class Assignment
31  {
32  public:
37 
42 
47 
52  static string typeName()
53  {
54  return "Sdmdl::Experimental::Assignment";
55  }
56 
57  private:
61  using ConstVarReference = std::reference_wrapper<Variable const>;
65  using ConstValReference = std::reference_wrapper<Value const>;
69  using VarVector = std::vector<ConstVarReference>;
73  using ValVector = std::vector<ConstValReference>;
84 
85 
86  public:
87 
92  Assignment() : _variable(), _value()
93  {
94  }
95 
104  bool valid() const
105  {
106  return (_variable.size()==1) && (_value.size()==1)
107  && (!_variable[0].get().isNull()) && (!_value[0].get().isNull())
108  && (_variable[0].get().contains(_value[0].get()));
109  }
110 
121  Assignment(const Variable & var, const Value & val): _variable(), _value()
122  {
123  _variable.push_back(var);
124  _value.push_back(val);
125  }
126 
132  Assignment(const Assignment & assignment) : _variable(), _value()
133  {
134  if(assignment._variable.size()==1)
135  {
136  _variable.emplace_back(assignment.variable());
137  }
138  if(assignment._value.size()==1)
139  {
140  _value.emplace_back(assignment.value());
141  }
142  }
143 
149  Assignment(const Assignment && assignment) : _variable(), _value()
150  {
151  if(assignment._variable.size()==1)
152  {
153  _variable.emplace_back(assignment.variable());
154  }
155  if(assignment._value.size()==1)
156  {
157  _value.emplace_back(assignment.value());
158  }
159  }
160 
161 
168  virtual ~Assignment()
169  {
170  }
171 
179  void setVariable(const Variable & var)
180  {
181  _variable.clear();
182  _value.clear();
183  if(!var.isNull())
184  {
185  if(!var.defaultValue().isNull())
186  {
187  _variable.push_back(var);
188  _value.push_back(var.defaultValue());
189  }
190  }
191  }
192 
193 
202  void setValue(const Value & val)
203  {
204  if((_variable.size()==1) && (!_variable[0].get().isNull()))
205  {
206  if(!val.isNull() && _variable[0].get().contains(val))
207  {
208  _value.clear();
209  _value.push_back(val);
210  }
211  }
212  }
213 
214 
221  const Variable & variable() const
222  {
223  require(Exception,valid(),"variable(): invalid assignment");
224  return _variable[0].get();
225  }
226 
233  const Value & value() const
234  {
235  require(Exception,valid(),"value(): invalid assignment");
236  return _value[0].get();
237  }
238 
246  Assignment & operator=(const Assignment & assign)
247  {
248  if (&assign != this)
249  {
250  _variable.clear();
251  if(assign._variable.size() == 1)
252  {
253  _variable.push_back(assign.variable());
254  }
255  _value.clear();
256  if(assign._value.size()==1)
257  {
258  _value.push_back(assign.value());
259  }
260  }
261  return *this;
262  }
263 
272  bool operator==(const Assignment & assignment) const
273  {
274  return (!valid() && !assignment.valid())
275  || (valid() && assignment.valid() && (variable() == assignment.variable()) && (value() == assignment.value()));
276  }
277 
278 
283  void unvalidate()
284  {
285  _variable.clear();
286  _value.clear();
287  }
288 
289 
299  friend ostream & operator<<(ostream & os, const Assignment & assign)
300  {
301  if(assign.valid())
302  {
303  os << assign.variable() << "=" << assign.value();
304  }
305  else
306  {
307  os << "INVALID_ASSIGNMENT";
308  }
309  return os;
310  }
311 
312  };
313  }
314  }
315 }
316 
317 #endif
318 
std::reference_wrapper< Value const > ConstValReference
Definition: Assignment.hh:65
const Value & defaultValue() const
friend ostream & operator<<(ostream &os, const Assignment &assign)
Definition: Assignment.hh:299
Assignment(const Assignment &&assignment)
Definition: Assignment.hh:149
std::reference_wrapper< Variable const > ConstVarReference
Definition: Assignment.hh:61
Assignment & operator=(const Assignment &assign)
Definition: Assignment.hh:246
bool isNull() const
std::vector< ConstValReference > ValVector
Definition: Assignment.hh:73
void setVariable(const Variable &var)
Definition: Assignment.hh:179
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
std::vector< ConstVarReference > VarVector
Definition: Assignment.hh:69
const Variable & variable() const
Definition: Assignment.hh:221
Assignment(const Assignment &assignment)
Definition: Assignment.hh:132
Assignment(const Variable &var, const Value &val)
Definition: Assignment.hh:121
bool operator==(const Assignment &assignment) const
Definition: Assignment.hh:272