1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
/*
Adept MobileRobots Robotics Interface for Applications (ARIA)
Copyright (C) 2004, 2005 ActivMedia Robotics LLC
Copyright (C) 2006, 2007, 2008, 2009, 2010 MobileRobots Inc.
Copyright (C) 2011, 2012, 2013 Adept Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you wish to redistribute ARIA under different terms, contact
Adept MobileRobots for information about a commercial version of ARIA at
robots@mobilerobots.com or
Adept MobileRobots, 10 Columbia Drive, Amherst, NH 03031; +1-603-881-7960
*/
#include "Aria.h"
/*
TestClass - This class has 6 functions which are used to test out the
functors.
*/
class TestClass
{
public:
void function();
void function(int arg1);
void function(bool arg1, std::string arg2);
bool retFunction();
char * retFunction(int arg1);
double retFunction(bool arg1, std::string arg2);
};
void TestClass::function()
{
printf("TestClass::function\n");
}
void TestClass::function(int arg1)
{
printf("TestClass::function(int arg1=%d)\n", arg1);
}
void TestClass::function(bool arg1, std::string arg2)
{
printf("TestClass::function(bool arg1=%d, std::string arg2='%s')\n",
arg1, arg2.c_str());
}
bool TestClass::retFunction()
{
printf("bool TestClass::retFunction\n");
return(true);
}
char * TestClass::retFunction(int arg1)
{
printf("char * TestClass::retFunction(int arg1=%d)\n", arg1);
return("Hello");
}
double TestClass::retFunction(bool arg1, std::string arg2)
{
printf("double TestClass::retFunction(bool arg1=%d, std::string arg2='%s')\n",
arg1, arg2.c_str());
return(4.62);
}
/*
Here are 6 global functions to test out the functors for non-member funtions.
*/
void function()
{
printf("function\n");
}
void function(int arg1)
{
printf("function(int arg1=%d)\n", arg1);
}
void function(bool arg1, std::string arg2)
{
printf("function(bool arg1=%d, std::string arg2='%s')\n",
arg1, arg2.c_str());
}
bool retFunction()
{
printf("bool retFunction\n");
return(true);
}
char * retFunction(int arg1)
{
printf("char * retFunction(int arg1=%d)\n", arg1);
return("Hello");
}
double retFunction(bool arg1, std::string arg2)
{
printf("double retFunction(bool arg1=%d, std::string arg2='%s')\n",
arg1, arg2.c_str());
return(4.62);
}
/*
Test functors with class member funtions
*/
// Direct invocation of the functors with supplying parameters.
void testDirect()
{
TestClass test;
ArFunctorC<TestClass> functor(test, &TestClass::function);
ArFunctor1C<TestClass, int> functor1(test, &TestClass::function, 1);
ArFunctor2C<TestClass, bool, std::string> functor2(test,
&TestClass::function,
false, "default arg");
printf("\n****** Testing direct invocation using ArFunctor::invoke(...)\n");
puts("> Should see TestClass::function()...");
functor.invoke();
puts("> Should see TestClass::function(1)...");
functor1.invoke();
puts("> Should see TestClass::function(5)...");
functor1.invoke(5);
puts("> Should see TestClass::function(true, \"argument 1\")...");
functor2.invoke(true, "argument 1");
}
// Invocation of a base ArFunctor pointer to a functor. Because the pointer
// is of type ArFunctor, the parameters can not be supplied. The default
// parameters, which are supplied when the functor is constructed, are used.
void testBase()
{
TestClass test;
ArFunctor *fptr;
ArFunctorC<TestClass> functor(test, &TestClass::function);
ArFunctor1C<TestClass, int> functor1(test, &TestClass::function, 1);
ArFunctor2C<TestClass, bool, std::string> functor2(test,
&TestClass::function,
false, "default arg");
printf("\n****** Testing base invocation\n");
fptr=&functor;
puts("> Should see TestClass::function()...");
fptr->invoke();
fptr=&functor1;
puts("> Should see TestClass::function(1)...");
fptr->invoke();
fptr=&functor2;
puts("> Should see TestClass::function(false, \"default arg\")...");
fptr->invoke();
}
// Invocation of pointers which supply the parameter type. Full invocation
// with paramters is posesible in this fashion with out knowing the class
// that the functor refers to.
void testParams()
{
TestClass test;
ArFunctorC<TestClass> functor(test, &TestClass::function);
ArFunctor1C<TestClass, int> functor1(test, &TestClass::function);
ArFunctor2C<TestClass, bool, std::string> functor2(test,
&TestClass::function);
ArFunctor *fptr;
ArFunctor1<int> *fptr1;
ArFunctor2<bool, std::string> *fptr2;
printf("\n****** Testing pointer invocation\n");
fptr=&functor;
puts("> Should see TestClass::function()...");
fptr->invoke();
fptr1=&functor1;
puts("> Should see TestClass::function(2)...");
fptr1->invoke(2);
fptr2=&functor2;
puts("> Should see TestClass::function(true, \"argument 2\")...");
fptr2->invoke(true, "argument 2");
}
void setFunctorPtr(ArFunctor *f)
{
}
void setIntFunctorPtr(ArFunctor1<int> *f)
{
}
// It is possible to supply a more specialized ArFunctor class to a function
// that takes a plant ArFunctor pointer, since it will be
// implicitly cast to that parent class
void testDowncast()
{
ArRetFunctor1C<char*, TestClass, int> f;
ArFunctor* y = &f;
setFunctorPtr(&f);
setFunctorPtr(y);
}
/*
Test functors with return values, ArRetFunctor
*/
// Direct invocation of the functors with return values and supplying
// parameters. It is not posesible to have the operator() for functors with
// return values. This is due to limitations of C++ and different C++
// compilers where you can not overload return values in all cases.
void testReturnDirect()
{
TestClass test;
ArRetFunctorC<bool, TestClass> functor(test, &TestClass::retFunction);
ArRetFunctor1C<char*, TestClass, int>
functor1(test, &TestClass::retFunction, 1);
ArRetFunctor2C<double, TestClass, bool, std::string>
functor2(test, &TestClass::retFunction, false, "default arg");
bool bret;
char *cret;
double dret;
//bret=test.retFunction();
//cret=test.retFunction(4);
//dret=test.retFunction(true, "foof");
printf("\n****** Testing direct invocation with return\n");
puts("> TestClass::retFunction() should return true...");
bret=functor.invokeR();
printf("Returned: %d\n", bret);
puts("> TestClass::retFunction(5) should return \"Hello\"...");
cret=functor1.invokeR(5);
printf("Returned: %s\n", cret);
puts("> TestClass::retFunction(true, \"argument 1\") should return 4.62...");
dret=functor2.invokeR(true, "argument 1");
printf("Returned: %e\n", dret);
}
void testReturnBase()
{
TestClass test;
ArRetFunctorC<bool, TestClass> functor(test, &TestClass::retFunction);
ArRetFunctor1C<char*, TestClass, int>
functor1(test, &TestClass::retFunction, 1);
ArRetFunctor2C<double, TestClass, bool, std::string>
functor2(test, &TestClass::retFunction, false, "default arg");
ArRetFunctor<bool> *fBoolPtr;
ArRetFunctor<char*> *fCharPtr;
ArRetFunctor<double> *fDoublePtr;
bool bret;
char *cret;
double dret;
printf("\n****** Testing base invocation with return\n");
fBoolPtr=&functor;
puts("> TestClass::retFunction() should return true");
bret=fBoolPtr->invokeR();
printf("Returned: %d\n", bret);
fCharPtr=&functor1;
puts("> TestClass::retFunction(1) should return \"Hello\"");
cret=fCharPtr->invokeR();
printf("Returned: %s\n", cret);
fDoublePtr=&functor2;
puts("> TestClass::retFunction(false, \"default arg\" should return 4.62");
dret=fDoublePtr->invokeR();
printf("Returned: %e\n", dret);
}
void testReturnParams()
{
TestClass test;
ArRetFunctorC<bool, TestClass> functor(test, &TestClass::retFunction);
ArRetFunctor1C<char*, TestClass, int>
functor1(test, &TestClass::retFunction, 1);
ArRetFunctor2C<double, TestClass, bool, std::string>
functor2(test, &TestClass::retFunction, false, "default arg");
ArRetFunctor<bool> *fBoolPtr;
ArRetFunctor1<char*, int> *fCharPtr;
ArRetFunctor2<double, bool, std::string> *fDoublePtr;
bool bret;
char *cret;
double dret;
printf("\n****** Testing pointer invocation with return\n");
fBoolPtr=&functor;
puts("> TestClass::retFunction() should return true");
bret=fBoolPtr->invokeR();
printf("Returned: %d\n", bret);
fCharPtr=&functor1;
puts("> TestClass::retFunction(7) should return \"Hello\"");
cret=fCharPtr->invokeR(7);
printf("Returned: %s\n", cret);
fDoublePtr=&functor2;
puts("> TestClass::retFunction(false, \"argument 3\") should return 4.62...");
dret=fDoublePtr->invokeR(false, "argument 3");
printf("Returned: %e\n", dret);
}
/*
Test global functors, ArGlobalFunctor.
*/
// Direct invocation of the global functors with supplying parameters.
void testGlobalDirect()
{
ArGlobalFunctor functor(&function);
ArGlobalFunctor1<int> functor1(&function, 1);
ArGlobalFunctor2<bool, std::string> functor2(&function,
false, "default arg");
printf("\n****** Testing global direct invocation\n");
puts("> Should see function()...");
functor.invoke();
puts("> Should see function(5)...");
functor1.invoke(5);
puts("> Should see function(true, \"argument 1\")...");
functor2.invoke(true, "argument 1");
}
// Invocation of a base ArFunctor pointer to a global functor. Because the
// pointer is of type ArFunctor, the parameters can not be supplied. The
// default parameters, which are supplied when the functor is constructed,
// are used.
void testGlobalBase()
{
ArFunctor *fptr;
ArGlobalFunctor functor(function);
ArGlobalFunctor1<int> functor1(function, 1);
ArGlobalFunctor2<bool, std::string> functor2(function, false,
"default arg");
printf("\n****** Testing global base invocation\n");
fptr=&functor;
puts("> Should see function()...");
fptr->invoke();
fptr=&functor1;
puts("> Should see function(1)...");
fptr->invoke();
fptr=&functor2;
puts("> Should see function(false, \"default arg\")...");
fptr->invoke();
}
// Invocation of pointers which supply the parameter type. Full invocation
// with paramters is posesible in this fashion with out knowing the class
// that the functor refers to.
void testGlobalParams()
{
ArGlobalFunctor functor(function);
ArGlobalFunctor1<int> functor1(function, 1);
ArGlobalFunctor2<bool, std::string> functor2(function, false,
"default arg");
ArFunctor *fptr;
ArFunctor1<int> *fptr1;
ArFunctor2<bool, std::string> *fptr2;
printf("\n****** Testing global pointer invocation\n");
fptr=&functor;
puts("> Should see function()...");
fptr->invoke();
fptr1=&functor1;
puts("> Should see function(2)...");
fptr1->invoke(2);
fptr2=&functor2;
puts("> Should see function(true, \"argument 2\")...");
fptr2->invoke(true, "argument 2");
}
/*
Test global functors with return, ArGlobalRetFunctor.
*/
// Direct invocation of the global functors with supplying parameters.
void testGlobalReturnDirect()
{
ArGlobalRetFunctor<bool> functor(&retFunction);
ArGlobalRetFunctor1<char*, int> functor1(&retFunction, 1);
ArGlobalRetFunctor2<double, bool, std::string>
functor2(&retFunction, false, "default arg");
bool bret;
char *cret;
double dret;
printf("\n****** Testing global direct invocation with return\n");
puts("> bool retFunction() should return true...");
bret=functor.invokeR();
printf("Returned: %d\n", bret);
puts("> char* retFunction(5) should return \"Hello\"...");
cret=functor1.invokeR(5);
printf("Returned: %s\n", cret);
puts("> double retFunction(true, \"argument 1\") should return 4.62...");
dret=functor2.invokeR(true, "argument 1");
printf("Returned: %e\n", dret);
}
// Invocation of a base ArFunctor pointer to a global functor. Because the
// pointer is of type ArFunctor, the parameters can not be supplied. The
// default parameters, which are supplied when the functor is constructed,
// are used.
void testGlobalReturnBase()
{
ArGlobalRetFunctor<bool> functor(retFunction);
ArGlobalRetFunctor1<char*, int> functor1(retFunction, 1);
ArGlobalRetFunctor2<double, bool, std::string>
functor2(retFunction, false, "default arg");
ArRetFunctor<bool> *fBoolPtr;
ArRetFunctor<char*> *fCharPtr;
ArRetFunctor<double> *fDoublePtr;
bool bret;
char *cret;
double dret;
printf("\n****** Testing global base invocation with return\n");
fBoolPtr=&functor;
puts("> bool retFunction() should return true...");
bret=fBoolPtr->invokeR();
printf("Returned: %d\n", bret);
fCharPtr=&functor1;
puts("> char* retFunction(1) should return \"Hello\"...");
cret=fCharPtr->invokeR();
printf("Returned: %s\n", cret);
fDoublePtr=&functor2;
puts("> double retFunction(false, \"default arg\") should return 4.62...");
dret=fDoublePtr->invokeR();
printf("Returned: %e\n", dret);
}
// Invocation of pointers which supply the parameter type. Full invocation
// with paramters is posesible in this fashion with out knowing the class
// that the functor refers to.
void testGlobalReturnParams()
{
ArGlobalRetFunctor<bool> functor(retFunction);
ArGlobalRetFunctor1<char*, int> functor1(retFunction, 1);
ArGlobalRetFunctor2<double, bool, std::string>
functor2(retFunction, false, "default arg");
ArRetFunctor<bool> *fBoolPtr;
ArRetFunctor1<char*, int> *fCharPtr;
ArRetFunctor2<double, bool, std::string> *fDoublePtr;
bool bret;
char *cret;
double dret;
printf("\n****** Testing global pointer invocation with return\n");
fBoolPtr=&functor;
puts("> bool retFunction() should return true...");
bret=fBoolPtr->invokeR();
printf("Returned: %d\n", bret);
fCharPtr=&functor1;
puts("> char* retFunction(7) should return \"Hello\"...");
cret=fCharPtr->invokeR(7);
printf("Returned: %s\n", cret);
fDoublePtr=&functor2;
puts("> double retFunction(false, \"argument 3\") should return 4.62...");
dret=fDoublePtr->invokeR(false, "argument 3");
printf("Returned: %e\n", dret);
}
// main(). Drives this example by creating an instance of the TestClass and
// instances of functors. Then the functors are invoked.
int main()
{
testDirect();
testBase();
testParams();
testReturnDirect();
testReturnBase();
testReturnParams();
testGlobalDirect();
testGlobalBase();
testGlobalParams();
testGlobalReturnDirect();
testGlobalReturnBase();
testGlobalReturnParams();
ArGlobalFunctor2<bool, std::string> f(&function);
f.setP2("hello");
f.invoke(true);
return(0);
}
|