Menu

Delegate.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  * All rights reserved.
00005  *
00006  *    Redistribution and use in source and binary forms, with or without
00007  *    modification, are permitted provided that the following conditions
00008  *    are met:
00009  *
00010  *    * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *
00013  *    * Redistributions in binary form must reproduce the above copyright
00014  *      notice, this list of conditions and the following disclaimer in
00015  *      the documentation and/or other materials provided with the
00016  *      distribution.
00017  *
00018  *    * Neither the name of the copyright holders nor the names of
00019  *      contributors may be used to endorse or promote products derived
00020  *      from this software without specific prior written permission.
00021  *
00022  *
00023  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00025  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00026  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *
00036  * $Id$
00037  *
00038  ******************************************************************************/
00039 
00040 #ifndef __Delagate_h__
00041 #define __Delagate_h__
00042 
00043 namespace famouso {
00044     namespace util {
00045 
00050         template < typename parameter=void, typename R=void >
00051         class Delegate {
00052                 typedef R (*invoke_stub)(void const *, parameter);
00053                 void const  *obj_ptr_;
00054                 volatile invoke_stub stub_ptr_;
00055 
00056                 template < typename T, R (T::*Fxn)(parameter) >
00057                 struct mem_fn_stub {
00058                     static R invoke(void const * obj_ptr, parameter a0) {
00059                         T * obj = static_cast<T *>(const_cast<void *>(obj_ptr));
00060                         return (obj->*Fxn)(a0);
00061                     }
00062                 };
00063 
00064                 template < typename T, R (T::*Fxn)(parameter) const >
00065                 struct mem_fn_const_stub {
00066                     static R invoke(void const * obj_ptr, parameter a0) {
00067                         T const * obj = static_cast<T const *>(obj_ptr);
00068                         return (obj->*Fxn)(a0);
00069                     }
00070                 };
00071 
00072                 template < R (*Fxn)(parameter) >
00073                 struct function_stub {
00074                     static R invoke(void const *, parameter a0) {
00075                         return (*Fxn)(a0);
00076                     }
00077                 };
00078 
00079             public:
00080                 Delegate() : obj_ptr_(0), stub_ptr_(0) { }
00081 
00082                 template < typename T, R (T::*Fxn)(parameter) >
00083                 void bind(T * obj) {
00084                     obj_ptr_ = const_cast<T const *>(obj);
00085                     stub_ptr_ = &mem_fn_stub<T, Fxn>::invoke;
00086                 }
00087 
00088                 template < typename T, R (T::*Fxn)(parameter) const >
00089                 void bind(T const * obj) {
00090                     obj_ptr_ = obj;
00091                     stub_ptr_ = &mem_fn_const_stub<T, Fxn>::invoke;
00092                 }
00093 
00094                 template < R (*Fxn)(parameter) >
00095                 void bind() {
00096                     obj_ptr_ = 0;
00097                     stub_ptr_ = &function_stub<Fxn>::invoke;
00098                 }
00099 
00100                 //  additional attributes only support on newer compilers
00101                 R operator()(parameter a0) const {
00102                     return (*stub_ptr_)(obj_ptr_, a0);
00103                 }
00104 
00105                 operator bool() const {
00106                     return stub_ptr_;
00107                 }
00108         };
00109 
00110         template<>
00111         class Delegate<void> {
00112                 typedef void (*invoke_stub)(void const *);
00113                 void const  *obj_ptr_;
00114                 volatile invoke_stub stub_ptr_;
00115 
00116                 template < typename T, void (T::*Fxn)() >
00117                 struct mem_fn_stub {
00118                     static void invoke(void const * obj_ptr) {
00119                         T * obj = static_cast<T *>(const_cast<void *>(obj_ptr));
00120                         return (obj->*Fxn)();
00121                     }
00122                 };
00123 
00124                 template < typename T, void (T::*Fxn)() const >
00125                 struct mem_fn_const_stub {
00126                     static void invoke(void const * obj_ptr) {
00127                         T const * obj = static_cast<T const *>(obj_ptr);
00128                         return (obj->*Fxn)();
00129                     }
00130                 };
00131 
00132                 template < void (*Fxn)() >
00133                 struct function_stub {
00134                     static void invoke(void const *) {
00135                         return (*Fxn)();
00136                     }
00137                 };
00138 
00139             public:
00140                 Delegate() : obj_ptr_(0), stub_ptr_(0) { }
00141 
00142                 template < typename T, void (T::*Fxn)() >
00143                 void bind(T * obj) {
00144                     obj_ptr_ = const_cast<T const *>(obj);
00145                     stub_ptr_ = &mem_fn_stub<T, Fxn>::invoke;
00146                 }
00147 
00148                 template < typename T, void (T::*Fxn)() const >
00149                 void bind(T const * obj) {
00150                     obj_ptr_ = obj;
00151                     stub_ptr_ = &mem_fn_const_stub<T, Fxn>::invoke;
00152                 }
00153 
00154                 template < void (*Fxn)() >
00155                 void bind() {
00156                     obj_ptr_ = 0;
00157                     stub_ptr_ = &function_stub<Fxn>::invoke;
00158                 }
00159 
00160                 //  additional attributes only support on newer compilers
00161                 void operator()() const {
00162                     return (*stub_ptr_)(obj_ptr_);
00163                 }
00164 
00165                 operator bool() const {
00166                     return stub_ptr_;
00167                 }
00168         };
00169 
00170     } // namespace util
00171 } // namespace famouso
00172 #endif