Skip to main content
gamedev.net
🔒 Locked

Templates and Function (Pointers)

Started by Axiverse 3 replies 600+ views
Original Post
Axiverse
Axiverse
Hi! I'm trying to make an event class, but i'm having a little trouble... say you have a templated class where type should be a function pointer

template < typename type >
struct Event
{
...
...
void Call ();
...
}
and in the call function, i want it to the user to be able to put in a number of parameters equal to that in the function pointer type, also having the same types. Can you do this?
Tac-Tics
Tac-Tics
typedef the function pointer first, then make template around the typedef. Template syntax is bad enough -- don't mess with unsanitary function pointer syntax at the same time ^^;
Drew_Benton
Drew_Benton
Quote:
Original post by Axiverse
Hi! I'm trying to make an event class, but i'm having a little trouble...

say you have a templated class where type should be a function pointer

template < typename type >struct Event{......void Call ();...}


and in the call function, i want it to the user to be able to put in a number of parameters equal to that in the function pointer type, also having the same types. Can you do this?


This can be done - but only in Visual Studio 7 or Dev-CPP, not in VS6. I used the same concept for plugins. Here is what I did:
[source lang = "cpp"]// This is a function pointer to the function we are importingtypedef void (*FUNC1)(LPTSTR);Plugin.UseFunction< FUNC1 >("PrintStr")( "This is a test!" );// where UseFunction was defined as this:// Use a function from a .dll	template <typename _type>	// The template lets us specify the return type	_type UseFunction( const char* function )	{		// We can't use it - generates a General Protection Fault if we try to use it		if( NULL == hinstLib )			return 0;				// If the handle is valid, try to return the function		return (_type)GetProcAddress( hinstLib, function );	}


Here are my notes from the class:
[source lang = "cpp"]/***************************************************************************************************************************** The general pattern uses this model:																						** typedef [return type] (* [identifier] )( [Parameter1, Parameter2, ... , ParameterN] );									** 																															** If we wanted to use a function that returned an int and had two int parameters:											** typedef int (*FUNC2 )( int, int );																						**																															* * If we wanted to use a function that returned an char* had had one float and two long parameters:							** typedef char* (*FUNC3 )( float, long, long);																				**																															** The same applies for User Data Types, just be sure they are defined before you make the function pointer typedef!			*****************************************************************************************************************************/


Or what I also did was this:
// Return the memory for an abstract class	template <typename _T>	_T* GetClass( const char* exportfunction )	{		// So we can specify what type of memory we want it to return		typedef void (*temp)( _T** );		// We can't use it - generates a General Protection Fault if we try to use it		if( NULL == hinstLib )			return 0;														// Temporary storage		_T* memory;		// Obtain the memory for a new class		UseFunction< temp >( exportfunction ) ( &memory );		// Return the new memory		return memory;	}


I created the typedef inside the function based on what type of memory I was retrieving. If none of this makes sense - here is a link I used to make all this nonsense [smile]. Best of luck!

- Drew
Zipster
Zipster
[razz]

I wouldn't recommend designing your event system like that. Instead of trying to find a way of having your dispatch function take a variable number of parameters, instead have it take a parameter base class, and each event can downcast that to it's specific parameter class. Or even have it take a void* and cast that if you need to. Otherwise you're in for a world of headache.
class BaseParam{ /* common parameter stuff */};class CustomParam : public BaseParam{public:   int myInt;   float myFloat;};class Event{  /* ... */  virtual void Dispatch(BaseParam*);};class MyEvent : public Event{  /* ... */  void Dispatch(BaseParam* params)  {     CustomParam* myParams = dynamic_cast<CustomParam*>(params);     /* do stuff with parameters */  }};
Shannon Barber
Shannon Barber
Signal & Slots

You can make one your easily with boost::function and boost::bind.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.