dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
signal-time-dependent.h
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_SIGNAL_TIME_DEPENDENT_H
7 #define DYNAMIC_GRAPH_SIGNAL_TIME_DEPENDENT_H
8 #include <dynamic-graph/signal.h>
9 #include <dynamic-graph/time-dependency.h>
10 
11 namespace dynamicgraph {
62 template <class T, class Time>
63 class SignalTimeDependent : public virtual Signal<T, Time>,
64  public TimeDependency<Time> {
65  // TimeDependency<Time> timeDependency;
66 
67 public:
68  SignalTimeDependent(std::string name = "");
69  SignalTimeDependent(const SignalArray_const<Time> &arr,
70  std::string name = "");
71  SignalTimeDependent(boost::function2<T &, T &, Time> t,
72  const SignalArray_const<Time> &sig,
73  std::string name = "");
74 
75  virtual ~SignalTimeDependent() {}
76 
77  inline const T &operator()(const Time &t1) { return access(t1); }
78  const T &access(const Time &t1);
79 
80  virtual void addDependency(const SignalBase<Time> &signal);
81  virtual void removeDependency(const SignalBase<Time> &signal);
82  virtual void clearDependencies();
83 
84  std::ostream &writeGraph(std::ostream &os) const { return os; }
85 
86  std::ostream &displayDependencies(std::ostream &os, const int depth = -1,
87  std::string space = "",
88  std::string next1 = "",
89  std::string next2 = "") const {
90  return TimeDependency<Time>::displayDependencies(os, depth, space, next1,
91  next2);
92  }
93 
94  virtual bool needUpdate(const Time &t) const;
95  virtual void setPeriodTime(const Time &p);
96  virtual Time getPeriodTime() const;
97 };
98 
99 /* -------------------------------------------- */
100 
101 template <class T, class Time>
102 SignalTimeDependent<T, Time>::SignalTimeDependent(std::string name)
103  : Signal<T, Time>(name), TimeDependency<Time>(this) {}
104 
105 template <class T, class Time>
106 SignalTimeDependent<T, Time>::SignalTimeDependent(
107  const SignalArray_const<Time> &arr, std::string name)
108  : Signal<T, Time>(name), TimeDependency<Time>(this, arr) {}
109 
110 template <class T, class Time>
111 SignalTimeDependent<T, Time>::SignalTimeDependent(
112  boost::function2<T &, T &, Time> t, const SignalArray_const<Time> &sig,
113  std::string name)
114  : Signal<T, Time>(name), TimeDependency<Time>(this, sig) {
115  this->setFunction(t);
116 }
117 
118 template <class T, class Time>
119 const T &SignalTimeDependent<T, Time>::access(const Time &t1) {
120  const bool up = TimeDependency<Time>::needUpdate(t1);
121  // SignalBase<Time>::setReady(false);
122 
123  /* std::cout << "Time before: "<< signalTime << " -- " */
124  /* << t1<< " -> Up: "<<up <<std::endl ; */
125  if (up) {
126  TimeDependency<Time>::lastAskForUpdate = false;
127  const T &Tres = Signal<T, Time>::access(t1);
128  SignalBase<Time>::setReady(false);
129  return Tres;
130  } else {
131  return Signal<T, Time>::accessCopy();
132  }
133 }
134 
135 template <class T, class Time>
136 void SignalTimeDependent<T, Time>::addDependency(
137  const SignalBase<Time> &signal) {
138  TimeDependency<Time>::addDependency(signal);
139 }
140 
141 template <class T, class Time>
142 void SignalTimeDependent<T, Time>::removeDependency(
143  const SignalBase<Time> &signal) {
144  TimeDependency<Time>::removeDependency(signal);
145 }
146 
147 template <class T, class Time>
148 void SignalTimeDependent<T, Time>::clearDependencies() {
149  TimeDependency<Time>::clearDependency();
150 }
151 
152 template <class T, class Time>
153 bool SignalTimeDependent<T, Time>::needUpdate(const Time &t) const {
154  return TimeDependency<Time>::needUpdate(t);
155 }
156 
157 template <class T, class Time>
158 void SignalTimeDependent<T, Time>::setPeriodTime(const Time &p) {
159  TimeDependency<Time>::setPeriodTime(p);
160 }
161 template <class T, class Time>
162 Time SignalTimeDependent<T, Time>::getPeriodTime() const {
163  return TimeDependency<Time>::getPeriodTime();
164 }
165 
166 } // end of namespace dynamicgraph
167 
168 #endif