patterncppMinor
Anything I'm missing on this delegate implementation?
Viewed 0 times
thisanythingdelegatemissingimplementation
Problem
Just as the question states and to be sure, am I missing anything on this? Something important/obvious that I overlooked? As is, I can save any functor, function pointer and member function pointer using this delegate class (copy ctor and assignment operator left out for brevity, as were the versions with parameters).
For the delegate in eff
template
void Deleter(void* object){
Type* obj_ptr = static_cast(object);
delete obj_ptr;
}
template
struct DelegateHelper{
typedef typename Functor::result_type result_type;
DelegateHelper(Functor func)
: func_(func)
{}
result_type operator()(){
return func_();
}
Functor func_;
};
template
struct DelegateHelper{
typedef R (Obj::*FuncPtr)();
DelegateHelper(Obj* obj_ptr, FuncPtr func)
: object_(obj_ptr)
, func_(func)
{}
R operator()(){
return (object_->*func_)();
}
Obj* object_;
FuncPtr func_;
};
template
struct DelegateHelper{
typedef R (*FuncPtr)();
DelegateHelper(FuncPtr func)
: func_(func)
{}
R operator()(){
return (*func_)();
}
FuncPtr func_;
};
template
struct Delegate;
template
struct Delegate{
typedef R (*SinkPtr)(void*);
Delegate()
: object_(0)
, func_(0)
, deleter_(0)
{}
template
Delegate(F func)
: object_(new DelegateHelper(func))
, func_(&Sink >)
, deleter_(&Deleter >)
{}
template
Delegate(C* obj, F func)
: object_(new DelegateHelper(obj, func))
, func_(&Sink >)
, deleter_(&Deleter >)
{}
~Delegate(){
if(deleter_)
(*deleter_)(object_);
}
R operator()(){
return (*func_)(object_);
}
template
static R Sink(void* obj_ptr){
Type* object = (Type*)obj_ptr;
return (*object)();
}
typedef void (*DeleteFunc)(void*);
void* object_;
SinkPtr func_;
DeleteFunc deleter_;
};For the delegate in eff
Solution
You are missing const and volatile qualified method pointer versions.
Example:
Also, you may (probably) need specializations or internal delegates for handling of functions with void return values. Someone else may be able to fill in the details, but it seems some compilers are OK with
And others are not.
Another thing that makes the boost version and some others larger is being able to accept & or * arguments for the object value when calling methods.
Example:
template
struct DelegateHelper{
typedef R (Obj::*FuncPtr)() const;
DelegateHelper(const Obj* obj_ptr, FuncPtr func)
: object_(obj_ptr)
, func_(func)
{}
R operator()(){
return (object_->*func_)();
}
const Obj* object_;
FuncPtr func_;
};Also, you may (probably) need specializations or internal delegates for handling of functions with void return values. Someone else may be able to fill in the details, but it seems some compilers are OK with
return function_returning_void();And others are not.
Another thing that makes the boost version and some others larger is being able to accept & or * arguments for the object value when calling methods.
Code Snippets
template<class Obj, class R>
struct DelegateHelper<R (Obj::*)() const>{
typedef R (Obj::*FuncPtr)() const;
DelegateHelper(const Obj* obj_ptr, FuncPtr func)
: object_(obj_ptr)
, func_(func)
{}
R operator()(){
return (object_->*func_)();
}
const Obj* object_;
FuncPtr func_;
};return function_returning_void();Context
StackExchange Code Review Q#1388, answer score: 2
Revisions (0)
No revisions yet.