Delegate.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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
00161 void operator()() const {
00162 return (*stub_ptr_)(obj_ptr_);
00163 }
00164
00165 operator bool() const {
00166 return stub_ptr_;
00167 }
00168 };
00169
00170 }
00171 }
00172 #endif