functors_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 functors_test.cpp
3 
4  functors_test.cpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 
20 #include <iostream>
21 #include <TaskContext.hpp>
22 #include <OperationCaller.hpp>
23 #include <Operation.hpp>
24 #ifdef ORO_REMOTING
26 #endif
27 #include <Service.hpp>
29 
30 #include "unit.hpp"
31 #include "operations_fixture.hpp"
32 
37 BOOST_FIXTURE_TEST_SUITE( FunctorsTestSuite, OperationsFixture )
38 
39 BOOST_AUTO_TEST_CASE(testClientThreadFunctor)
40 {
41  // Test the produce() method, which maps to call()
42 
43  std::vector<base::DataSourceBase::shared_ptr> args;
44  DataSourceBase::shared_ptr dsb = tc->provides("methods")->produce("m0", args, caller->engine());
45  DataSource<double>::shared_ptr dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
46  BOOST_CHECK_EQUAL( -1.0, dsd->get());
47 
48  args = GenerateDataSource().operator()<int>(1);
49  dsb = tc->provides("methods")->produce("m1", args, caller->engine());
50  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
51  BOOST_CHECK_EQUAL( -2.0, dsd->get());
52 
53  args = GenerateDataSource().operator()<int,double>(1, 2.0);
54  dsb = tc->provides("methods")->produce("m2", args, caller->engine());
55  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
56  BOOST_CHECK_EQUAL( -3.0, dsd->get());
57 
58  args = GenerateDataSource().operator()<int,double,bool>(1,2.0,true);
59  dsb = tc->provides("methods")->produce("m3", args, caller->engine());
60  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
61  BOOST_CHECK_EQUAL( -4.0, dsd->get());
62 
63  args = GenerateDataSource().operator()<int,double,bool,std::string>(1,2.0,true,"hello");
64  dsb = tc->provides("methods")->produce("m4", args, caller->engine());
65  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
66  BOOST_CHECK_EQUAL( -5.0, dsd->get());
67 
68  args = GenerateDataSource().operator()<int,double,bool,std::string,float>(1,2.0,true,"hello", 5.0f);
69  dsb = tc->provides("methods")->produce("m5", args, caller->engine());
70  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
71  BOOST_CHECK_EQUAL( -6.0, dsd->get());
72 
73  args = GenerateDataSource().operator()<int,double,bool,std::string,float>(1,2.0,true,"hello", 5.0f, 'a');
74  dsb = tc->provides("methods")->produce("m6", args, caller->engine());
75  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
76  BOOST_CHECK_EQUAL( -7.0, dsd->get());
77 
78  args = GenerateDataSource().operator()<int,double,bool,std::string,float>(1,2.0,true,"hello", 5.0f, 'a', (unsigned int)7);
79  dsb = tc->provides("methods")->produce("m7", args, caller->engine());
80  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
81  BOOST_CHECK_EQUAL( -8.0, dsd->get());
82 
83  double d = 10.0;
84  args = GenerateDataSource().operator()<double&>(d);
85  dsb = tc->provides("methods")->produce("m1r", args, caller->engine());
86  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
87  BOOST_CHECK_EQUAL( 20.0, dsd->get()); // 2*a
88  BOOST_CHECK_EQUAL( 20.0, d ); // 2* &a
89 }
90 
91 BOOST_AUTO_TEST_CASE(testOwnThreadOperationCallerCall)
92 {
93  // Test the produce() method, which maps to call() for OwnThread case:
94 
95  std::vector<base::DataSourceBase::shared_ptr> args;
96  DataSourceBase::shared_ptr dsb = tc->provides("methods")->produce("o0", args, caller->engine());
97  DataSource<double>::shared_ptr dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
98  BOOST_CHECK_EQUAL( -1.0, dsd->get());
99 
100  args = GenerateDataSource().operator()<int>(1);
101  dsb = tc->provides("methods")->produce("o1", args, caller->engine());
102  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
103  BOOST_CHECK_EQUAL( -2.0, dsd->get());
104 
105  args = GenerateDataSource().operator()<int,double>(1, 2.0);
106  dsb = tc->provides("methods")->produce("o2", args, caller->engine());
107  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
108  BOOST_CHECK_EQUAL( -3.0, dsd->get());
109 
110  args = GenerateDataSource().operator()<int,double,bool>(1,2.0,true);
111  dsb = tc->provides("methods")->produce("o3", args, caller->engine());
112  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
113  BOOST_CHECK_EQUAL( -4.0, dsd->get());
114 
115  args = GenerateDataSource().operator()<int,double,bool,std::string>(1,2.0,true,"hello");
116  dsb = tc->provides("methods")->produce("o4", args, caller->engine());
117  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
118  BOOST_CHECK_EQUAL( -5.0, dsd->get());
119 
120  args = GenerateDataSource().operator()<int,double,bool,std::string,float>(1,2.0,true,"hello", 5.0f);
121  dsb = tc->provides("methods")->produce("o5", args, caller->engine());
122  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
123  BOOST_CHECK_EQUAL( -6.0, dsd->get());
124 
125  args = GenerateDataSource().operator()<int,double,bool,std::string,float,char>(1,2.0,true,"hello", 5.0f, 'a');
126  dsb = tc->provides("methods")->produce("o6", args, caller->engine());
127  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
128  BOOST_CHECK_EQUAL( -7.0, dsd->get());
129 
130  args = GenerateDataSource().operator()<int,double,bool,std::string,float,char,unsigned int>(1,2.0,true,"hello", 5.0f, 'a', (unsigned int)7);
131  dsb = tc->provides("methods")->produce("o7", args, caller->engine());
132  dsd = dynamic_pointer_cast<DataSource<double> >(dsb);
133  BOOST_CHECK_EQUAL( -8.0, dsd->get());
134 }
135 
136 
137 BOOST_AUTO_TEST_CASE(testOwnThreadOperationCallerSend)
138 {
139  BOOST_REQUIRE( tc->isRunning() );
140  BOOST_REQUIRE( caller->isRunning() );
150 
151  // Test the produceSend() and produceHandle() methods, which maps to send() for OwnThread case:
152 
153  std::vector<base::DataSourceBase::shared_ptr> args;
155  DataSourceBase::shared_ptr dsb; // send
156  DataSourceBase::shared_ptr dsh; // handle
157  DataSourceBase::shared_ptr dsc; // collect
159 
160  // Get send and handle functors:
161  dsb = tc->provides("methods")->produceSend("o0", args, caller->engine());
162  dsh= tc->provides("methods")->produceHandle("o0");
163  args.clear();
164  args.push_back( dsh );
165  args.push_back( dsr );
166  DataSourceBase::shared_ptr dsc0 = tc->provides("methods")->produceCollect("o0",args,new ValueDataSource<bool>(true));
167 
168  // Assign the handle of produceSend to the one of produceHandle:
169  update = dsh->updateAction( dsb.get() );
170  update->readArguments();
171  update->execute();
172  delete update;
173 
174  // get the handle:
175  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(void)> >* >( dsh.get() ) );
176  h0 = dynamic_cast<DataSource<SendHandle<double(void)> >* >( dsh.get() )->get();
177  BOOST_CHECK( h0.ready() );
178 
179  args = GenerateDataSource().operator()<int>(1);
180  // Get send and handle functors:
181  dsb = tc->provides("methods")->produceSend("o1", args, caller->engine());
182  dsh= tc->provides("methods")->produceHandle("o1");
183  args.clear();
184  args.push_back( dsh );
185  args.push_back( dsr );
186  DataSourceBase::shared_ptr dsc1 = tc->provides("methods")->produceCollect("o1",args, new ValueDataSource<bool>(true));
187 
188  // Assign the handle of produceSend to the one of produceHandle:
189  update = dsh->updateAction( dsb.get() );
190  update->readArguments();
191  update->execute();
192  delete update;
193 
194  // get the handle:
195  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int)> >* >( dsh.get() ) );
196  h1 = dynamic_cast<DataSource<SendHandle<double(int)> >* >( dsh.get() )->get();
197  BOOST_CHECK( h1.ready() );
198 
199 
200  args = GenerateDataSource().operator()<int,double>(1, 2.0);
201  // Get send and handle functors:
202  dsb = tc->provides("methods")->produceSend("o2", args, caller->engine());
203  dsh= tc->provides("methods")->produceHandle("o2");
204 
205  // Assign the handle of produceSend to the one of produceHandle:
206  update = dsh->updateAction( dsb.get() );
207  update->readArguments();
208  update->execute();
209  delete update;
210 
211  // get the handle:
212  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int,double)> >* >( dsh.get() ) );
213  h2 = dynamic_cast<DataSource<SendHandle<double(int,double)> >* >( dsh.get() )->get();
214  BOOST_CHECK( h2.ready() );
215 
216  args = GenerateDataSource().operator()<int,double,bool>(1,2.0,true);
217  // Get send and handle functors:
218  dsb = tc->provides("methods")->produceSend("o3", args, caller->engine());
219  dsh= tc->provides("methods")->produceHandle("o3");
220 
221  // Assign the handle of produceSend to the one of produceHandle:
222  update = dsh->updateAction( dsb.get() );
223  update->readArguments();
224  update->execute();
225  delete update;
226  // get the handle:
227  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int,double,bool)> >* >( dsh.get() ) );
228  h3 = dynamic_cast<DataSource<SendHandle<double(int,double,bool)> >* >( dsh.get() )->get();
229  BOOST_CHECK( h3.ready() );
230 
231  args = GenerateDataSource().operator()<int,double,bool,std::string>(1,2.0,true,"hello");
232  // Get send and handle functors:
233  dsb = tc->provides("methods")->produceSend("o4", args, caller->engine());
234  dsh= tc->provides("methods")->produceHandle("o4");
235  // Assign the handle of produceSend to the one of produceHandle:
236  update = dsh->updateAction( dsb.get() );
237  update->readArguments();
238  update->execute();
239  delete update;
240  // get the handle:
241  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string)> >* >( dsh.get() ) );
242  h4 = dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string)> >* >( dsh.get() )->get();
243  BOOST_CHECK( h4.ready() );
244 
245  args = GenerateDataSource().operator()<int,double,bool,std::string,float>(1,2.0,true,"hello",5.0f);
246  // Get send and handle functors:
247  dsb = tc->provides("methods")->produceSend("o5", args, caller->engine());
248  dsh= tc->provides("methods")->produceHandle("o5");
249  // Assign the handle of produceSend to the one of produceHandle:
250  update = dsh->updateAction( dsb.get() );
251  update->readArguments();
252  update->execute();
253  delete update;
254  // get the handle:
255  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string,float)> >* >( dsh.get() ) );
256  h5 = dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string,float)> >* >( dsh.get() )->get();
257  BOOST_CHECK( h5.ready() );
258 
259  args = GenerateDataSource().operator()<int,double,bool,std::string,float,char>(1,2.0,true,"hello",5.0f,'a');
260  // Get send and handle functors:
261  dsb = tc->provides("methods")->produceSend("o6", args, caller->engine());
262  dsh= tc->provides("methods")->produceHandle("o6");
263  // Assign the handle of produceSend to the one of produceHandle:
264  update = dsh->updateAction( dsb.get() );
265  update->readArguments();
266  update->execute();
267  delete update;
268  // get the handle:
269  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string,float,char)> >* >( dsh.get() ) );
270  h6 = dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string,float,char)> >* >( dsh.get() )->get();
271  BOOST_CHECK( h6.ready() );
272 
273  args = GenerateDataSource().operator()<int,double,bool,std::string,float,char,unsigned int>(1,2.0,true,"hello",5.0f,'a',(unsigned int)7);
274  // Get send and handle functors:
275  dsb = tc->provides("methods")->produceSend("o7", args, caller->engine());
276  dsh= tc->provides("methods")->produceHandle("o7");
277  // Assign the handle of produceSend to the one of produceHandle:
278  update = dsh->updateAction( dsb.get() );
279  update->readArguments();
280  update->execute();
281  delete update;
282  // get the handle:
283  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(int,double,bool,std::string,float,char,unsigned int)> >* >( dsh.get() ) );
285  BOOST_CHECK( h7.ready() );
286 
287 
288  double d = 10.0;
289  args = GenerateDataSource().operator()<double&>(d);
290  dsb = tc->provides("methods")->produceSend("o1r", args, caller->engine());
291  dsh = tc->provides("methods")->produceHandle("o1r");
292  // Assign the handle of produceSend to the one of produceHandle:
293  update = dsh->updateAction( dsb.get() );
294  update->readArguments();
295  update->execute();
296  delete update;
297  // get the handle:
298  BOOST_REQUIRE( dynamic_cast<DataSource<SendHandle<double(double&)> >* >( dsh.get() ) );
299  h1r = dynamic_cast<DataSource<SendHandle<double(double&)> >* >( dsh.get() )->get();
300  BOOST_CHECK( h1r.ready() );
301 
302  // Now check all receptions all-in-once:
303  double retn=0;
304  double rarg=0;
305  dsc0->evaluate();
306  BOOST_CHECK_EQUAL( dsr->get(), -1.0 );
307  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
308  BOOST_CHECK_EQUAL( retn, -1.0 );
309  BOOST_CHECK_EQUAL( SendSuccess, h1.collect(retn) );
310  BOOST_CHECK_EQUAL( retn, -2.0 );
311  BOOST_CHECK_EQUAL( SendSuccess, h2.collect(retn) );
312  BOOST_CHECK_EQUAL( retn, -3.0 );
313  BOOST_CHECK_EQUAL( SendSuccess, h3.collect(retn) );
314  BOOST_CHECK_EQUAL( retn, -4.0 );
315  BOOST_CHECK_EQUAL( SendSuccess, h4.collect(retn) );
316  BOOST_CHECK_EQUAL( retn, -5.0 );
317  BOOST_CHECK_EQUAL( SendSuccess, h5.collect(retn) );
318  BOOST_CHECK_EQUAL( retn, -6.0 );
319  BOOST_CHECK_EQUAL( SendSuccess, h6.collect(retn) );
320  BOOST_CHECK_EQUAL( retn, -7.0 );
321  BOOST_CHECK_EQUAL( SendSuccess, h7.collect(retn) );
322  BOOST_CHECK_EQUAL( retn, -8.0 );
323  BOOST_CHECK_EQUAL( SendSuccess, h1r.collect(retn, rarg) );
324  BOOST_CHECK_EQUAL( retn, 20.0 );
325  BOOST_CHECK_EQUAL( rarg, 20.0 );
326 
327  retn = rarg = 0;
328  // collectIfDone will certainly succeed after collect
329  BOOST_CHECK_EQUAL( SendSuccess, h0.collectIfDone(retn) );
330  BOOST_CHECK_EQUAL( retn, -1.0 );
331  BOOST_CHECK_EQUAL( SendSuccess, h1.collectIfDone(retn) );
332  BOOST_CHECK_EQUAL( retn, -2.0 );
333  BOOST_CHECK_EQUAL( SendSuccess, h2.collectIfDone(retn) );
334  BOOST_CHECK_EQUAL( retn, -3.0 );
335  BOOST_CHECK_EQUAL( SendSuccess, h3.collectIfDone(retn) );
336  BOOST_CHECK_EQUAL( retn, -4.0 );
337  BOOST_CHECK_EQUAL( SendSuccess, h4.collectIfDone(retn) );
338  BOOST_CHECK_EQUAL( retn, -5.0 );
339  BOOST_CHECK_EQUAL( SendSuccess, h5.collectIfDone(retn) );
340  BOOST_CHECK_EQUAL( retn, -6.0 );
341  BOOST_CHECK_EQUAL( SendSuccess, h6.collectIfDone(retn) );
342  BOOST_CHECK_EQUAL( retn, -7.0 );
343  BOOST_CHECK_EQUAL( SendSuccess, h7.collectIfDone(retn) );
344  BOOST_CHECK_EQUAL( retn, -8.0 );
345  BOOST_CHECK_EQUAL( SendSuccess, h1r.collectIfDone(retn,rarg) );
346  BOOST_CHECK_EQUAL( retn, 20.0 );
347  BOOST_CHECK_EQUAL( rarg, 20.0 );
348 
349  // the return value api.
350  BOOST_CHECK_EQUAL( -1.0, h0.ret() );
351  BOOST_CHECK_EQUAL( -2.0, h1.ret(1) );
352  BOOST_CHECK_EQUAL( -3.0, h2.ret(1, 2.0) );
353  BOOST_CHECK_EQUAL( -4.0, h3.ret(1, 2.0, true) );
354  BOOST_CHECK_EQUAL( -5.0, h4.ret(1, 2.0, true,"hello") );
355  BOOST_CHECK_EQUAL( -6.0, h5.ret(1, 2.0, true,"hello", 5.0f) );
356  BOOST_CHECK_EQUAL( -7.0, h6.ret(1, 2.0, true,"hello", 5.0f, 'a') );
357  BOOST_CHECK_EQUAL( -8.0, h7.ret(1, 2.0, true,"hello", 5.0f, 'a', (unsigned int)7) );
358 
359  BOOST_CHECK_EQUAL( -2.0, h1.ret() );
360  BOOST_CHECK_EQUAL( -3.0, h2.ret() );
361  BOOST_CHECK_EQUAL( -4.0, h3.ret() );
362  BOOST_CHECK_EQUAL( -5.0, h4.ret() );
363  BOOST_CHECK_EQUAL( -6.0, h5.ret() );
364  BOOST_CHECK_EQUAL( -7.0, h6.ret() );
365  BOOST_CHECK_EQUAL( -8.0, h7.ret() );
366  BOOST_CHECK_EQUAL( 20.0, h1r.ret() );
367 }
368 #ifdef ORO_REMOTING_OLD
369 
370 BOOST_AUTO_TEST_CASE(testOperationCallerFactory)
371 {
372  // Test the addition of 'simple' methods to the operation interface,
373  // and retrieving it back in a new OperationCaller object.
374 
375  Operation<double(void)> m0("m0");
376  m0.calls(&FunctorsTest::m0, this);
377  Operation<double(int)> m1("m1");
378  m1.calls(&FunctorsTest::m1, this);
380  m2.calls(&FunctorsTest::m2, this);
381 
382  Service to("task");
383 
384  BOOST_CHECK( !to.addOperation(m0).ready() );
385  to.setOwner(tc);
386  BOOST_CHECK( to.addOperation(m0).ready() );
387  BOOST_CHECK( to.addOperation(m0).ready() );
388  BOOST_CHECK( to.addOperation(m1).ready() );
389  BOOST_CHECK( to.addOperation(m2).ready() );
390 
391  // test constructor
392  OperationCaller<double(void)> mm0 = to.getOperation<double(void)>("m0");
393  BOOST_CHECK( mm0.getOperationCallerImpl() );
394  BOOST_CHECK( mm0.ready() );
395 
396  // test operator=()
398  mm1 = to.getOperation<double(int)>("m1");
399  BOOST_CHECK( mm1.getOperationCallerImpl() );
400  BOOST_CHECK( mm1.ready() );
401 
402  OperationCaller<double(int,double)> mm2 = to.getOperation<double(int,double)>("m2");
403  BOOST_CHECK( mm2.getOperationCallerImpl() );
404  BOOST_CHECK( mm2.ready() );
405 
406  // execute methods and check status:
407  BOOST_CHECK_EQUAL( -1.0, mm0() );
408 
409  BOOST_CHECK_EQUAL( -2.0, mm1(1) );
410  BOOST_CHECK_EQUAL( -3.0, mm2(1, 2.0) );
411 
412  // test error cases:
413  // Add uninitialised op:
414  Operation<void(void)> ovoid("voidm");
415  BOOST_CHECK(to.addOperation( ovoid ).ready() == false);
416  ovoid = Operation<void(void)>("voidm");
417  BOOST_CHECK(to.addOperation( ovoid ).ready() == false);
418 
419  // wrong type 1:
421  mvoid = to.getOperation<void(void)>("m1");
422  BOOST_CHECK( mvoid.ready() == false );
423  // wrong type 2:
424  mvoid = to.getOperation<void(bool)>("m1");
425  // wrong type 3:
426  mvoid = to.getOperation<double(void)>("m0");
427  BOOST_CHECK( mvoid.ready() == false );
428  // non existing
429  mvoid = to.getOperation<void(void)>("voidm");
430  BOOST_CHECK( mvoid.ready() == false );
431 
432  // this line may not crash:
433  mvoid();
434 
435 }
436 
437 BOOST_AUTO_TEST_CASE(testCROperationCaller)
438 {
439  this->ret = -3.3;
440 
441  OperationCaller<double&(void)> m0r("m0r", &FunctorsTest::m0r, this);
442  OperationCaller<const double&(void)> m0cr("m0cr", &FunctorsTest::m0cr, this);
443 
444  OperationCaller<double(double&)> m1r("m1r", &FunctorsTest::m1r, this);
445  OperationCaller<double(const double&)> m1cr("m1cr", &FunctorsTest::m1cr, this);
446 
447  BOOST_CHECK_EQUAL( -3.3, m0r() );
448  BOOST_CHECK_EQUAL( -3.3, m0cr() );
449 
450  double value = 5.3;
451  BOOST_CHECK_EQUAL( 5.3*2, m1r(value) );
452  BOOST_CHECK_EQUAL( 5.3*2, value );
453  BOOST_CHECK_EQUAL( 5.3, m1cr(5.3) );
454 }
455 
456 BOOST_AUTO_TEST_CASE(testOperationCallerFromDS)
457 {
458  Service to("task");
459 
460  OperationCaller<double(void)> m0("m0", &FunctorsTest::m0, this);
461  OperationCaller<double(int)> m1("m1", &FunctorsTest::m1, this);
462  OperationCaller<double(int,double)> m2("m2", &FunctorsTest::m2, this);
463  OperationCaller<double(int,double,bool)> m3("m3", &FunctorsTest::m3, this);
464  OperationCaller<double(int,double,bool,std::string)> m4("m4", &FunctorsTest::m4, this);
465 
466  to.addOperation( &m0, "desc");
467  to.addOperation( &m1, "desc", "a1", "d1");
468  to.addOperation( &m2, "desc", "a1", "d1", "a2","d2");
469  to.addOperation( &m3, "desc", "a1", "d1", "a2","d2","a3","d3");
470  to.addOperation( &m4, "desc", "a1", "d1", "a2","d2","a3","d3", "a4","d4");
471 
472  double ret;
473  OperationCallerC mc0( to.methods(), "m0");
474  mc0.ret(ret);
475  OperationCallerC mc1( to.methods(), "m1");
476  mc1.argC(1).ret(ret);
477  OperationCallerC mc2( to.methods(), "m2");
478  mc2.argC(1).argC(2.0).ret(ret);
479  OperationCallerC mc3( to.methods(), "m3");
480  mc3.argC(1).argC(2.0).argC(false).ret(ret);
481  OperationCallerC mc4( to.methods(), "m4");
482  mc4.argC(1).argC(2.0).argC(false).argC(std::string("hello")).ret(ret);
483 
484  BOOST_CHECK( mc0.execute() );
485  BOOST_CHECK_EQUAL(-1.0, ret);
486  BOOST_CHECK( mc1.execute() );
487  BOOST_CHECK_EQUAL(-2.0, ret);
488  BOOST_CHECK( mc2.execute() );
489  BOOST_CHECK_EQUAL(-3.0, ret);
490  BOOST_CHECK( mc3.execute() );
491  BOOST_CHECK_EQUAL(-4.0, ret);
492  BOOST_CHECK( mc4.execute() );
493  BOOST_CHECK_EQUAL(-5.0, ret);
494 }
495 
496 BOOST_AUTO_TEST_CASE(testDSOperationCaller)
497 {
498  Service to("task", tc);
499 
500  // A method of which the first argument type is a pointer to the object
501  // on which it must be invoked. The pointer is internally stored as a weak_ptr,
502  // thus the object must be stored in a shared_ptr, in a DataSource. Scripting
503  // requires this for copying state machines.
504 
506  meth0.calls( boost::bind(&FunctorsTest::m0, _1));
507 
508  //method_ds("m0", &FunctorsTest::m0);
509 
511  meth1.calls(boost::bind(&FunctorsTest::m1, _1,_2));
512 
513  //method_ds("m1", &FunctorsTest::m1);
514  //method_ds("ms",&FunctorsTest::comstr );
515 
516  boost::shared_ptr<FunctorsTest> ptr( new FunctorsTest() );
518  BOOST_CHECK( to.addOperationDS( wp.get(), meth0).doc("desc" ).ready() );
519  BOOST_CHECK( to.addOperationDS( wp.get(), meth1).doc("desc").arg("a1", "d1" ).ready() );
520 
521  // this actually works ! the method will detect the deleted pointer.
522  //ptr.reset();
523 
524  double ret;
525  OperationCallerC c0 = to.create("m0").ret(ret);
526  BOOST_CHECK( c0.execute() );
527  BOOST_CHECK_EQUAL( -1.0, ret );
528  OperationCallerC c1 = to.create("m1").argC(1).ret(ret);
529  BOOST_CHECK( c1.execute() );
530  BOOST_CHECK_EQUAL( -2.0, ret );
531 
532 }
533 #endif // defined(ORO_REMOTING)
534 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
virtual result_t get() const =0
#define BOOST_AUTO_TEST_SUITE_END()
Operation< Signature > & addOperation(Operation< Signature > &op)
Definition: Service.hpp:341
virtual void readArguments()=0
BOOST_AUTO_TEST_CASE(testClientThreadFunctor)
The SendHandle is used to collect the result values of an asynchronous invocation. The template argument Signature must have the same type as the method being invoked.
Definition: rtt-fwd.hpp:79
virtual bool execute()=0
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
OperationCallerC & ret(base::AttributeBase *r)
Operation & calls(boost::function< Signature > func, ExecutionThread et=ClientThread, ExecutionEngine *ownerEngine=NULL)
Definition: Operation.hpp:147
DataSource< T >::result_t get() const
Definition: DataSources.hpp:78
OperationCallerC & argC(const ArgT a)
OperationInterfacePart * getOperation(std::string name)
Definition: Service.cpp:142
SendStatus collect() const
Definition: SendHandle.hpp:124
Operation< typename internal::GetSignatureDS< Func >::Signature > & addOperationDS(const std::string &name, Func func, internal::DataSource< boost::shared_ptr< ObjT > > *sp, ExecutionThread et=ClientThread)
Definition: Service.hpp:506
boost::intrusive_ptr< DataSourceBase > shared_ptr
void setOwner(TaskContext *new_owner)
Definition: Service.cpp:254
internal::OperationCallerC create(std::string name, ExecutionEngine *caller)
Definition: Service.cpp:268
bool ready() const
Definition: SendHandle.hpp:117
static bool update(TaskContext *tc)
boost::intrusive_ptr< ValueDataSource< T > > shared_ptr
Definition: DataSources.hpp:72


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:24