//////////////////////////////////////////////////////////////////////////////// // The Loki Library // Copyright (c) 2005 Peter Kümmel // // Code covered by the MIT License // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //////////////////////////////////////////////////////////////////////////////// #ifndef LOKI_FUNCTION_INC_ #define LOKI_FUNCTION_INC_ // $Id$ #define LOKI_ENABLE_FUNCTION #include #include #include namespace Loki { //////////////////////////////////////////////////////////////////////////////// /// \struct Function /// /// \ingroup FunctorGroup /// Allows a boost/TR1 like usage of Functor. /// /// \par Usage /// /// - free functions: e.g. \code Function f(&freeFunction); /// \endcode /// - member functions: e.g \code Function f(&object,&ObjectType::memberFunction); /// \endcode /// /// see also test/Function/FunctionTest.cpp (the modified test program from boost) //////////////////////////////////////////////////////////////////////////////// template struct Function; template struct Function : public Functor { typedef Functor FBase; Function() : FBase() {} Function(const Function& func) : FBase() { if( !func.empty()) FBase::operator=(func); } // test on emptiness template Function(Function func) : FBase() { if(!func.empty()) FBase::operator=(func); } // clear by '= 0' Function(const int i) : FBase() { if(i==0) FBase::clear(); else throw std::runtime_error("Loki::Function(const int i): i!=0"); } template Function(Func func) : FBase(func) {} template Function(const Host& host, const Func& func) : FBase(host,func) {} }; //////////////////////////////////////////////////////////////////////////////// // macros for the repetitions //////////////////////////////////////////////////////////////////////////////// #define LOKI_FUNCTION_BODY \ \ Function() : FBase() {} \ \ Function(const Function& func) : FBase() \ { \ if( !func.empty()) \ FBase::operator=(func); \ } \ \ Function(const int i) : FBase() \ { \ if(i==0) \ FBase::clear(); \ else \ throw std::runtime_error( \ "Loki::Function(const int i): i!=0"); \ } \ \ template \ Function(Func func) : FBase(func) {} \ \ template \ Function(const Host& host, const Func& func): FBase(host,func) {} #define LOKI_FUNCTION_R2_CTOR_BODY \ \ : FBase() \ { \ if(!func.empty()) \ FBase::operator=(func); \ } //////////////////////////////////////////////////////////////////////////////// // repetitions //////////////////////////////////////////////////////////////////////////////// template<> struct Function<> : public Loki::Functor<> { typedef Functor<> FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY // if compilation breaks here then // Function.h was not included before // Functor.h, check your include order // or define LOKI_ENABLE_FUNCTION }; template struct Function : public Loki::Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor >FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; template struct Function : public Functor > { typedef Functor > FBase; template Function(Function func) LOKI_FUNCTION_R2_CTOR_BODY LOKI_FUNCTION_BODY }; }// namespace Loki #endif // end file guardian