managers.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_SIGSLOTS_LITE_MANAGERS_HPP_
13 #define ECL_SIGSLOTS_LITE_MANAGERS_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include "slot.hpp"
20 
21 /*****************************************************************************
22 ** Namespaces
23 *****************************************************************************/
24 
25 namespace ecl {
26 namespace lite {
27 
28 /*****************************************************************************
29 ** Forward Declarations
30 *****************************************************************************/
31 
32 template <typename Data, unsigned int Capacity> class Signal;
33 
34 /*****************************************************************************
35 ** Managers
36 *****************************************************************************/
37 
38 namespace sigslots {
42 template <typename Data, typename FunctionClass>
44 public:
45  virtual unsigned int stored() const { return 0; }
46  virtual unsigned int capacity() const { return 0; }
47  virtual sigslots::MemberSlot<Data,FunctionClass>* addSlot(void (FunctionClass::*func)(Data), FunctionClass &instance) = 0;
48 };
49 
53 template <typename FunctionClass>
54 class MemberSlotsBase<void,FunctionClass> {
55 public:
56  virtual unsigned int stored() const { return 0; }
57  virtual unsigned int capacity() const { return 0; }
58  virtual sigslots::MemberSlot<void,FunctionClass>* addSlot(void (FunctionClass::*func)(), FunctionClass &instance) = 0;
59 };
60 
61 } // namespace sigslots
62 
63 /******************************************
64 ** Member Manager
65 *******************************************/
90 template <typename Data, typename FunctionClass, unsigned int Capacity = 1>
91 class MemberSlots : public sigslots::MemberSlotsBase<Data,FunctionClass> {
92 public:
93  /*********************
94  ** Friends
95  **********************/
96  // allow connect to use addSlot
97  template <typename Data_, unsigned int Capacity_, typename FunctionClass_>
98  friend sigslots::Error connect(Signal<Data_,Capacity_> &signal, void(FunctionClass_::*f)(Data_), FunctionClass_ &o);
99 
100  // needs access to stored()
101  template <typename Data_, typename FunctionClass_>
102  friend unsigned int member_slots_stored(const FunctionClass_ &object);
103 
104  // needs access to capacity()
105  template <typename Data_, typename FunctionClass_>
106  friend unsigned int member_slots_capacity(const FunctionClass_ &object);
107 
108 
109 protected:
110  MemberSlots() : size(0) {};
111 
112 private:
118  unsigned int stored() const { return size; }
124  unsigned int capacity() const { return Capacity; }
125 
135  sigslots::MemberSlot<Data,FunctionClass>* addSlot(void (FunctionClass::*func)(Data), FunctionClass &instance) {
136  for ( unsigned int i = 0; i < size; ++i ) {
137  if ( func == slots[i].member_function ) {
138  return &(slots[i]);
139  }
140  }
141  if ( size < Capacity ) {
142  slots[size] = sigslots::MemberSlot<Data,FunctionClass>(func,instance);
143  ++size;
144  return &(slots[size-1]);
145  } else {
146  return NULL;
147  }
148  }
149 
150  unsigned int size;
152 };
153 
154 /******************************************
155 ** Global Manager
156 *******************************************/
157 
190 template <typename Data, typename Dummy = int>
191 class GlobalSlots {
192 public:
193  static const unsigned int capacity;
195  /*********************
196  ** Friends
197  **********************/
198  // needs access to addSlot()
199  template <typename Data_, unsigned int Capacity_>
200  friend sigslots::Error connect(Signal<Data_, Capacity_> &signal, void (*function)(Data_));
201 
202  // needs access to stored()
203  template <typename Data_>
204  friend unsigned int global_slots_stored();
205 
206 private:
218  static unsigned int stored(const bool increment = false) {
219  static unsigned int stored_slots = 0;
220  if ( increment ) { ++stored_slots; }
221  return stored_slots;
222  }
231  static sigslots::GlobalSlot<Data>* addSlot(void (*func)(Data)) {
232  unsigned int size = stored();
233  static sigslots::GlobalSlot<Data> slots[capacity];
234  for ( unsigned int i = 0; i < size; ++i ) {
235  if ( func == slots[i].global_function ) {
236  return &(slots[i]);
237  }
238  }
239  if ( size < capacity ) {
240  slots[size] = sigslots::GlobalSlot<Data>(func);
241  bool inc = true;
242  stored(inc); // increment the number of stored slots variable
243  return &(slots[size]);
244  } else {
245  return NULL;
246  }
247  }
248 };
249 
250 /*****************************************************************************
251 ** Specialisations
252 *****************************************************************************/
256 template <typename FunctionClass, unsigned int Capacity>
257 class MemberSlots<void, FunctionClass, Capacity> : public sigslots::MemberSlotsBase<void,FunctionClass> {
258 public:
259  /*********************
260  ** Friends
261  **********************/
262  // allow connect to use addSlot
263  template <unsigned int Capacity_, typename FunctionClass_>
264  friend sigslots::Error connect(Signal<void,Capacity_> &signal, void(FunctionClass_::*f)(void), FunctionClass_ &o);
265  // needs access to stored()
266  template <typename Data_, typename FunctionClass_>
267  friend unsigned int member_slots_stored(const FunctionClass_ &object);
268 
269  // needs access to capacity()
270  template <typename Data_, typename FunctionClass_>
271  friend unsigned int member_slots_capacity(const FunctionClass_ &object);
272 
273 protected:
274  MemberSlots() : size(0) {};
275 
276 private:
282  unsigned int stored() const { return size; }
283 
289  unsigned int capacity() const { return Capacity; }
290 
300  sigslots::MemberSlot<void,FunctionClass>* addSlot(void (FunctionClass::*func)(void), FunctionClass &instance) {
301  for ( unsigned int i = 0; i < size; ++i ) {
302  if ( func == slots[i].member_function ) {
303  return &(slots[i]);
304  }
305  }
306  if ( size < Capacity ) {
307  slots[size] = sigslots::MemberSlot<void,FunctionClass>(func,instance);
308  ++size;
309  return &(slots[size-1]);
310  } else {
311  return NULL;
312  }
313  }
314 
315  unsigned int size;
317 };
318 
324 template <typename Dummy>
325 class GlobalSlots<void, Dummy> {
326 public:
327  static const unsigned int capacity;
330  /*********************
331  ** Friends
332  **********************/
333  // needs access to addSlot()
334  template <unsigned int Capacity_>
335  friend sigslots::Error connect(Signal<void,Capacity_> &signal, void (*function)(void));
336 
337  // needs access to stored()
338  template <typename Data> friend unsigned int global_slots_stored();
339 
340 private:
352  static unsigned int stored(const bool increment = false) {
353  static unsigned int stored_slots = 0;
354  if ( increment ) { ++stored_slots; }
355  return stored_slots;
356  }
357 
366  static sigslots::GlobalSlot<void>* addSlot(void (*func)(void)) {
367  unsigned int size = stored();
368  static sigslots::GlobalSlot<void> slots[capacity];
369  for ( unsigned int i = 0; i < size; ++i ) {
370  if ( func == slots[i].global_function ) {
371  return &(slots[i]);
372  }
373  }
374  if ( size < capacity ) {
375  slots[size] = sigslots::GlobalSlot<void>(func);
376  bool inc = true;
377  stored(inc); // increment the number of stored slots variable
378  return &(slots[size]);
379  } else {
380  return NULL;
381  }
382  }
383 };
384 
385 
386 } // namespace lite
387 } // namespace ecl
388 
389 #endif /* ECL_SIGSLOTS_LITE_MANAGERS_HPP_ */
A simple signal class.
Definition: managers.hpp:32
A slot with global/static callback function.
Definition: slot.hpp:132
static unsigned int stored(const bool increment=false)
Number of slots currently stored.
Definition: managers.hpp:352
virtual unsigned int stored() const
Definition: managers.hpp:45
unsigned int capacity() const
The number of slots that can be attached to member functions.
Definition: managers.hpp:124
Used internally to retrieve info about members lots.
Definition: managers.hpp:43
static const unsigned int capacity
Number of global void functions that can be slotted.
Definition: managers.hpp:327
A slot with member callback function.
Definition: slot.hpp:52
This is the global slot interface.
Definition: managers.hpp:191
static sigslots::GlobalSlot< void > * addSlot(void(*func)(void))
Add a slot.
Definition: managers.hpp:366
ecl::lite::sigslots::Error connect(ecl::lite::Signal< Data, Capacity > &signal, MemberSlot< Data, FunctionClass > &slot)
Internal worker used to connect a signal with a slot.
Definition: connect.hpp:47
A slot with member callback function.
Definition: slot.hpp:110
static sigslots::GlobalSlot< Data > * addSlot(void(*func)(Data))
Add a slot.
Definition: managers.hpp:231
virtual sigslots::MemberSlot< Data, FunctionClass > * addSlot(void(FunctionClass::*func)(Data), FunctionClass &instance)=0
unsigned int member_slots_capacity(const FunctionClass &object)
Definition: utilities.hpp:44
Extends the generic ecl error handler with some time specific error strings.
Definition: errors.hpp:41
virtual unsigned int capacity() const
Definition: managers.hpp:46
This is the member slot interface, inheritable by classes.
Definition: managers.hpp:91
static const unsigned int capacity
Number of global functions of this type that can be slotted.
Definition: managers.hpp:193
A slot with global/static callback function.
Definition: slot.hpp:75
sigslots::MemberSlot< void, FunctionClass > * addSlot(void(FunctionClass::*func)(void), FunctionClass &instance)
Add a slot.
Definition: managers.hpp:300
unsigned int member_slots_stored(const FunctionClass &object)
Definition: utilities.hpp:38
unsigned int capacity() const
The number of slots that can be attached to member functions.
Definition: managers.hpp:289
static unsigned int stored(const bool increment=false)
Number of slots currently stored.
Definition: managers.hpp:218
unsigned int global_slots_stored()
Definition: utilities.hpp:29
sigslots::MemberSlot< Data, FunctionClass > * addSlot(void(FunctionClass::*func)(Data), FunctionClass &instance)
Add a slot.
Definition: managers.hpp:135
unsigned int stored() const
The number of slots stored.
Definition: managers.hpp:118
Simple slots - these are actually all hidden from the user.
unsigned int stored() const
The number of slots stored.
Definition: managers.hpp:282


ecl_sigslots_lite
Author(s): Daniel Stonier
autogenerated on Fri Jun 7 2019 21:52:46