User Manual, Developers Guide and API Documentation

Functor.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_FUNCTOR_HPP
00029 #define WNS_FUNCTOR_HPP
00030 
00031 namespace wns {
00035     template<typename RetVal = void>
00036     class Functor
00037     {
00038     public:
00042         virtual RetVal
00043         operator()() const = 0;
00044 
00045         virtual
00046         ~Functor()
00047         {}
00048     };
00049 
00053     template<typename Arg, typename RetVal = void>
00054     class ArgumentFunctor
00055     {
00056     public:
00060         virtual RetVal
00061         operator()(Arg) const = 0;
00062 
00063         virtual
00064         ~ArgumentFunctor()
00065         {}
00066     };
00067 
00071     namespace tfunctor {
00072 
00076         enum funcType { constFunc, nonConstFunc };
00077 
00081         template<typename T, typename RetVal, funcType F>
00082         struct ConstructFPtrType
00083         {
00084             typedef RetVal (T::*fPtr)();
00085         };
00086 
00090         template<typename T, typename RetVal>
00091         struct ConstructFPtrType<T, RetVal, constFunc>
00092         {
00093             typedef RetVal (T::*fPtr)() const;
00094         };
00095     }
00096 
00104     template<
00105         typename T,
00106         typename RetVal = void,
00107         tfunctor::funcType funcType = tfunctor::nonConstFunc,
00108         typename fPtr = typename tfunctor::ConstructFPtrType<T, RetVal, funcType>::fPtr >
00109     class TFunctor : public Functor<RetVal>
00110     {
00111     public:
00112         typedef fPtr functionPointer;
00116         TFunctor(T* Obj, fPtr F) : obj(Obj), f(F) {}
00117 
00121         virtual RetVal operator()() const { return (*obj.*f)(); }
00122 
00123     private:
00127         T* obj;
00131         fPtr f;
00132     };
00133 
00137     namespace targumentfunctor {
00138         using tfunctor::funcType;
00139         using tfunctor::constFunc;
00140         using tfunctor::nonConstFunc;
00141 
00145         template<typename T, typename Arg, typename RetVal, funcType F>
00146         struct ConstructFPtrType
00147         {
00148             typedef RetVal (T::*fPtr)(Arg);
00149         };
00150 
00154         template<typename T, typename Arg, typename RetVal>
00155         struct ConstructFPtrType<T, Arg, RetVal, constFunc>
00156         {
00157             typedef RetVal (T::*fPtr)(Arg) const;
00158         };
00159     }
00160 
00167     template<
00168         typename T,
00169         typename Arg,
00170         typename RetVal = void,
00171         targumentfunctor::funcType funcType = targumentfunctor::nonConstFunc,
00172         typename fPtr = typename targumentfunctor::ConstructFPtrType<T, Arg, RetVal, funcType>::fPtr >
00173     class TArgumentFunctor : public ArgumentFunctor<Arg, RetVal>
00174     {
00175     public:
00179         TArgumentFunctor(T* Obj, fPtr F) : obj(Obj), f(F) {};
00180 
00184         virtual RetVal operator()(Arg a) const { return (*obj.*f)(a); };
00185 
00186     private:
00190         T* obj;
00194         fPtr f;
00195     };
00196 }
00197 
00198 #endif // WNS_FUNCTOR_HPP
00199 
00200 

Generated on Thu May 24 03:31:36 2012 for openWNS by  doxygen 1.5.5