aGrUM  0.13.2
signaler0.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
29 #ifndef GUM_SIGNALER0_H
30 #define GUM_SIGNALER0_H
31 #include <functional>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/core/list.h>
36 
37 namespace gum {
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 
41  namespace __sig__ {
42 
43  class IConnector0 {
44  public:
45  virtual ~IConnector0(){};
46 
47  virtual Listener* target() const = 0;
48  virtual void notify(const void*) = 0;
49  virtual IConnector0* clone() = 0;
50  virtual IConnector0* duplicate(Listener* target) = 0;
51  };
52 
53  class BasicSignaler0 : public ISignaler {
54  protected:
55  typedef std::vector< IConnector0* > ConnectorList;
56 
57  BasicSignaler0();
58 
59  BasicSignaler0(const BasicSignaler0& s);
60 
61  public:
62  virtual ~BasicSignaler0();
63 
64  bool hasListener();
65 
66  void detach(Listener* target);
67 
68  protected:
69  friend class Listener;
70 
71  void _detachFromTarget(Listener* target);
72 
73  void _duplicateTarget(const Listener* oldtarget, Listener* newtarget);
74 
75  ConnectorList _connectors;
76 
77  private:
78  std::function< bool(IConnector0* el) > __find_target(const gum::Listener* l);
79  };
80 
81  template < class TargetClass >
82  class Connector0 : public IConnector0 {
83  public:
84  Connector0();
85 
86  Connector0(TargetClass* target, void (TargetClass::*action)(const void*));
87 
88  Connector0(const Connector0< TargetClass >* src);
89 
90  virtual ~Connector0();
91 
92  virtual IConnector0* clone();
93 
94  virtual IConnector0* duplicate(Listener* target);
95 
96  virtual void notify(const void* src);
97 
98  virtual Listener* target() const;
99 
100  private:
101  TargetClass* __target;
102  void (TargetClass::*__action)(const void*);
103  };
104 
105  } // namespace __sig__
106 
107 #endif // DOXYGEN_SHOULD_SKIP_THIS
108 
116  class Signaler0 : public __sig__::BasicSignaler0 {
117  public:
118  Signaler0() { GUM_CONSTRUCTOR(Signaler0); }
119 
120  Signaler0(const Signaler0& s) : __sig__::BasicSignaler0(s) {
121  GUM_CONS_CPY(Signaler0);
122  }
123 
124  virtual ~Signaler0() { GUM_DESTRUCTOR(Signaler0); }
125 
126  template < class TargetClass >
127  void attach(TargetClass* target, void (TargetClass::*action)(const void*)) {
128  __sig__::Connector0< TargetClass >* conn =
129  new __sig__::Connector0< TargetClass >(target, action);
130  this->_connectors.push_back(conn);
131  target->attachSignal__(this);
132  }
133 
134  INLINE void operator()(const void* src) {
135  for (const auto el : _connectors) {
136  el->notify(src);
137  }
138  }
139  };
140 
141 } // namespace gum
142 
144 #define GUM_EMIT0(signal) this->signal(this)
145 
146 // Template implementation should always be in headers
148 
149 #ifndef GUM_NO_INLINE
151 #endif // GUM_NO_INLINE
152 
153 #endif // GUM_SIGNALER0_H
Class of listener.
Class of signalers.
virtual ~Signaler0()
Definition: signaler0.h:124
void attach(TargetClass *target, void(TargetClass::*action)(const void *))
Definition: signaler0.h:127
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Classes of signaler.
Generic class for manipulating lists.
class for signal with 0 args
Definition: signaler0.h:116
Signaler0(const Signaler0 &s)
Definition: signaler0.h:120
INLINE void operator()(const void *src)
Definition: signaler0.h:134
Every class who would catch signal from signaler should derive from Listener.
Definition: listener.h:72