dynamic-graph  4.4.3
Dynamic graph library
signal-ptr.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_PTR_H
7 #define DYNAMIC_GRAPH_SIGNAL_PTR_H
8 
9 #include <dynamic-graph/exception-signal.h>
10 #include <dynamic-graph/signal.h>
11 
12 namespace dynamicgraph {
26 template <class T, class Time>
27 class SignalPtr : public virtual Signal<T, Time> {
28  public:
30 
31  protected:
32  Signal<T, Time> *signalPtr;
33  bool modeNoThrow;
34  SignalBase<Time> *abstractTransmitter;
35  T *transmitAbstractData;
36 
37  inline bool autoref() const { return signalPtr == this; }
38 
39  public: /* --- CONSTRUCTORS --- */
40  SignalPtr(Signal<T, Time> *ptr, std::string name = "")
41  : Signal<T, Time>(name),
42  signalPtr(ptr),
43  modeNoThrow(false),
44  abstractTransmitter(NULL) {}
45 
46  virtual ~SignalPtr() { signalPtr = NULL; }
47 
48  public:
49  /* --- PLUG-IN OPERATION --- */
50  Signal<T, Time> *getPtr(); // throw
51  const Signal<T, Time> *getPtr() const; // throw
52  virtual void plug(SignalBase<Time> *ref);
53 
54  virtual void unplug() { plug(NULL); }
55 
56  virtual bool isPlugged() const { return (NULL != signalPtr); }
57  virtual SignalBase<Time> *getPluged() const { return signalPtr; }
58  virtual const Time &getTime() const;
59 
60  /* Equivalent operator-like definitions. */
61  inline Signal<T, Time> *operator->() { return getPtr(); }
62  inline const Signal<T, Time> *operator->() const { return getPtr(); }
63  inline Signal<T, Time> &operator*() { return *getPtr(); }
64  inline const Signal<T, Time> &operator*() const { return *getPtr(); }
65  inline operator bool() const { return isPlugged(); }
66 
67  public: /* --- INHERITANCE --- */
68  virtual bool needUpdate(const Time &t) const;
69  virtual std::ostream &writeGraph(std::ostream &os) const;
70  virtual std::ostream &display(std::ostream &os) const;
71 
72  /* For compatibility, .access () is equivalent to ->access (). For explicit
73  * pointer dereference :
74  * Prefere -> () to ()
75  */
76  virtual const T &operator()(const Time &t);
77  /* Similarly, Prefere ->access to .access
78  */
79  virtual const T &access(const Time &t);
80  virtual const T &accessCopy() const;
81 
82  inline void setConstantDefault(const T &t) {
84  modeNoThrow = true;
85  }
86  virtual inline void setConstantDefault() { setConstantDefault(accessCopy()); }
87  inline void unsetConstantDefault() { modeNoThrow = false; }
88 
89  public: /* --- INHERITANCE --- */
90  /* SignalPtr could be used as a classical signal, through the normal
91  * setting functions. The behavior is to plugged the signalPtr on
92  * the classical mother Signal layer of the object.
93  */
94  virtual void setConstant(const T &t) {
95  plug(this);
97  }
98  virtual void setReference(const T *t,
99  typename Signal<T, Time>::Mutex *m = NULL) {
100  plug(this);
102  }
103  virtual void setFunction(boost::function2<T &, T &, Time> t,
104  typename Signal<T, Time>::Mutex *m = NULL) {
105  plug(this);
107  }
108 
109  /* template< class Provider > */
110  /* void setFunction( T& (Provider::*fun)(Time,T&),Provider& obj, */
111  /* boost::try_mutex *mutexref=NULL ) */
112  /* { plug(this); Signal<T,Time>::setFunction(fun,obj,mutexref); } */
113 
114  virtual inline Signal<T, Time> &operator=(const T &t) {
115  setConstant(t);
116  return *this;
117  }
118 
119  virtual std::ostream &displayDependencies(std::ostream &os,
120  const int depth = -1,
121  std::string space = "",
122  std::string next1 = "",
123  std::string next2 = "") const;
124 
125  protected: // Interdiction of the rest of the heritage
127  virtual void addDependency() {}
129  virtual void removeDependency() {}
130  virtual void clearDependencies() {}
131 };
132 
133 } // end of namespace dynamicgraph
134 
135 #include <dynamic-graph/signal-ptr.t.cpp>
136 #endif
The base class for signals: not to be used as such.
Definition: signal-base.h:27
This is the only type of signal that can be plugged to, using the plug () command.
Definition: signal-ptr.h:27
Signals link I/O ports of entities. They can be constant-valued signals, or copy the value of a heap ...
Definition: signal.h:41