GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/dynamic-graph/signal-ptr.h Lines: 23 26 88.5 %
Date: 2023-03-15 12:04:10 Branches: 2 4 50.0 %

Line Branch Exec Source
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 {
13
/// \ingroup dgraph
14
///
15
/// \brief This is the only type of signal that can be plugged to,
16
/// using the plug () command.
17
///
18
/// In that sense, when plugged into, it acts as a "pointer" to the
19
/// input signal, hence the name. Operator -> is also overloaded and
20
/// can be used to access the pointed signal.
21
///
22
/// If the signal provided as a parameter of the plug operation
23
/// cannot be casted in type T, but is compatible then the class
24
/// holds a reference to an abstract object.
25
///
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
44
  inline bool autoref() const { return signalPtr == this; }
39
40
 public: /* --- CONSTRUCTORS --- */
41
19
  SignalPtr(Signal<T, Time> *ptr, std::string name = "")
42
      : Signal<T, Time>(name),
43
        signalPtr(ptr),
44
        modeNoThrow(false),
45
        transmitAbstract(false),
46
19
        abstractTransmitter(NULL) {}
47
48
18
  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
2
  virtual void unplug() { plug(NULL); }
59
60
40
  virtual bool isPlugged() const { return (NULL != signalPtr); }
61
1
  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
4
  inline void setConstantDefault(const T &t) {
88
4
    Signal<T, Time>::setConstant(t);
89
4
    modeNoThrow = true;
90
  }
91
1
  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
4
  virtual void setConstant(const T &t) {
102
4
    plug(this);
103
4
    Signal<T, Time>::setConstant(t);
104
  }
105
1
  virtual void setReference(const T *t,
106
                            typename Signal<T, Time>::Mutex *m = NULL) {
107
2
    plug(this);
108
2
    Signal<T, Time>::setReference(t, m);
109
  }
110
1
  virtual void setFunction(boost::function2<T &, T &, Time> t,
111
                           typename Signal<T, Time>::Mutex *m = NULL) {
112
2
    plug(this);
113
2
    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
2
  virtual inline Signal<T, Time> &operator=(const T &t) {
122
2
    setConstant(t);
123
2
    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  //! DYNAMIC_GRAPH_SIGNAL_PTR_H