1 /**************************************************************************
2 * callback.hxx -- Wrapper classes to treat function and method pointers
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 **************************************************************************/
22 #ifndef _SG_CALLBACK_HXX
23 #define _SG_CALLBACK_HXX
26 * Abstract base class for all callbacks.
35 virtual ~SGCallback() {}
40 virtual SGCallback* clone() const = 0;
43 * Execute the callback function.
45 virtual void operator()() = 0;
55 void operator=( const SGCallback& );
59 * Callback for invoking a file scope function.
61 template< typename Fun >
62 class SGFunctionCallback : public SGCallback
68 SGFunctionCallback( const Fun& fun )
69 : SGCallback(), f_(fun) {}
71 SGCallback* clone() const
73 return new SGFunctionCallback( *this );
76 void operator()() { f_(); }
87 * Callback for invoking a member function.
89 template< class ObjPtr, typename MemFn >
90 class SGMethodCallback : public SGCallback
97 SGMethodCallback( const ObjPtr& pObj, MemFn pMemFn )
107 SGCallback* clone() const
109 return new SGMethodCallback( *this );
117 ((*pObj_).*pMemFn_)();
130 * Helper template functions.
133 template< typename Fun >
135 make_callback( const Fun& fun )
137 return new SGFunctionCallback<Fun>(fun);
140 template< class ObjPtr, typename MemFn >
142 make_callback( const ObjPtr& pObj, MemFn pMemFn )
144 return new SGMethodCallback<ObjPtr,MemFn>(pObj, pMemFn );
147 #endif // _SG_CALLBACK_HXX