sigslots.cpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Includes
13 *****************************************************************************/
14 
15 #include <iostream>
16 #include "../../include/ecl/sigslots_lite.hpp"
17 
18 /*****************************************************************************
19 ** Foo
20 *****************************************************************************/
24 class Foo : public ecl::lite::MemberSlots<const char*,Foo>,
25  public ecl::lite::MemberSlots<void,Foo,2>
26 {
27 public:
28  Foo() {}
29 
30  void f(const char* str) {
31  std::cout << "Foo::f() " << str << std::endl;
32  }
33  void h() {
34  std::cout << "Foo::h()" << std::endl;
35  }
36 };
37 
38 void f(const char* str) {
39  std::cout << "f(char*) " << str << std::endl;
40 }
41 
42 void g(const char* str) {
43  std::cout << "g(char*) " << str << std::endl;
44 }
45 
46 void h() {
47  std::cout << "h()" << std::endl;
48 }
49 
50 /*****************************************************************************
51 ** Static Variables
52 *****************************************************************************/
53 
54 template<>
56 
57 template<>
58 const unsigned int ecl::lite::GlobalSlots<void>::capacity = 2;
59 
64 /*****************************************************************************
65 ** Main
66 *****************************************************************************/
67 
68 int main(int argc, char **argv) {
69 
70  Foo foo;
71 
72  std::cout << std::endl;
73  std::cout << "***********************************************************" << std::endl;
74  std::cout << " Data Sigslots" << std::endl;
75  std::cout << "***********************************************************" << std::endl;
76  std::cout << std::endl;
77 
78  ecl::lite::Signal<const char*,2> dual_slot_signal;
79  ecl::lite::Signal<const char*> lone_signal1, lone_signal2;
80  // koenig lookup means a simple connect(dual_slot_signal,f) also works
81  ecl::lite::connect(dual_slot_signal,f);
82  ecl::lite::connect(dual_slot_signal,g);
83  dual_slot_signal.emit("one signal, two global slots");
84  std::cout << std::endl;
85 
86  ecl::lite::connect(lone_signal1,&Foo::f,foo);
87  lone_signal1.emit("direct member slot connection");
88  std::cout << std::endl;
89 
90  ecl::lite::connect(lone_signal2,&Foo::f,foo);
91  lone_signal1.emit("multiple signals, one slot connection - I");
92  lone_signal2.emit("multiple signals, one slot connection - II");
93  std::cout << std::endl;
94 
95  std::cout << std::endl;
96  std::cout << "***********************************************************" << std::endl;
97  std::cout << " Void Sigslots" << std::endl;
98  std::cout << "***********************************************************" << std::endl;
99  std::cout << std::endl;
100 
101  ecl::lite::Signal<void> void_signal, member_void_signal;
102  connect(member_void_signal,&Foo::h, foo);
103  connect(void_signal,h);
104  void_signal.emit();
105  member_void_signal.emit();
106 
107  std::cout << std::endl;
108  std::cout << "***********************************************************" << std::endl;
109  std::cout << " Storage" << std::endl;
110  std::cout << "***********************************************************" << std::endl;
111  std::cout << std::endl;
112 
113  std::cout << "GlobalSlots<const char*> Stored : " << ecl::lite::global_slots_stored<const char*>() << std::endl;
114  std::cout << "GlobalSlots<const char*> Capacity: " << ecl::lite::global_slots_capacity<const char*>() << std::endl;
115  std::cout << std::endl;
116  std::cout << "GlobalSlots<void> Stored : " << ecl::lite::global_slots_stored<void>() << std::endl;
117  std::cout << "GlobalSlots<void> Capacity: " << ecl::lite::global_slots_capacity<void>() << std::endl;
118  std::cout << std::endl;
119 
120  std::cout << "MemberSlots<const char*> Stored(foo) : " << ecl::lite::member_slots_stored<const char*>(foo) << std::endl;
121  std::cout << "MemberSlots<const char*> Capacity(Foo): " << ecl::lite::member_slots_capacity<const char*>(foo) << std::endl;
122  std::cout << std::endl;
123  std::cout << "MemberSlots<void> Stored(foo) : " << ecl::lite::member_slots_stored<void>(foo) << std::endl;
124  std::cout << "MemberSlots<void> Capacity(Foo): " << ecl::lite::member_slots_capacity<void>(foo) << std::endl;
125  std::cout << std::endl;
126 
127  ecl::lite::Signal<const char*,2> signal_storage_test;
128  std::cout << "Incrementally adding slots to signal<const char*,2>" << std::endl;
129  std::cout << " capacity: " << signal_storage_test.capacity() << std::endl;
130  std::cout << " stored : " << signal_storage_test.stored() << std::endl;
131  connect(signal_storage_test,f);
132  std::cout << " stored : " << signal_storage_test.stored() << std::endl;
133  connect(signal_storage_test,g);
134  std::cout << " stored : " << signal_storage_test.stored() << std::endl;
135  std::cout << std::endl;
136 
137  std::cout << std::endl;
138  std::cout << "***********************************************************" << std::endl;
139  std::cout << " Error Handling" << std::endl;
140  std::cout << "***********************************************************" << std::endl;
141  std::cout << std::endl;
142 
144  std::cout << "Adding two slots to a single slot capacity signal." << std::endl;
145  ecl::lite::Signal<const char*> one_shot_signal;
146  std::cout << " Connecting a first slot" << std::endl;
147  error = connect(one_shot_signal,f);
148  if ( error.flag() != ecl::lite::sigslots::NoError ) {
149  std::cout << " " << error.what() << std::endl;
150  }
151  std::cout << " Connecting a second slot" << std::endl;
152  error = connect(one_shot_signal,f);
153  if ( error.flag() != ecl::lite::sigslots::NoError ) {
154  std::cout << " " << error.what() << std::endl;
155  }
156  std::cout << std::endl;
157 
158  std::cout << "Adding two slots to a single slot capacity MemberSlots interface." << std::endl;
159  ecl::lite::Signal<const char*> two_shot_signal;
160  Foo foo_new; // 0 slots stored in MemberSlots<char*>
161  std::cout << " Connecting a first slot" << std::endl;
162  error = connect(two_shot_signal, &Foo::f, foo_new); // 1 slot stored in MemberSlots<char*>
163  if ( error.flag() != ecl::lite::sigslots::NoError ) {
164  std::cout << " " << error.what() << std::endl;
165  }
166  std::cout << " Connecting a second slot" << std::endl;
167  error = connect(two_shot_signal, &Foo::f, foo_new); // 2nd slot, when only 1 declared, fail!
168  if ( error.flag() != ecl::lite::sigslots::NoError ) {
169  std::cout << " " << error.what() << std::endl;
170  }
171 
172  std::cout << std::endl;
173  std::cout << "***********************************************************" << std::endl;
174  std::cout << " Passed" << std::endl;
175  std::cout << "***********************************************************" << std::endl;
176  std::cout << std::endl;
177 
178  return 0;
179 }
friend sigslots::Error connect(Signal< Data_, Capacity_ > &signal, void(FunctionClass_::*f)(Data_), FunctionClass_ &o)
A simple signal class.
Definition: managers.hpp:32
This is the global slot interface.
Definition: managers.hpp:191
sigslots::Error connect(Signal< Data, Capacity > &signal, void(FunctionClass::*f)(Data), FunctionClass &o)
Convenience method to connect signal with member function.
Definition: connect.hpp:89
virtual sigslots::ErrorFlag flag() const
The flag identifying the error identified with this error handler.
Definition: errors.hpp:53
void emit(Data data) const
Signal slots with the specified data.
Definition: signal.hpp:125
Extends the generic ecl error handler with some time specific error strings.
Definition: errors.hpp:41
int main(int argc, char **argv)
Definition: sigslots.cpp:68
This is the member slot interface, inheritable by classes.
Definition: managers.hpp:91
unsigned int stored() const
The current number of connections stored.
Definition: signal.hpp:109
unsigned int capacity() const
The reserved capacity for this signaller.
Definition: signal.hpp:103
virtual const char * what() const
A simple string verbosely representing the error that is handled.
Definition: errors.hpp:59


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