signal-time-dependent.h 5.03 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
2
3
4
/*
 * Copyright 2010,
 * François Bleibel,
 * Olivier Stasse,
5
 *
Thomas Moulard's avatar
Thomas Moulard committed
6
 * CNRS/AIST
7
 *
Thomas Moulard's avatar
Thomas Moulard committed
8
9
10
11
12
13
14
15
16
17
18
19
 * This file is part of dynamic-graph.
 * dynamic-graph is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 * dynamic-graph is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.  You should
 * have received a copy of the GNU Lesser General Public License along
 * with dynamic-graph.  If not, see <http://www.gnu.org/licenses/>.
 */
20
21
22
23
24
25
26
27
28
29

#ifndef __DEPENDENT_SIGNAL_HH
#define __DEPENDENT_SIGNAL_HH

#include <dynamic-graph/signal.h>

#include <dynamic-graph/time-dependency.h>

namespace dynamicgraph {

30
31
32
33
34
35
36
37
/*!  \brief A type of signal that enforces a time dependency between other signals,
making sure its inputs are up to date on access, using a incrementing time tick as reference.
It works this way: for a given SignalTimeDependent S, the user manually adds dependent signals through the
use of the addDependency function. On access (calling the signal S operator () or access(Time) function),
if the dependent signals are not up-to-date, i.e. if their [last update] time is less than the
current time, their value will be access()'ed to bring them up-to-date. Thus, the value of dependent
signals can be accessed \b quickly and \b repeatedly through the accessCopy() function.
 */
38
39
40
template< class T,class Time >
class SignalTimeDependent
: public virtual Signal<T,Time>
Thomas Moulard's avatar
Thomas Moulard committed
41
, public TimeDependency<Time>
42
43
44
45
46
47
48
49
50
51
52
{

  //TimeDependency<Time> timeDependency;

 public:
  SignalTimeDependent( std::string name = "" );
  SignalTimeDependent( const SignalArray_const<Time> & arr
			  ,std::string name = "" );
  SignalTimeDependent( boost::function2<T&,T&,Time> t
			  ,const SignalArray_const<Time> & sig
			  ,std::string name = "" );
Thomas Moulard's avatar
Thomas Moulard committed
53

54
  virtual ~SignalTimeDependent( void ) {}
Thomas Moulard's avatar
Thomas Moulard committed
55

56
57
  inline const T& operator() ( const Time& t1 ){ return access(t1); }
  const T& access( const Time& t1 );
Thomas Moulard's avatar
Thomas Moulard committed
58

59
60
61
62
63
64
65
66
67
68
69
70
  virtual void addDependency( const SignalBase<Time>& signal ) ;
  virtual void removeDependency( const SignalBase<Time>& signal ) ;
  virtual void clearDependencies( void );

  std::ostream& writeGraph(std::ostream &os) const
    { return os;}

  std::ostream& displayDependencies( std::ostream& os,const int depth=-1,
				     std::string space="",
				     std::string next1="",std::string next2="" ) const
    { return TimeDependency<Time>::displayDependencies( os,depth,space,next1,next2 ); }

Thomas Moulard's avatar
Thomas Moulard committed
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  virtual bool needUpdate( const Time& t ) const;
  virtual void setPeriodTime( const Time& p ) ;
  virtual Time getPeriodTime( void ) const;

};


/* -------------------------------------------- */


template < class T,class Time >
SignalTimeDependent<T,Time>::
SignalTimeDependent( std::string name )
    :Signal<T,Time>(name)
     ,TimeDependency<Time>(this)
    {}

template< class T,class Time>
SignalTimeDependent<T,Time>::
SignalTimeDependent( const SignalArray_const<Time> & arr
			  ,std::string name )
    :Signal<T,Time>(name)
    ,TimeDependency<Time>(this,arr)
    {}

template< class T,class Time>
SignalTimeDependent<T,Time>::
  SignalTimeDependent( boost::function2<T&,T&,Time> t
			  ,const SignalArray_const<Time> & sig
			  ,std::string name )
    :Signal<T,Time>(name)
    ,TimeDependency<Time>(this,sig)
    { setFunction(t); }
Thomas Moulard's avatar
Thomas Moulard committed
105

106
107
108
109
template< class T,class Time>
  const T& SignalTimeDependent<T,Time>::
access( const Time& t1 )
    {
Thomas Moulard's avatar
Thomas Moulard committed
110
      const bool up = TimeDependency<Time>::needUpdate(t1);
111
112
113
114
115
116
117
118
      //SignalBase<Time>::setReady(false);

/*       std::cout << "Time before: "<< signalTime << " -- "   */
/*   		<< t1<< "  -> Up: "<<up <<std::endl ;   */
      if( up )
	{
	  TimeDependency<Time>::lastAskForUpdate = false;
	  const T& Tres = Signal<T,Time>::access(t1);
Thomas Moulard's avatar
Thomas Moulard committed
119
	  SignalBase<Time>::setReady(false);
120
121
122
123
124
125
126
127
	  return Tres;
	}
      else { return Signal<T,Time>::accessCopy(); }
    }


template< class T,class Time>
   void SignalTimeDependent<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
128
addDependency( const SignalBase<Time>& signal )
129
130
131
132
    { TimeDependency<Time>::addDependency(signal); }

template< class T,class Time>
   void SignalTimeDependent<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
133
removeDependency( const SignalBase<Time>& signal )
134
135
136
137
138
139
140
141
142
143
144
    { TimeDependency<Time>::removeDependency(signal); }

template< class T,class Time>
   void SignalTimeDependent<T,Time>::
clearDependencies( void )
    { TimeDependency<Time>::clearDependency(); }

template< class T,class Time>
  bool SignalTimeDependent<T,Time>::
needUpdate( const Time& t ) const
{
Thomas Moulard's avatar
Thomas Moulard committed
145
  return TimeDependency<Time>::needUpdate(t);
146
147
148
149
}

template< class T,class Time>
    void SignalTimeDependent<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
150
setPeriodTime( const Time& p )
151
152
153
154
155
156
157
158
159
    { TimeDependency<Time>::setPeriodTime( p ); }
template< class T,class Time>
 Time SignalTimeDependent<T,Time>::
getPeriodTime( void )  const
    { return TimeDependency<Time>::getPeriodTime();  }

} // namespace dynamicgraph

#endif /* #ifndef __DEPENDENT_SIGNAL_HH */