dynamic-graph  4.4.3
Dynamic graph library
time-dependency.t.cpp
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_TIME_DEPENDENCY_T_CPP
7 #define DYNAMIC_GRAPH_TIME_DEPENDENCY_T_CPP
8 #include <dynamic-graph/time-dependency.h>
9 
10 #undef VP_TEMPLATE_DEBUG_MODE
11 #define VP_TEMPLATE_DEBUG_MODE 0
12 #include <dynamic-graph/debug.h>
13 
14 #define __TIME_DEPENDENCY_INIT(sig, dep) \
15  leader(*sig), dependencies(), updateFromAllChildren(ALL_READY_DEFAULT), \
16  dependencyType(dep), periodTime(PERIOD_TIME_DEFAULT)
17 
18 namespace dynamicgraph {
19 template <class Time>
20 TimeDependency<Time>::TimeDependency(SignalBase<Time> *sig,
21  const DependencyType dep)
22  : __TIME_DEPENDENCY_INIT(sig, dep) {}
23 
24 template <class Time>
25 TimeDependency<Time>::TimeDependency(SignalBase<Time> *sig,
26  const SignalArray_const<Time> &ar,
27  const DependencyType dep)
28  : __TIME_DEPENDENCY_INIT(sig, dep) {
29  addDependencies(ar);
30 
31  return;
32 }
33 
34 /* ------------------------------------------------------------------------ */
35 template <class Time>
36 void TimeDependency<Time>::addDependencies(const SignalArray_const<Time> &ar) {
37  for (unsigned int i = 0; i < ar.getSize(); ++i) {
38  addDependency(ar[i]);
39  }
40 }
41 
42 template <class Time>
43 void TimeDependency<Time>::addDependency(const SignalBase<Time> &sig) {
44  dependencies.push_front(&sig);
45 }
46 
47 template <class Time>
48 void TimeDependency<Time>::removeDependency(const SignalBase<Time> &sig) {
49  dependencies.remove(&sig);
50 }
51 
52 template <class Time>
53 void TimeDependency<Time>::clearDependency() {
54  dependencies.clear();
55 }
56 
57 template <class Time>
58 bool TimeDependency<Time>::needUpdate(const Time &t1) const {
59  dgTDEBUG(15) << "# In {" << leader.getName() << " : (" << leader.getReady()
60  << ") " << t1 << " >? " << leader.getTime() << std::endl;
61 
62  if (leader.getReady()) {
63  dgTDEBUGOUT(15);
64  return true;
65  }
66  if (lastAskForUpdate) {
67  dgTDEBUGOUT(15);
68  return true;
69  }
70 
71  switch (dependencyType) {
72  case ALWAYS_READY: {
73  dgTDEBUGOUT(15);
74  return true;
75  }
76  case BOOL_DEPENDENT:
77  break;
78  case TIME_DEPENDENT: {
79  if (t1 < leader.getTime() + periodTime) {
80  dgTDEBUGOUT(15);
81  return false;
82  }
83  break;
84  }
85  };
86 
87  bool res = updateFromAllChildren;
88  const typename Dependencies::const_iterator itend = dependencies.end();
89  for (typename Dependencies::const_iterator it = dependencies.begin();
90  it != itend; ++it) {
91  const SignalBase<Time> &sig = **it;
92  dgTDEBUG(15) << "Ask update for " << sig << std::endl;
93 
94  if ((sig.getTime() > leader.getTime()) || (sig.needUpdate(t1))) {
95  if (updateFromAllChildren)
96  continue;
97  else {
98  res = true;
99  break;
100  }
101  } else {
102  if (updateFromAllChildren) {
103  res = false;
104  break;
105  } else
106  continue;
107  }
108  }
109  lastAskForUpdate = res;
110 
111  dgTDEBUGOUT(15);
112  return res;
113 }
114 
115 template <class Time>
116 std::ostream &TimeDependency<Time>::writeGraph(std::ostream &os) const {
117  return os;
118 }
119 
120 template <class Time>
121 std::ostream &TimeDependency<Time>::displayDependencies(
122  std::ostream &os, const int depth, std::string space, std::string next1,
123  std::string next2) const {
124  leader.SignalBase<Time>::displayDependencies(os, depth, space, next1, next2)
125  << " (";
126  switch (dependencyType) {
127  case ALWAYS_READY:
128  os << "A";
129  break;
130  case BOOL_DEPENDENT:
131  os << "ready=" << ((leader.getReady()) ? "TRUE" : "FALSE");
132  break;
133  case TIME_DEPENDENT:
134  os << "t=" << leader.getTime() << " (/" << periodTime << ") ";
135  break;
136  };
137  os << ")"; //<<std::endl;
138  {
139  const typename Dependencies::const_iterator itend = dependencies.end();
140  for (typename Dependencies::const_iterator it = dependencies.begin();
141  it != itend; ++it)
142  if (depth != 0) {
143  os << std::endl;
144  std::string ajout = "|";
145  std::string ajout2 = "|";
146  typename Dependencies::const_iterator it2 = it;
147  ++it2;
148  if (it2 == dependencies.end()) {
149  ajout = "`";
150  ajout2 = " ";
151  }
152  (*it)->displayDependencies(os, depth - 1, space + next2 + " ", ajout,
153  ajout2);
154  } else {
155  os << std::endl << space << " `-- ...";
156  break;
157  }
158  }
159  return os;
160 }
161 
162 } // end of namespace dynamicgraph
163 
164 #undef __TIME_DEPENDENCY_INIT
165 #endif
dynamicgraph
Definition: command-bind.h:30