Page 1 of 1

Callback Library

Posted: Sat Sep 01, 2012 3:58 pm
by adikid89
Hi,
I just made a pretty nice callback library. It's easy to use in projects, just include a header file. It can make callbacks to normal functions, functors and member functions.
Most of the credit goes to http://www.newty.de/jakubik/callback.pdf as that's what I based the library upon. I just added return type and multiple parameters.

I'm pretty sure you'll find it useful. So here you go: link

The link includes a visual studio 2008 project with some examples of how to use it. Basic syntax looks like this:

Code: Select all

   omg o;
   //   ret_type param_type
	Callback<int, int> fail = make_callback<int, int>(o,&omg::failure);
	if(fail(1) == 0)
	{
		std::cout << "Callback failed!" << std::endl;
	}

Update: Added delegates. Basic syntax looks like:

Code: Select all

	std::cout << "Delegates test: " << std::endl;
	Delegate<void,int> delegate;
	delegate += make_callback<void, int>(o,&omg::lol);
	delegate += make_callback<void, int>(&lol);

	delegate(123);

Code: Select all

Created 10 million callback using Boost::Function<>: Took 17seconds!
Created 10 million callback using my Callback<>: Took 10seconds!
Created 10 million callback using Fast Callback<>: Took 2seconds!
Update: Added FastCallback and FastDelegate. It's a lot faster, but syntax looks a little worse, and it's very type restrictive.
credit goes to: http://www.codeproject.com/Articles/110 ... -Delegates , as that is what I based it upon.
Basic syntax looks like:

Code: Select all

	FastCallback<void,int> _boost = make_callback<void,int,compute>();
	_boost(1);

	FastDelegate<void,int> delegate;
	delegate += make_callback<void, int,omg, &omg::lol>(&o);
	delegate += make_callback<void, int,lol>();
	delegate -= make_callback<void, int,omg, &omg::lol>(&o);
	delegate(123);

Update: Added Timestamp(helper class,converts time to a string format), Timer, clamp, safe_enum, convenient randomization functions, EventDispacher. :)
I also added a much better example showing off possible uses of most of the features in the library. It's in CallbackExample.h/cpp.

*disclaimer*
Might contain issues, it was definitely not thoroughly tested!