///////////////////////////////////////////////////////////////////// // TestFunctors.cpp - Illustrates use of Loki's Functor class // // // // Loki is a template-based library developed by the author of: // // Modern C++ Design, Andrei Alexandrescu, Addison-Wesley, 2001 // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2004 // ///////////////////////////////////////////////////////////////////// // Required Files: // // TestFunctors.cpp, Functor.h, Singleton.h, Singleton.cpp, // // SmallObj.h, SmallObj.cpp, static_check.h, Threads.h, // // TypeManip.h, TypeList.h, NullType, TypeTraits.h // // // // Note: You must enable RTTI // // Properties\C/C++\Language\Enable RTTI // ///////////////////////////////////////////////////////////////////// // // template // < // typename ReturnType, // class TList = NullType, // template class ThreadingModel = DEFAULT_THREADING // > // class Functor // #include "Functor.h" #include using namespace Loki; void GlobalFunction(int arg) { std::cout << "\n Global function called with arg = " << arg; } class TestFunctor { public: int operator()(int i, double d) { std::cout << "\n TestFunctior called with arg1 = " << i << " and arg2 = " << d; return i; } }; class TestClass { public: void myFun(double d) { std::cout << "\n TestClass::myFun called with arg = " << d; } }; // ///////////////////////////////////////////////////////////////////// // Demonstration void main() { std::cout << "\n Demonstrating Loki::Functor class " << "\n ===================================\n"; std::cout << "\n Functor with global function:" << "\n -------------------------------"; Functor cmd1(GlobalFunction); cmd1(3); std::cout << std::endl; std::cout << "\n Functor with function pointer:" << "\n -------------------------------"; typedef void(*pFun)(int); pFun ptr = GlobalFunction; Functor cmd5(ptr); cmd5(-15); std::cout << std::endl; std::cout << "\n Functor with ordinary functor:" << "\n --------------------------------"; TestFunctor tf; Functor cmd2(tf); int ret = cmd2(3,3.1415927); std::cout << ". Return value = " << ret; std::cout << std::endl; std::cout << "\n Functor with member pointer" << "\n -----------------------------"; TestClass myInstance; Functor cmd3(&myInstance, &TestClass::myFun); cmd3(17); std::cout << std::endl; std::cout << "\n Functor with Loki Functor:" << "\n ----------------------------"; Functor cmd4(cmd1); cmd4(-2); std::cout << "\n\n"; }