sprint 1-alpha
sprint/function/simple_function.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::function
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SPRINT_FUNCTION_H
00023 #define _SPRINT_FUNCTION_H
00024 
00028 namespace sprint {
00029 
00034 struct simple_function {
00035        
00036        typedef void (* Type)(void *);
00037        
00038        private:
00039         
00041        static void nothing(void *f)
00042        {
00043        }
00044 
00046        template< void (* F)(void)>
00047        static void delegate_fun(void *f)
00048        {
00049          F();
00050        }
00051 
00053        template<class T, void (T::*F)(void)>
00054        static void delegate_mem_fun(void * t)
00055        {
00056        ((reinterpret_cast<T*>(t))->*F)();
00057        }
00058        
00059        public:
00060        // the function jumper
00061        Type m_func;
00063        void * m_param;
00064        
00065        public:
00066        simple_function() : m_func(&nothing) { }
00067        ~simple_function() { }
00068        
00070        bool is_nothing() const { return m_func == &nothing; }
00071        
00073        void operator()(void) { m_func(m_param); }
00074        
00076        void ptr_fun(void (* func) (void *), void *param) 
00077          {
00078           m_func = func;
00079           m_param = param;
00080          }
00081 
00083        template<void (* F)(void)>
00084        void bind() 
00085          {
00086           m_func = &delegate_fun<F>;
00087           m_param = NULL;
00088          }
00089 
00091        template<class T, void (T::*F)(void)>
00092        void bind(T * _this) 
00093          {
00094           m_func = &delegate_mem_fun<T,F>;
00095           m_param = reinterpret_cast<void *>(_this);
00096          }
00097 
00098        };
00099        
00100 };
00101 
00102 
00103 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines