lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
|
00001 /*****************************************************/ 00002 /* lean Functional (c) Tobias Zirr 2011 */ 00003 /*****************************************************/ 00004 00005 #ifndef LEAN_FUNCTIONAL_CALLABLE 00006 #define LEAN_FUNCTIONAL_CALLABLE 00007 00008 #include "../lean.h" 00009 #include "../tags/noncopyable.h" 00010 00011 namespace lean 00012 { 00013 namespace functional 00014 { 00015 00017 template <class Signature> 00018 class callable_fun 00019 { 00020 private: 00021 Signature *m_fun; 00022 00023 public: 00025 LEAN_INLINE callable_fun(Signature *fun) 00026 : m_fun(fun) 00027 { 00028 LEAN_ASSERT(m_fun); 00029 } 00031 LEAN_INLINE void operator ()() 00032 { 00033 (*m_fun)(); 00034 } 00035 }; 00036 00038 template <class Class, class Signature> 00039 class callable_memfun 00040 { 00041 private: 00042 Class *m_obj; 00043 Signature Class::*m_fun; 00044 00045 public: 00047 LEAN_INLINE callable_memfun(Class *obj, Signature Class::*fun) 00048 : m_obj(obj), 00049 m_fun(fun) 00050 { 00051 LEAN_ASSERT(m_obj); 00052 LEAN_ASSERT(m_fun); 00053 } 00055 LEAN_INLINE void operator ()() 00056 { 00057 (m_obj->*m_fun)(); 00058 } 00059 }; 00060 00062 template <class Signature> 00063 LEAN_INLINE callable_fun<Signature> make_callable(Signature *fun) 00064 { 00065 return callable_fun<Signature>(fun); 00066 } 00067 00069 template <class Class, class Signature> 00070 LEAN_INLINE callable_memfun<Class, Signature> make_callable(Class *obj, Signature Class::*fun) 00071 { 00072 return callable_memfun<Class, Signature>(obj, fun); 00073 } 00074 00075 } // namespace 00076 00077 using functional::callable_fun; 00078 using functional::callable_memfun; 00079 00080 using functional::make_callable; 00081 00082 } // namespace 00083 00084 #endif