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