event horizon - lua/c++ event system

Details

---
c++ callbacks have this signature, and may be global, or member functions:
	void trig( const void* arg )

	// NOTE:  inside, you then cast the "arg" to a struct containing the list of args:
	const TestStruct* args = (const TestStruct*)arg;

lua callbacks have this signature, and deals with real arguments:
	function trig( arg1, arg2, my3rdArg )


---
c++ callbacks are registered like this:
	reg( "fire", EventCallback( &trig, TestStruct::getdef() ) );

lua callbacks are registered like this:
	reg( "fire", trig )


---
we can also register [obj,member] callbacks in both c++ and lua:
	c++
		reg( "fire", EventCallback( &obj, &MyClass::trig, TestStruct::getdef() ) );
	lua
		reg( "fire", trig, obj )


---
c++ events are triggered like this:
	trigger( "fire", TestStruct( 112358, 3.14159265f, "pi is good to taste, please use enough cornstarch" ) );

lua events are triggered like this:
	trigger( "fire", 987654321, 0.112358, "cake is for feasting, 33 times this morning" )


---
finally... you're probably wondering about getdef()
getdef() is what enables the magic to happen to translate arguments from lua->c++ and c++->lua
Only basic types (int, bool, float, string) are supported as members.
	struct TestStruct
	{
		TestStruct( int b, float f, const char* o )
		{
			beans = b;
			frys = f;
			safeStrCpy( oompas, o, sizeof( oompas ) );
		}
		int beans;
		float frys;
		char oompas[20];
		
		/// describe the data packet
		static packet& getdef()
		{
			static packet o = 
				packet_( "TestStruct" )
					.add( "beans", &TestStruct::beans )
					.add( "frys", &TestStruct::frys )
					.add( "oompas", &TestStruct::oompas );
			return o;
		}
	};

Additional Credits

Thanks to Don Clugston's awesome FastDelegate code for genericizing function pointers of any kind.

Download

License

   event horizon - lua/c++ event system demonstration
   Copyright (c) 2006 kevin meinert all rights reserved

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301  USA