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