ocra-recipes
Doxygen documentation for the ocra-recipes repository
MergedVariable.cpp
Go to the documentation of this file.
3 #include <boost/foreach.hpp>
4 
5 namespace ocra
6 {
7  MergedVariable::MergedVariable(const std::string& name)
8  : _base(name)
9  , _isVariableUpToDate(false)
10  {
11  }
12 
14  {
15  BOOST_FOREACH(map_t::value_type& element, _varToMapping)
16  delete element.second.second;
17  }
18 
20  {
21  var->connect<EVT_CHANGE_DEPENDENCIES>(*this, &MergedVariable::invalidateVariable);
22  invalidateVariable();
23 
24  map_t::iterator it = _varToMapping.find(var);
25  if(it == _varToMapping.end())
26  _varToMapping[var] = std::make_pair(1, new VariableMapping(*var, _base));
27  else
28  ++it->second.first;
29  }
30 
32  {
33  map_t::iterator it = _varToMapping.find(var);
34  ocra_assert(it != _varToMapping.end() && "Precondition violation: nesting class should ensure this never happens");
35  ocra_assert(it->second.first > 0 && "Invariant violation: there should always be at least one variable attached to a mapping");
36 
37  var->disconnect<EVT_CHANGE_DEPENDENCIES>(*this, &MergedVariable::invalidateVariable);
38  invalidateVariable();
39 
40  --it->second.first;
41  if(it->second.first == 0)
42  {
43  delete it->second.second; // talk about readability...
44  _varToMapping.erase(it);
45  }
46  }
47 
49  {
50  if (!_isVariableUpToDate)
52 
53  map_t::const_iterator it = _varToMapping.find(var);
54  return it != _varToMapping.end() ? it->second.second : 0x0;
55  }
56 
58  {
59  if (!_isVariableUpToDate)
61 
62  return _base;
63  }
64 
65  void MergedVariable::setValue(const VectorXd& val) const
66  {
67  if(val.rows() == 0)
68  return;
69  if (!_isVariableUpToDate)
71 
72  _base.setValue(val);
73  }
74 
76  {
78 
79  BOOST_FOREACH(const map_t::value_type& mapping, _varToMapping)
80  _base.addByMerge(*mapping.first);
81 
82  _isVariableUpToDate = true;
83  }
84 
85  void MergedVariable::invalidateVariable()
86  {
87  _isVariableUpToDate = false;
88  }
89 
90  void MergedVariable::invalidateVariable(int timestamp)
91  {
92  invalidateVariable();
93  }
94 
96  {
97  return _isVariableUpToDate;
98  }
99 }
100 
101 // cmake:sourcegroup=Variable
MergedVariable(const std::string &name)
void insert(Variable *var)
void setValue(const VectorXd &value)
Definition: Variable.cpp:99
void setValue(const VectorXd &val) const
bool isVariableUpToDate() const
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void remove(Variable *var)
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void recomputeVariable() const
A class to manage the relative mapping of a variable with respect to another one. ...
CompositeVariable & addByMerge(Variable &v)
Definition: Variable.cpp:475
VariableMapping * find(Variable *var) const
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
CompositeVariable & getVariable()
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
void disconnect(Derived &object, void(Base::*callbackToErase)(int)) const
Disconnect non-static method.
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.
Declaration file of the VariableMapping class.