test/sigslots.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 
14 #include <iostream>
15 #include <gtest/gtest.h>
17 #include "../../include/ecl/sigslots/signal.hpp"
18 #include "../../include/ecl/sigslots/slot.hpp"
19 
20 /*****************************************************************************
21 ** Using
22 *****************************************************************************/
23 
25 using ecl::Signal;
26 using ecl::Slot;
27 
31 /*****************************************************************************
32 ** Namespaces
33 *****************************************************************************/
34 
35 namespace ecl {
36 namespace sigslots {
37 namespace tests {
38 
39 /*****************************************************************************
40 ** Classes
41 *****************************************************************************/
42 
43 class A {
44 public:
45  A() : f_count(0), g_count(0) {}
46 
47  void f(const int &i) {
48 // std::cout << " Member function with argument: " << i << std::endl;;
49  f_count++;
50  }
51  void g() {
52  g_count++;
53 // std::cout << " Member function" << std::endl;;
54  }
55  int f_count, g_count;
56 };
57 
58 /*****************************************************************************
59 ** Functions
60 *****************************************************************************/
61 
62 static int f_count = 0;
63 static int g_count = 0;
64 
65 void f(const int &i)
66 {
67  f_count++;
68 // std::cout << " Global function with argument: " << i << std::endl;;
69 }
70 
71 void g()
72 {
73  g_count++;
74 // std::cout << " Global function" << std::endl;;
75 }
76 
77 } // namespace tests
78 } // namespace signals
79 } // namespace ecl
80 
81 /*****************************************************************************
82 ** Using
83 *****************************************************************************/
84 
85 using namespace ecl::sigslots::tests;
86 
90 /*****************************************************************************
91 ** Tests
92 *****************************************************************************/
93 
94 TEST(SigSlotsTests, voidSigSlots) {
95 
96  A a;
97  Signal<> sig_void;
98 
99  Slot<> slot_void0(g,"void_test");
100  Slot<> slot_void1(&A::g,a);
101 
102  sig_void.connect("void_test");
103 // slot_void0.connect("void_test");
104  slot_void1.connect("void_test");
105 
106  sig_void.emit();
107 
108 // ecl::SigSlotsManager<>::printStatistics();
109 
110 // std::cout << "a.g_count: " << a.g_count << std::endl;
111 // std::cout << "g_count: " << g_count << std::endl;
112  EXPECT_EQ(1,a.g_count);
113  EXPECT_EQ(1,g_count);
114 }
115 
116 TEST(SigSlotsTests, relay) {
117 
118  A a;
119  Signal<> sig_starter;
120  Signal<> sig_relay;
121 
122  Slot<> slot_relayed_goal(g);
123 
124  sig_starter.connect("relay");
125  sig_relay.connectAsSlot("relay");
126  sig_relay.connect("final_goal");
127  slot_relayed_goal.connect("final_goal");
128 
129  sig_starter.emit();
130 
131 // std::cout << "g_count: " << g_count << std::endl;
132  EXPECT_EQ(2,g_count);
133 }
134 
135 TEST(SigSlotsTests,dataSigSlots) {
136 
137  A a;
138  Signal<const int&> sig_int;
139  Slot<const int&> slot_int0(f);
140  Slot<const int&> slot_int1(&A::f,a);
141 
142  sig_int.connect("int_test");
143  slot_int0.connect("int_test");
144  slot_int1.connect("int_test");
145 
146  sig_int.emit(4);
147 // std::cout << "a.f_count: " << a.f_count << std::endl;
148 // std::cout << "f_count: " << f_count << std::endl;
149  EXPECT_EQ(1,a.f_count);
150  EXPECT_EQ(1,f_count);
151 }
152 
153 TEST(SigSlotsTests, disconnect) {
154 
155  Signal<> signal;
156  Slot<> slot(g);
157  signal.connect("disconnect");
158  slot.connect("disconnect");
159  signal.emit();
160  slot.disconnect();
161  Signal<const int&> data_signal;
162  Slot<const int&> data_slot(f);
163  data_signal.connect("disconnect"); // Each data type has its own 'namespace' per se, so this doesn't clash with the above.
164  data_slot.connect("disconnect");
165  data_signal.emit(1);
166  data_slot.disconnect();
167  data_signal.emit(1); // Shouldn't increment
168 
169  EXPECT_EQ(3,g_count);
170  EXPECT_EQ(2,f_count);
171 }
172 
173 /*****************************************************************************
174 ** Main program
175 *****************************************************************************/
176 
177 int main(int argc, char **argv) {
178 
179  testing::InitGoogleTest(&argc,argv);
180  return RUN_ALL_TESTS();
181 }
void g(const int &i)
void connectAsSlot(const std::string &topic)
Connect as a slot, with the emit function loaded.
Definition: signal.hpp:113
int main(int argc, char **argv)
void connect(const std::string &topic)
Make a connection to the specified topic.
Definition: slot.hpp:152
void disconnect()
Disconnect the slot from all topics.
Definition: slot.hpp:158
TEST(SigSlotsTests, voidSigSlots)
void f(int i) ecl_debug_throw_decl(StandardException)
Signalling component of a callback system.
Definition: signal.hpp:47
void connect(const std::string &topic)
Make a connection to the specified topic.
Definition: signal.hpp:104
void emit(Data data)
The primary purpose of the signal, to emit!
Definition: signal.hpp:128
Function loading component of a callback system.
Definition: slot.hpp:48


ecl_sigslots
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:48