Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

functorExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 
00027 
00028 #include <string>
00029 #include "Aria.h"
00030 
00031 
00037 /*
00038   This is a class that has some callback methods. Functors which refer to these
00039   callbacks will be passed to the DriverClass.  
00040 */
00041 class CallbackContainer
00042 {
00043 public:
00044 
00045   void callback1();
00046   void callback2(int i);
00047   bool callback3(const char *str);
00048 };
00049 
00050 void CallbackContainer::callback1()
00051 {
00052   printf("CallbackContainer::callback1 called.\n");
00053 }
00054 
00055 void CallbackContainer::callback2(int i)
00056 {
00057   printf("CallbackContainer::callback2 called with argument of '%d'\n", i);
00058 }
00059 
00060 bool CallbackContainer::callback3(const char *str)
00061 {
00062   printf("CallbackContainer::callback3 called with argument of '%s'.\n", str);
00063   return(true);
00064 }
00065 
00066 /* 
00067  * Functors can also invoke global functions.
00068  */
00069 
00070 void globalCallback()
00071 {
00072   printf("globalCallback() called.\n");
00073 }
00074 
00075 
00076 /*
00077   This is a "driver" class. It takes three functors of different types and
00078   will invoke the three functors. This is a typical use of
00079   functors: to pass information or event notifications between loosely
00080   coupled objects.
00081 */
00082 class DriverClass
00083 {
00084 public:
00085 
00086   void invokeFunctors();
00087 
00088   void setCallback1(ArFunctor *func) {myFunc1=func;}
00089   void setCallback2(ArFunctor1<int> *func) {myFunc2=func;}
00090   void setCallback3(ArRetFunctor1<bool, const char *> *func) {myFunc3=func;}
00091 
00092 
00093 protected:
00094 
00095   ArFunctor *myFunc1;
00096   ArFunctor1<int> *myFunc2;
00097   ArRetFunctor1<bool, const char *> *myFunc3;
00098 };
00099 
00100 void DriverClass::invokeFunctors()
00101 {
00102   bool ret;
00103 
00104   printf("Invoking functor1... ");
00105   myFunc1->invoke();
00106 
00107   printf("Invoking functor2... ");
00108   myFunc2->invoke(23);
00109      
00110 
00111   /*
00112      For functors with return values, use invorkeR() instead of invoke()
00113      to get the return value.  The invoke() function can also be used to invoke
00114      the functor, but the return value is lost.  (And is a possible source
00115      of memory leaks if you were supposed to free a pointer returned.)
00116   */
00117   printf("Invoking functor3... ");
00118   ret=myFunc3->invokeR("This is a string argument");
00119   if (ret)
00120     printf("\t-> functor3 returned 'true'\n");
00121   else
00122     printf("\t-> functor3 returned 'false'\n");
00123 }
00124 
00125 int main()
00126 {
00127   CallbackContainer cb;
00128   DriverClass driver;
00129 
00130   ArFunctorC<CallbackContainer> functor1(cb, &CallbackContainer::callback1);
00131   ArFunctor1C<CallbackContainer, int> functor2(cb, &CallbackContainer::callback2);
00132   ArRetFunctor1C<bool, CallbackContainer, const char *>
00133     functor3(cb, &CallbackContainer::callback3);
00134 
00135   driver.setCallback1(&functor1);
00136   driver.setCallback2(&functor2);
00137   driver.setCallback3(&functor3);
00138 
00139   driver.invokeFunctors();
00140 
00141   /* You can make functors that target global functions too. */
00142   ArGlobalFunctor globalFunctor(&globalCallback);
00143   printf("Invoking globalFunctor... ");
00144   globalFunctor.invoke();
00145 
00146   /* You can also include the values of arguments in an ArFunctor object, if you
00147    * want to use the same value in every invocation of the functor.
00148    */
00149   ArFunctor1C<CallbackContainer, int> functor4(cb, &CallbackContainer::callback2, 42);
00150   printf("Invoking functor with constant argument... ");
00151   functor4.invoke();
00152 
00153   /* Functors can be downcast to parent interface classes, as long as their invocation
00154    * does not require arguments.
00155    */
00156   ArFunctor* baseFunctor = &functor4;
00157   printf("Invoking downcast functor... ");
00158   baseFunctor->invoke();
00159 
00160 
00161   return(0);
00162 }

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0