Mercurial > hg > orthanc-stone
comparison UnitTestsSources/TestMessageBroker.cpp @ 1076:008dbc4ceb62 broker
removed LambdaCallable
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 17 Oct 2019 14:42:41 +0200 |
parents | 05b2e71ed145 |
children | f72d1ab42932 |
comparison
equal
deleted
inserted
replaced
1075:81b29bc7c3d4 | 1076:008dbc4ceb62 |
---|---|
100 // the connection is permanent; if we emit the same message again, the observer will be notified again | 100 // the connection is permanent; if we emit the same message again, the observer will be notified again |
101 testCounter = 0; | 101 testCounter = 0; |
102 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); | 102 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); |
103 ASSERT_EQ(0, testCounter); | 103 ASSERT_EQ(0, testCounter); |
104 } | 104 } |
105 | |
106 | |
107 | |
108 #if 0 /* __cplusplus >= 201103L*/ | |
109 | |
110 TEST(MessageBroker, TestLambdaSimpleUseCase) | |
111 { | |
112 MyObservable observable; | |
113 MyObserver* observer(new MyObserver); | |
114 | |
115 // create a permanent connection between an observable and an observer | |
116 observable.RegisterObserverCallback(new LambdaCallable<MyObservable::MyCustomMessage>(*observer, [&](const MyObservable::MyCustomMessage& message) {testCounter += 2 * message.payload_;})); | |
117 | |
118 testCounter = 0; | |
119 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); | |
120 ASSERT_EQ(24, testCounter); | |
121 | |
122 // delete the observer and check that the callback is not called anymore | |
123 delete observer; | |
124 | |
125 // the connection is permanent; if we emit the same message again, the observer will be notified again | |
126 testCounter = 0; | |
127 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); | |
128 ASSERT_EQ(0, testCounter); | |
129 } | |
130 | |
131 namespace { | |
132 class MyObserverWithLambda : public IObserver { | |
133 private: | |
134 int multiplier_; // this is a private variable we want to access in a lambda | |
135 | |
136 public: | |
137 MyObserverWithLambda(int multiplier, MyObservable& observable) | |
138 : multiplier_(multiplier) | |
139 { | |
140 // register a callable to a lambda that access private members | |
141 observable.RegisterObserverCallback(new LambdaCallable<MyObservable::MyCustomMessage>(*this, [this](const MyObservable::MyCustomMessage& message) { | |
142 testCounter += multiplier_ * message.payload_; | |
143 })); | |
144 | |
145 } | |
146 }; | |
147 } | |
148 | |
149 TEST(MessageBroker, TestLambdaCaptureThisAndAccessPrivateMembers) | |
150 { | |
151 MyObservable observable; | |
152 MyObserverWithLambda* observer = new MyObserverWithLambda(3, observable); | |
153 | |
154 testCounter = 0; | |
155 observable.BroadcastMessage(MyObservable::MyCustomMessage(12)); | |
156 ASSERT_EQ(36, testCounter); | |
157 | |
158 // delete the observer and check that the callback is not called anymore | |
159 delete observer; | |
160 | |
161 // the connection is permanent; if we emit the same message again, the observer will be notified again | |
162 testCounter = 0; | |
163 observable.BroadcastMessage(MyObservable::MyCustomMessage(20)); | |
164 ASSERT_EQ(0, testCounter); | |
165 } | |
166 | |
167 #endif // C++ 11 |