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.
Simple callback system...
#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
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.