Simple callback system...

Echo89

OK, I've taken advice from some members here, and members from another forum. And I've come up with this, thouht I'd just share it with you.

203 Views
About the Author
#include <iostream>
#include <string>

class CallbackWrapper
{
    public:
        virtual ~CallbackWrapper() { };
        virtual void Call() = 0;
};

template <typename MT> class CallbackFunction : public CallbackWrapper
{
    typedef void (*CALLBACK)(MT);
    CALLBACK func;
    MT data;
    public:
        CallbackFunction() { };
        void SetData(void (*f)(MT), MT d)
        {
            func = f;
            data = d;
        }
        void Call()
        {
            func(data);
        }
};

class Callback
{
    CallbackWrapper *CallbackClass;
    public:
        Callback() { };
        ~Callback()
        {
            delete CallbackClass;
        }
        template <typename T> void SetCallback(CallbackFunction <T> *func)
        {
            CallbackClass = func;
        }
        void Call()
        {
            CallbackClass->Call();
        }
};

template <typename CT> Callback NewCallback(void (*func)(CT), CT data)
{
    CallbackFunction <CT> *cf = new CallbackFunction <CT>;
    cf->SetData(func, data);
    Callback cb;
    cb.SetCallback(cf);
    return cb;
};

void Call(Callback CallbackFunc)
{
    CallbackFunc.Call();
}

void foo(std::string str)
{
    std::cout << str << "\n";
}

int main()
{
    std::string str;
    str.append("Hello, World!");
    Call( NewCallback(&foo, str) );
    return 0;
}
dexblack_1 14 Newbie Poster

I recommend you add assertions to ensure correctness in your Debug build.

void Call()
{
    assert(CallbackClass != nullptr);
    CallbackClass->Call();
}

Try to use class member names that don't look like class type names.

Returning a Callback object by copy that is expected to manage its copy of the CallbackClass object is a memory leak waiting to happen.
Have a good look at the std::unique_ptr or boost::shared_ptr and wrap EVERY heap memory allocation using 'new' with such a smart pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.