17
17
*/
18
18
19
19
// The purpose of this test case is threefold.
20
- // - Test sigc::track_obj().
20
+ // - Test sigc::track_obj() and sigc::track_object() .
21
21
// - Show that a slot with a C++11 lambda expression can be automatically
22
22
// disconnected when an object derived from sigc::trackable is deleted,
23
- // provided sigc::track_obj() is used.
23
+ // provided sigc::track_obj() or sigc::track_object() is used.
24
24
// It shows that C++11 lambda expressions can replace the libsigc++ lambda
25
25
// expressions, which have been removed.
26
26
// See https://bugzilla.gnome.org/show_bug.cgi?id=672555
27
27
// - Test the code example in the documentation in sigc++/adaptors/track_obj.h.
28
28
//
29
- // To test the C++11 lambda expressions with gcc 4.6.3 (and probably some later
30
- // versions of gcc; gcc 4.7.x also understands -std=c++11):
31
- // make CXXFLAGS='-g -O2 -std=c++0x' test_track_obj
32
- // ./test_track_obj
33
- // echo $?
34
29
// If test_track_obj writes nothing and the return code is 0, the test has passed.
35
30
36
-
37
31
#include " testutilities.h"
38
32
#include < string>
39
33
#include < iostream>
@@ -130,74 +124,88 @@ int main(int argc, char* argv[])
130
124
return util->get_result_and_delete_instance () ? EXIT_SUCCESS : EXIT_FAILURE;
131
125
132
126
sigc::slot<std::string, int > sl1;
127
+ sigc::slot<std::string, int > sl2;
133
128
{
134
129
bar_group4 bar4;
135
130
sl1 = sigc::track_obj (Functor1 (bar4), bar4);
136
- result_stream << sl1 (-2 );
137
- util->check_result (result_stream, " negative" );
131
+ sl2 = sigc::track_object (Functor1 (bar4), bar4);
132
+ result_stream << sl1 (-2 ) << " , " << sl2 (2 );
133
+ util->check_result (result_stream, " negative, positive" );
138
134
139
- } // auto-disconnect sl1
135
+ } // auto-disconnect sl1 and sl2
140
136
141
- result_stream << sl1 (-2 );
142
- util->check_result (result_stream, " " );
137
+ result_stream << sl1 (-2 ) << " , " << sl2 ( 2 ) ;
138
+ util->check_result (result_stream, " , " );
143
139
144
140
// Allocate on the heap. valgrind can then find erroneous memory accesses.
145
141
// (There should be none, of course.)
146
- auto psl2 = new sigc::slot<std::string, int , std::string>;
142
+ auto psl3 = new sigc::slot<std::string, int , std::string>;
143
+ auto psl4 = new sigc::slot<std::string, int , std::string>;
147
144
bar_group4* pbar4 = new bar_group4;
148
145
book* pbook4 = new book (" A Book" );
149
- *psl2 = sigc::track_obj (Functor2 (*pbar4, *pbook4), *pbar4, *pbook4);
150
- result_stream << (*psl2)(0 , " Book title: " );
151
- util->check_result (result_stream, " zero, Book title: A Book" );
146
+ *psl3 = sigc::track_obj (Functor2 (*pbar4, *pbook4), *pbar4, *pbook4);
147
+ *psl4 = sigc::track_object (Functor2 (*pbar4, *pbook4), *pbar4, *pbook4);
148
+ result_stream << (*psl3)(0 , " Book title: " ) << " , " << (*psl4)(1 , " Title: " );
149
+ util->check_result (result_stream, " zero, Book title: A Book, positive, Title: A Book" );
152
150
153
- delete pbook4; // auto-disconnect *psl2
151
+ delete pbook4; // auto-disconnect *psl3 and *psl4
154
152
pbook4 = nullptr ;
155
- result_stream << (*psl2)(0 , " Book title: " );
156
- util->check_result (result_stream, " " );
157
- delete psl2;
158
- psl2 = nullptr ;
153
+ result_stream << (*psl3)(0 , " Book title: " ) << " , " << (*psl4)(1 , " Title: " );
154
+ util->check_result (result_stream, " , " );
155
+ delete psl3;
156
+ psl3 = nullptr ;
157
+ delete psl4;
158
+ psl4 = nullptr ;
159
159
delete pbar4;
160
160
pbar4 = nullptr ;
161
161
162
-
163
- // C++11 lambda expressions:
162
+ // C++11 lambda expressions:
164
163
165
164
// auto-disconnect
166
165
// If you want to auto-disconnect a slot with a C++11 lambda expression
167
166
// that contains references to sigc::trackable-derived objects, you must use
168
- // sigc::track_obj().
169
- sigc::slot<void , std::ostringstream&> sl10;
167
+ // sigc::track_obj() or sigc::track_object().
168
+ sigc::slot<void , std::ostringstream&> sl11;
169
+ sigc::slot<void , std::ostringstream&> sl12;
170
170
{
171
171
book guest_book (" karl" );
172
172
// sl1 = [&guest_book](std::ostringstream& stream){ stream << guest_book << "\n"; }; // no auto-disconnect
173
- sl10 = sigc::track_obj ([&guest_book](std::ostringstream& stream){ stream << guest_book; }, guest_book);
174
- sl10 (result_stream);
175
- util->check_result (result_stream, " karl" );
176
-
177
- } // auto-disconnect sl10
178
-
179
- sl10 (result_stream);
173
+ sl11 = sigc::track_obj (
174
+ [&guest_book](std::ostringstream& stream) { stream << guest_book; }, guest_book);
175
+ sl12 = sigc::track_object (
176
+ [&guest_book](std::ostringstream& stream) { stream << guest_book; }, guest_book);
177
+ sl11 (result_stream);
178
+ sl12 (result_stream);
179
+ util->check_result (result_stream, " karlkarl" );
180
+
181
+ } // auto-disconnect sl11 and sl12
182
+
183
+ sl11 (result_stream);
184
+ sl12 (result_stream);
180
185
util->check_result (result_stream, " " );
181
186
182
187
// auto-disconnect
183
- sigc::slot<void > sl20;
188
+ sigc::slot<void > sl21;
189
+ sigc::slot<void > sl22;
184
190
{
185
191
book guest_book (" karl" );
186
192
// sl2 = [&guest_book] () { egon(guest_book); }; // no auto-disconnect
187
193
// sl2 = std::bind(&egon, std::ref(guest_book)); // does not compile (gcc 4.6.3)
188
- sl20 = sigc::track_obj ([&guest_book] () { egon (guest_book); }, guest_book);
189
- sl20 ();
190
- util->check_result (result_stream, " egon(string 'karl')" );
194
+ sl21 = sigc::track_obj ([&guest_book]() { egon (guest_book); }, guest_book);
195
+ sl22 = sigc::track_obj ([&guest_book]() { egon (guest_book); }, guest_book);
196
+ sl21 ();
197
+ sl22 ();
198
+ util->check_result (result_stream, " egon(string 'karl')egon(string 'egon was here')" );
191
199
192
200
result_stream << static_cast <const std::string&>(guest_book);
193
201
util->check_result (result_stream, " egon was here" );
194
202
195
- } // auto-disconnect sl20
203
+ } // auto-disconnect sl21 and sl22
196
204
197
- sl20 ();
205
+ sl21 ();
206
+ sl22 ();
198
207
util->check_result (result_stream, " " );
199
208
200
-
201
209
// Code example in the documentation sigc++/adaptors/macros/track_obj.h.m4
202
210
// -----------------------------------------------------------------------
203
211
{
@@ -210,15 +218,15 @@ int main(int argc, char* argv[])
210
218
// some_signal.connect([&some_bar](){ foo_group4(some_bar); }); // no auto-disconnect
211
219
// some_signal.connect(sigc::bind(&foo_group4, std::ref(some_bar))); // auto-disconnects, but we prefer C++11 lambda
212
220
some_signal.connect (sigc::track_obj ([&some_bar](){ foo_group4 (some_bar); }, some_bar));
221
+ some_signal.connect (sigc::track_object ([&some_bar](){ foo_group4 (some_bar); }, some_bar));
213
222
some_signal.emit ();
214
- util->check_result (result_stream, " foo_group4(bar_group4&)" );
223
+ util->check_result (result_stream, " foo_group4(bar_group4&)foo_group4(bar_group4&) " );
215
224
216
225
} // auto-disconnect the lambda expression
217
226
218
227
some_signal.emit ();
219
228
util->check_result (result_stream, " " );
220
229
}
221
230
222
-
223
231
return util->get_result_and_delete_instance () ? EXIT_SUCCESS : EXIT_FAILURE;
224
232
}
0 commit comments