I have a function pointer for the deleter, but most of the time, the deletor will not be needed only when I save an internal copy of something. I am currently doing this with the noop deleter function:
class MyClass{
public:
template<class Type>
void Bind(Type* obj){
Cleanup();
object_ = obj;
}
template<class Type>
void Bind(Type obj){
Cleanup();
object_ = new Type(obj);
deleter = &Deleter<Type>;
}
private:
template<class Type>
static void Deleter(void* obj_ptr){
Type* obj = static_cast<Type*>(obj_ptr);
delete obj;
}
static void NoopDeleter(void* unused){
}
void Cleanup(){
(*deleter_)(object_);
object_ = 0;
deleter_ = &NoopDeleter;
}
typedef void (*DeleterFunc)(void*);
void* object_;
DeleterFunc deleter_;
};
Now, the obvious other choice would be to install it 0when it is not needed, and test the function Cleanupwith if(deleter_ != 0) (*deleter_)(object_).
Now, while coding, it occurred to me: βHmm, which version will be faster?β, So this is more of a personal interest than an optimization. Sorry if the question sounds kind of silly, but it bothers me and I really want to know. Thanks for any answers in advance!
source
share