/////////////////////////////////////////////////////////////////////// // File: callback.h // Description: classes and functions to replace pointer-to-functions // Author: Samuel Charron // // (C) Copyright 2006, Google Inc. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /////////////////////////////////////////////////////////////////////// #ifndef _CALLBACK_SPECIALIZATIONS_H #define _CALLBACK_SPECIALIZATIONS_H struct CallbackUtils_ { static void FailIsRepeatable(const char* name); }; class Closure { public: virtual ~Closure() { } virtual void Run() = 0; }; template class ResultCallback { public: virtual ~ResultCallback() { } virtual R Run() = 0; }; template class _ConstMemberResultCallback_0_0 : public ResultCallback { public: typedef ResultCallback base; typedef R (T::*MemberSignature)() const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_0( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run() { if (!del) { R result = (object_->*member_)(); return result; } else { R result = (object_->*member_)(); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _ConstMemberResultCallback_0_0 : public Closure { public: typedef Closure base; typedef void (T::*MemberSignature)() const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_0( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run() { if (!del) { (object_->*member_)(); } else { (object_->*member_)(); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _ConstMemberResultCallback_0_0::base* NewCallback( const T1* obj, R (T2::*member)() const) { return new _ConstMemberResultCallback_0_0( obj, member); } #endif #ifndef SWIG template inline typename _ConstMemberResultCallback_0_0::base* NewPermanentCallback( const T1* obj, R (T2::*member)() const) { return new _ConstMemberResultCallback_0_0( obj, member); } #endif template class _MemberResultCallback_0_0 : public ResultCallback { public: typedef ResultCallback base; typedef R (T::*MemberSignature)() ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_0( T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run() { if (!del) { R result = (object_->*member_)(); return result; } else { R result = (object_->*member_)(); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _MemberResultCallback_0_0 : public Closure { public: typedef Closure base; typedef void (T::*MemberSignature)() ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_0( T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run() { if (!del) { (object_->*member_)(); } else { (object_->*member_)(); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _MemberResultCallback_0_0::base* NewCallback( T1* obj, R (T2::*member)() ) { return new _MemberResultCallback_0_0( obj, member); } #endif #ifndef SWIG template inline typename _MemberResultCallback_0_0::base* NewPermanentCallback( T1* obj, R (T2::*member)() ) { return new _MemberResultCallback_0_0( obj, member); } #endif template class _FunctionResultCallback_0_0 : public ResultCallback { public: typedef ResultCallback base; typedef R (*FunctionSignature)(); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_0( FunctionSignature function) : function_(function) { } virtual R Run() { if (!del) { R result = (*function_)(); return result; } else { R result = (*function_)(); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; return result; } } }; template class _FunctionResultCallback_0_0 : public Closure { public: typedef Closure base; typedef void (*FunctionSignature)(); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_0( FunctionSignature function) : function_(function) { } virtual void Run() { if (!del) { (*function_)(); } else { (*function_)(); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; } } }; template inline typename _FunctionResultCallback_0_0::base* NewCallback(R (*function)()) { return new _FunctionResultCallback_0_0(function); } template inline typename _FunctionResultCallback_0_0::base* NewPermanentCallback(R (*function)()) { return new _FunctionResultCallback_0_0(function); } template class Callback1 { public: virtual ~Callback1() { } virtual void Run(A1) = 0; }; template class ResultCallback1 { public: virtual ~ResultCallback1() { } virtual R Run(A1) = 0; }; template class _ConstMemberResultCallback_0_1 : public ResultCallback1 { public: typedef ResultCallback1 base; typedef R (T::*MemberSignature)(A1) const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_1( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run(A1 a1) { if (!del) { R result = (object_->*member_)(a1); return result; } else { R result = (object_->*member_)(a1); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _ConstMemberResultCallback_0_1 : public Callback1 { public: typedef Callback1 base; typedef void (T::*MemberSignature)(A1) const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_1( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run(A1 a1) { if (!del) { (object_->*member_)(a1); } else { (object_->*member_)(a1); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _ConstMemberResultCallback_0_1::base* NewCallback( const T1* obj, R (T2::*member)(A1) const) { return new _ConstMemberResultCallback_0_1( obj, member); } #endif #ifndef SWIG template inline typename _ConstMemberResultCallback_0_1::base* NewPermanentCallback( const T1* obj, R (T2::*member)(A1) const) { return new _ConstMemberResultCallback_0_1( obj, member); } #endif template class _MemberResultCallback_0_1 : public ResultCallback1 { public: typedef ResultCallback1 base; typedef R (T::*MemberSignature)(A1) ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_1( T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run(A1 a1) { if (!del) { R result = (object_->*member_)(a1); return result; } else { R result = (object_->*member_)(a1); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _MemberResultCallback_0_1 : public Callback1 { public: typedef Callback1 base; typedef void (T::*MemberSignature)(A1) ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_1( T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run(A1 a1) { if (!del) { (object_->*member_)(a1); } else { (object_->*member_)(a1); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _MemberResultCallback_0_1::base* NewCallback( T1* obj, R (T2::*member)(A1) ) { return new _MemberResultCallback_0_1( obj, member); } #endif #ifndef SWIG template inline typename _MemberResultCallback_0_1::base* NewPermanentCallback( T1* obj, R (T2::*member)(A1) ) { return new _MemberResultCallback_0_1( obj, member); } #endif template class _FunctionResultCallback_0_1 : public ResultCallback1 { public: typedef ResultCallback1 base; typedef R (*FunctionSignature)(A1); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_1( FunctionSignature function) : function_(function) { } virtual R Run(A1 a1) { if (!del) { R result = (*function_)(a1); return result; } else { R result = (*function_)(a1); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; return result; } } }; template class _FunctionResultCallback_0_1 : public Callback1 { public: typedef Callback1 base; typedef void (*FunctionSignature)(A1); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_1( FunctionSignature function) : function_(function) { } virtual void Run(A1 a1) { if (!del) { (*function_)(a1); } else { (*function_)(a1); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; } } }; template inline typename _FunctionResultCallback_0_1::base* NewCallback(R (*function)(A1)) { return new _FunctionResultCallback_0_1(function); } template inline typename _FunctionResultCallback_0_1::base* NewPermanentCallback(R (*function)(A1)) { return new _FunctionResultCallback_0_1(function); } template class Callback2 { public: virtual ~Callback2() { } virtual void Run(A1,A2) = 0; }; template class ResultCallback2 { public: virtual ~ResultCallback2() { } virtual R Run(A1,A2) = 0; }; template class _ConstMemberResultCallback_0_2 : public ResultCallback2 { public: typedef ResultCallback2 base; typedef R (T::*MemberSignature)(A1,A2) const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_2( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run(A1 a1,A2 a2) { if (!del) { R result = (object_->*member_)(a1,a2); return result; } else { R result = (object_->*member_)(a1,a2); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _ConstMemberResultCallback_0_2 : public Callback2 { public: typedef Callback2 base; typedef void (T::*MemberSignature)(A1,A2) const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_2( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run(A1 a1,A2 a2) { if (!del) { (object_->*member_)(a1,a2); } else { (object_->*member_)(a1,a2); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _ConstMemberResultCallback_0_2::base* NewCallback( const T1* obj, R (T2::*member)(A1,A2) const) { return new _ConstMemberResultCallback_0_2( obj, member); } #endif #ifndef SWIG template inline typename _ConstMemberResultCallback_0_2::base* NewPermanentCallback( const T1* obj, R (T2::*member)(A1,A2) const) { return new _ConstMemberResultCallback_0_2( obj, member); } #endif template class _MemberResultCallback_0_2 : public ResultCallback2 { public: typedef ResultCallback2 base; typedef R (T::*MemberSignature)(A1,A2) ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_2( T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run(A1 a1,A2 a2) { if (!del) { R result = (object_->*member_)(a1,a2); return result; } else { R result = (object_->*member_)(a1,a2); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _MemberResultCallback_0_2 : public Callback2 { public: typedef Callback2 base; typedef void (T::*MemberSignature)(A1,A2) ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_2( T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run(A1 a1,A2 a2) { if (!del) { (object_->*member_)(a1,a2); } else { (object_->*member_)(a1,a2); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _MemberResultCallback_0_2::base* NewCallback( T1* obj, R (T2::*member)(A1,A2) ) { return new _MemberResultCallback_0_2( obj, member); } #endif #ifndef SWIG template inline typename _MemberResultCallback_0_2::base* NewPermanentCallback( T1* obj, R (T2::*member)(A1,A2) ) { return new _MemberResultCallback_0_2( obj, member); } #endif template class _FunctionResultCallback_0_2 : public ResultCallback2 { public: typedef ResultCallback2 base; typedef R (*FunctionSignature)(A1,A2); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_2( FunctionSignature function) : function_(function) { } virtual R Run(A1 a1,A2 a2) { if (!del) { R result = (*function_)(a1,a2); return result; } else { R result = (*function_)(a1,a2); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; return result; } } }; template class _FunctionResultCallback_0_2 : public Callback2 { public: typedef Callback2 base; typedef void (*FunctionSignature)(A1,A2); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_2( FunctionSignature function) : function_(function) { } virtual void Run(A1 a1,A2 a2) { if (!del) { (*function_)(a1,a2); } else { (*function_)(a1,a2); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; } } }; template inline typename _FunctionResultCallback_0_2::base* NewCallback(R (*function)(A1,A2)) { return new _FunctionResultCallback_0_2(function); } template inline typename _FunctionResultCallback_0_2::base* NewPermanentCallback(R (*function)(A1,A2)) { return new _FunctionResultCallback_0_2(function); } template class Callback3 { public: virtual ~Callback3() { } virtual void Run(A1,A2,A3) = 0; }; template class ResultCallback3 { public: virtual ~ResultCallback3() { } virtual R Run(A1,A2,A3) = 0; }; template class _ConstMemberResultCallback_0_3 : public ResultCallback3 { public: typedef ResultCallback3 base; typedef R (T::*MemberSignature)(A1,A2,A3) const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_3( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run(A1 a1,A2 a2,A3 a3) { if (!del) { R result = (object_->*member_)(a1,a2,a3); return result; } else { R result = (object_->*member_)(a1,a2,a3); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _ConstMemberResultCallback_0_3 : public Callback3 { public: typedef Callback3 base; typedef void (T::*MemberSignature)(A1,A2,A3) const; private: const T* object_; MemberSignature member_; public: inline _ConstMemberResultCallback_0_3( const T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run(A1 a1,A2 a2,A3 a3) { if (!del) { (object_->*member_)(a1,a2,a3); } else { (object_->*member_)(a1,a2,a3); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _ConstMemberResultCallback_0_3::base* NewCallback( const T1* obj, R (T2::*member)(A1,A2,A3) const) { return new _ConstMemberResultCallback_0_3( obj, member); } #endif #ifndef SWIG template inline typename _ConstMemberResultCallback_0_3::base* NewPermanentCallback( const T1* obj, R (T2::*member)(A1,A2,A3) const) { return new _ConstMemberResultCallback_0_3( obj, member); } #endif template class _MemberResultCallback_0_3 : public ResultCallback3 { public: typedef ResultCallback3 base; typedef R (T::*MemberSignature)(A1,A2,A3) ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_3( T* object, MemberSignature member) : object_(object), member_(member) { } virtual R Run(A1 a1,A2 a2,A3 a3) { if (!del) { R result = (object_->*member_)(a1,a2,a3); return result; } else { R result = (object_->*member_)(a1,a2,a3); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; return result; } } }; template class _MemberResultCallback_0_3 : public Callback3 { public: typedef Callback3 base; typedef void (T::*MemberSignature)(A1,A2,A3) ; private: T* object_; MemberSignature member_; public: inline _MemberResultCallback_0_3( T* object, MemberSignature member) : object_(object), member_(member) { } virtual void Run(A1 a1,A2 a2,A3 a3) { if (!del) { (object_->*member_)(a1,a2,a3); } else { (object_->*member_)(a1,a2,a3); // zero out the pointer to ensure segfault if used again member_ = NULL; delete this; } } }; #ifndef SWIG template inline typename _MemberResultCallback_0_3::base* NewCallback( T1* obj, R (T2::*member)(A1,A2,A3) ) { return new _MemberResultCallback_0_3( obj, member); } #endif #ifndef SWIG template inline typename _MemberResultCallback_0_3::base* NewPermanentCallback( T1* obj, R (T2::*member)(A1,A2,A3) ) { return new _MemberResultCallback_0_3( obj, member); } #endif template class _FunctionResultCallback_0_3 : public ResultCallback3 { public: typedef ResultCallback3 base; typedef R (*FunctionSignature)(A1,A2,A3); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_3( FunctionSignature function) : function_(function) { } virtual R Run(A1 a1,A2 a2,A3 a3) { if (!del) { R result = (*function_)(a1,a2,a3); return result; } else { R result = (*function_)(a1,a2,a3); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; return result; } } }; template class _FunctionResultCallback_0_3 : public Callback3 { public: typedef Callback3 base; typedef void (*FunctionSignature)(A1,A2,A3); private: FunctionSignature function_; public: inline _FunctionResultCallback_0_3( FunctionSignature function) : function_(function) { } virtual void Run(A1 a1,A2 a2,A3 a3) { if (!del) { (*function_)(a1,a2,a3); } else { (*function_)(a1,a2,a3); // zero out the pointer to ensure segfault if used again function_ = NULL; delete this; } } }; template inline typename _FunctionResultCallback_0_3::base* NewCallback(R (*function)(A1,A2,A3)) { return new _FunctionResultCallback_0_3(function); } template inline typename _FunctionResultCallback_0_3::base* NewPermanentCallback(R (*function)(A1,A2,A3)) { return new _FunctionResultCallback_0_3(function); } #endif /* _CALLBACK_SPECIALIZATIONS_H */