functorExample.cpp

Illustrates the use of functors

See also:
Functors in the ARIA overview
00001 
00002 
00003 #include <string>
00004 #include "Aria.h"
00005 
00006 
00012 /*
00013   This is a class that has some callback methods. Functors which refer to these
00014   callbacks will be passed to the DriverClass.  
00015 */
00016 class CallbackContainer
00017 {
00018 public:
00019 
00020   void callback1();
00021   void callback2(int i);
00022   bool callback3(const char *str);
00023 };
00024 
00025 void CallbackContainer::callback1()
00026 {
00027   printf("CallbackContainer::callback1 called.\n");
00028 }
00029 
00030 void CallbackContainer::callback2(int i)
00031 {
00032   printf("CallbackContainer::callback2 called with argument of '%d'\n", i);
00033 }
00034 
00035 bool CallbackContainer::callback3(const char *str)
00036 {
00037   printf("CallbackContainer::callback3 called with argument of '%s'.\n", str);
00038   return(true);
00039 }
00040 
00041 /* 
00042  * Functors can also invoke global functions.
00043  */
00044 
00045 void globalCallback()
00046 {
00047   printf("globalCallback() called.\n");
00048 }
00049 
00050 
00051 /*
00052   This is a "driver" class. It takes three functors of different types and
00053   will invoke the three functors. This is a typical use of
00054   functors: to pass information or event notifications between loosely
00055   coupled objects.
00056 */
00057 class DriverClass
00058 {
00059 public:
00060 
00061   void invokeFunctors();
00062 
00063   void setCallback1(ArFunctor *func) {myFunc1=func;}
00064   void setCallback2(ArFunctor1<int> *func) {myFunc2=func;}
00065   void setCallback3(ArRetFunctor1<bool, const char *> *func) {myFunc3=func;}
00066 
00067 
00068 protected:
00069 
00070   ArFunctor *myFunc1;
00071   ArFunctor1<int> *myFunc2;
00072   ArRetFunctor1<bool, const char *> *myFunc3;
00073 };
00074 
00075 void DriverClass::invokeFunctors()
00076 {
00077   bool ret;
00078 
00079   printf("Invoking functor1... ");
00080   myFunc1->invoke();
00081 
00082   printf("Invoking functor2... ");
00083   myFunc2->invoke(23);
00084      
00085 
00086   /*
00087      For functors with return values, use invorkeR() instead of invoke()
00088      to get the return value.  The invoke() function can also be used to invoke
00089      the functor, but the return value is lost.  (And is a possible source
00090      of memory leaks if you were supposed to free a pointer returned.)
00091   */
00092   printf("Invoking functor3... ");
00093   ret=myFunc3->invokeR("This is a string argument");
00094   if (ret)
00095     printf("\t-> functor3 returned 'true'\n");
00096   else
00097     printf("\t-> functor3 returned 'false'\n");
00098 }
00099 
00100 int main()
00101 {
00102   CallbackContainer cb;
00103   DriverClass driver;
00104 
00105   ArFunctorC<CallbackContainer> functor1(cb, &CallbackContainer::callback1);
00106   ArFunctor1C<CallbackContainer, int> functor2(cb, &CallbackContainer::callback2);
00107   ArRetFunctor1C<bool, CallbackContainer, const char *>
00108     functor3(cb, &CallbackContainer::callback3);
00109 
00110   driver.setCallback1(&functor1);
00111   driver.setCallback2(&functor2);
00112   driver.setCallback3(&functor3);
00113 
00114   driver.invokeFunctors();
00115 
00116   /* You can make functors that target global functions too. */
00117   ArGlobalFunctor globalFunctor(&globalCallback);
00118   printf("Invoking globalFunctor... ");
00119   globalFunctor.invoke();
00120 
00121   /* You can also include the values of arguments in an ArFunctor object, if you
00122    * want to use the same value in every invocation of the functor.
00123    */
00124   ArFunctor1C<CallbackContainer, int> functor4(cb, &CallbackContainer::callback2, 42);
00125   printf("Invoking functor with constant argument... ");
00126   functor4.invoke();
00127 
00128   /* Functors can be downcast to parent interface classes, as long as their invocation
00129    * does not require arguments.
00130    */
00131   ArFunctor* baseFunctor = &functor4;
00132   printf("Invoking downcast functor... ");
00133   baseFunctor->invoke();
00134 
00135 
00136   return(0);
00137 }

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7