Callback Library

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Callback Library

Post 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!
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
Post Reply