signal.hpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Ifdefs
12 *****************************************************************************/
13 
14 #ifndef ECL_SIGSLOTS_LITE_SIGNAL_HPP_
15 #define ECL_SIGSLOTS_LITE_SIGNAL_HPP_
16 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include "slot.hpp"
22 #include "errors.hpp"
23 
24 /*****************************************************************************
25 ** Namespaces
26 *****************************************************************************/
27 
28 namespace ecl {
29 namespace lite {
30 
31 /*****************************************************************************
32 ** Classes
33 *****************************************************************************/
62 template <typename Data=void, unsigned int Capacity = 1>
63 class Signal {
64 public:
68  Signal() {
69  for ( unsigned int i = 0; i < Capacity; ++i) {
70  slots[i] = NULL;
71  }
72  }
89  for (unsigned int i = 0; i < Capacity; ++i ) {
90  if ( slots[i] == NULL ) {
91  slots[i] = &slot;
93  }
94  }
95  // if we reach here, we've run out of resources.
97  }
103  unsigned int capacity() const { return Capacity; }
109  unsigned int stored() const {
110  unsigned int i;
111  for (i = 0; i < Capacity; ++i ) {
112  if ( slots[i] == NULL ) {
113  break;
114  }
115  }
116  return i;
117 
118  }
119 
125  void emit(Data data) const {
126  for (unsigned int i = 0; i < Capacity; ++i ) {
127  if ( slots[i] == NULL ) {
128  break;
129  } else {
130  slots[i]->execute(data);
131  }
132  }
133  }
134 
135 private:
136  sigslots::SlotBase<Data> *slots[Capacity];
137 };
138 
147 template <unsigned int Capacity>
148 class Signal<void,Capacity> {
149 public:
153  Signal() {
154  for ( unsigned int i = 0; i < Capacity; ++i) {
155  slots[i] = NULL;
156  }
157  }
173  sigslots::Error connect(sigslots::SlotBase<void> &slot) {
174  for (unsigned int i = 0; i < Capacity; ++i ) {
175  if ( slots[i] == NULL ) {
176  slots[i] = &slot;
177  return sigslots::Error(sigslots::NoError);
178  }
179  }
180  // if we reach here, we've run out of resources.
181  return sigslots::Error(sigslots::OutOfResourcesError);
182  }
188  unsigned int capacity() const { return Capacity; }
189 
195  unsigned int stored() const {
196  unsigned int i;
197  for (i = 0; i < Capacity; ++i ) {
198  if ( slots[i] == NULL ) {
199  break;
200  }
201  }
202  return i;
203 
204  }
208  void emit() const {
209  for (unsigned int i = 0; i < Capacity; ++i ) {
210  if ( slots[i] == NULL ) {
211  break;
212  } else {
213  slots[i]->execute();
214  }
215  }
216  }
217 
218 private:
219  sigslots::SlotBase<void> *slots[Capacity];
220 };
221 
222 
223 } // namespace lite
224 } // namespace ecl
225 
226 #endif /* ECL_SIGSLOTS_LITE_SIGNAL_HPP_ */
ecl::lite::Signal::connect
sigslots::Error connect(sigslots::SlotBase< Data > &slot)
Connect the signal to the specified slot.
Definition: signal.hpp:108
errors.hpp
Error handling types and classes for lite sigslots.
ecl::lite::sigslots::NoError
@ NoError
Definition: errors.hpp:47
ecl::lite::sigslots::OutOfResourcesError
@ OutOfResourcesError
Definition: errors.hpp:49
ecl::lite::Signal::Signal
Signal()
Initialise the storage.
Definition: signal.hpp:88
slot.hpp
Simple slots - these are actually all hidden from the user.
ecl::lite::sigslots::SlotBase
Parent slot class with the common, publicly exposed interface.
Definition: slot.hpp:61
ecl::lite::sigslots::Error
Extends the generic ecl error handler with some time specific error strings.
Definition: errors.hpp:53
ecl::lite::Signal::slots
sigslots::SlotBase< Data > * slots[Capacity]
Definition: signal.hpp:156
ecl::lite::Signal::stored
unsigned int stored() const
The current number of connections stored.
Definition: signal.hpp:129
ecl::lite::Signal::emit
void emit(Data data) const
Signal slots with the specified data.
Definition: signal.hpp:145
ecl
ecl::lite::Signal::capacity
unsigned int capacity() const
The reserved capacity for this signaller.
Definition: signal.hpp:123


ecl_sigslots_lite
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:13:56