signal-time-dependent.h 4.58 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
2
3
4
5
6
7
8
9
10
11
// -*- mode: c++ -*-
// Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
// JRL, CNRS/AIST.
//

#ifndef DYNAMIC_GRAPH_SIGNAL_TIME_DEPENDENT_H
# define DYNAMIC_GRAPH_SIGNAL_TIME_DEPENDENT_H
# include <dynamic-graph/signal.h>
# include <dynamic-graph/time-dependency.h>

namespace dynamicgraph
12
{
Thomas Moulard's avatar
Thomas Moulard committed
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*!  \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.
  */
  template< class T,class Time >
    class SignalTimeDependent
    : public virtual Signal<T,Time>
    , public TimeDependency<Time>
  {
26

Thomas Moulard's avatar
Thomas Moulard committed
27
    //TimeDependency<Time> timeDependency;
28

Thomas Moulard's avatar
Thomas Moulard committed
29
30
31
32
33
34
35
  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
36

Thomas Moulard's avatar
Thomas Moulard committed
37
    virtual ~SignalTimeDependent  () {}
Thomas Moulard's avatar
Thomas Moulard committed
38

Thomas Moulard's avatar
Thomas Moulard committed
39
40
    inline const T& operator () ( const Time& t1 ){ return access(t1); }
    const T& access( const Time& t1 );
Thomas Moulard's avatar
Thomas Moulard committed
41

Thomas Moulard's avatar
Thomas Moulard committed
42
43
44
    virtual void addDependency( const SignalBase<Time>& signal ) ;
    virtual void removeDependency( const SignalBase<Time>& signal ) ;
    virtual void clearDependencies  ();
45

Thomas Moulard's avatar
Thomas Moulard committed
46
47
    std::ostream& writeGraph(std::ostream &os) const
      { return os;}
48

Thomas Moulard's avatar
Thomas Moulard committed
49
50
51
52
    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 ); }
53

Thomas Moulard's avatar
Thomas Moulard committed
54

Thomas Moulard's avatar
Thomas Moulard committed
55
56
57
    virtual bool needUpdate( const Time& t ) const;
    virtual void setPeriodTime( const Time& p ) ;
    virtual Time getPeriodTime  () const;
58

Thomas Moulard's avatar
Thomas Moulard committed
59
  };
60
61


Thomas Moulard's avatar
Thomas Moulard committed
62
  /* -------------------------------------------- */
63
64


Thomas Moulard's avatar
Thomas Moulard committed
65
66
67
  template < class T,class Time >
    SignalTimeDependent<T,Time>::
    SignalTimeDependent( std::string name )
68
    :Signal<T,Time>(name)
Thomas Moulard's avatar
Thomas Moulard committed
69
    ,TimeDependency<Time>(this)
70
71
    {}

Thomas Moulard's avatar
Thomas Moulard committed
72
73
74
75
  template< class T,class Time>
    SignalTimeDependent<T,Time>::
    SignalTimeDependent( const SignalArray_const<Time> & arr
			 ,std::string name )
76
77
78
79
    :Signal<T,Time>(name)
    ,TimeDependency<Time>(this,arr)
    {}

Thomas Moulard's avatar
Thomas Moulard committed
80
81
82
83
84
  template< class T,class Time>
    SignalTimeDependent<T,Time>::
    SignalTimeDependent( boost::function2<T&,T&,Time> t
			 ,const SignalArray_const<Time> & sig
			 ,std::string name )
85
86
    :Signal<T,Time>(name)
    ,TimeDependency<Time>(this,sig)
87
    { this->setFunction(t); }
Thomas Moulard's avatar
Thomas Moulard committed
88

Thomas Moulard's avatar
Thomas Moulard committed
89
90
91
  template< class T,class Time>
    const T& SignalTimeDependent<T,Time>::
    access( const Time& t1 )
92
    {
Thomas Moulard's avatar
Thomas Moulard committed
93
      const bool up = TimeDependency<Time>::needUpdate(t1);
94
95
      //SignalBase<Time>::setReady(false);

Thomas Moulard's avatar
Thomas Moulard committed
96
97
      /*       std::cout << "Time before: "<< signalTime << " -- "   */
      /*   		<< t1<< "  -> Up: "<<up <<std::endl ;   */
98
99
100
101
      if( up )
	{
	  TimeDependency<Time>::lastAskForUpdate = false;
	  const T& Tres = Signal<T,Time>::access(t1);
Thomas Moulard's avatar
Thomas Moulard committed
102
	  SignalBase<Time>::setReady(false);
103
104
	  return Tres;
	}
Thomas Moulard's avatar
Thomas Moulard committed
105
      else { return Signal<T,Time>::accessCopy (); }
106
107
108
    }


Thomas Moulard's avatar
Thomas Moulard committed
109
110
111
  template< class T,class Time>
    void SignalTimeDependent<T,Time>::
    addDependency( const SignalBase<Time>& signal )
112
113
    { TimeDependency<Time>::addDependency(signal); }

Thomas Moulard's avatar
Thomas Moulard committed
114
115
116
  template< class T,class Time>
    void SignalTimeDependent<T,Time>::
    removeDependency( const SignalBase<Time>& signal )
117
118
    { TimeDependency<Time>::removeDependency(signal); }

Thomas Moulard's avatar
Thomas Moulard committed
119
120
121
122
  template< class T,class Time>
    void SignalTimeDependent<T,Time>::
    clearDependencies  ()
    { TimeDependency<Time>::clearDependency (); }
123

Thomas Moulard's avatar
Thomas Moulard committed
124
125
126
127
128
129
  template< class T,class Time>
    bool SignalTimeDependent<T,Time>::
    needUpdate( const Time& t ) const
    {
      return TimeDependency<Time>::needUpdate(t);
    }
130

Thomas Moulard's avatar
Thomas Moulard committed
131
  template< class T,class Time>
132
    void SignalTimeDependent<T,Time>::
Thomas Moulard's avatar
Thomas Moulard committed
133
    setPeriodTime( const Time& p )
134
    { TimeDependency<Time>::setPeriodTime( p ); }
Thomas Moulard's avatar
Thomas Moulard committed
135
136
137
138
  template< class T,class Time>
    Time SignalTimeDependent<T,Time>::
    getPeriodTime  ()  const
    { return TimeDependency<Time>::getPeriodTime ();  }
139

Thomas Moulard's avatar
Thomas Moulard committed
140
} // end of namespace dynamicgraph
141

Thomas Moulard's avatar
Thomas Moulard committed
142
#endif //! DYNAMIC_GRAPH_SIGNAL_TIME_DEPENDENT_H