Skip to main content
gamedev.net
🔒 Locked

Template help

Started by Amnesty Oct 30, 2004 at 7:28 PM 0 replies 350+ views
Original Post
Amnesty
Amnesty
Looking for some help from the compile time programming gurus here.

const int no_msg = 0;
// .. other message IDs

template 
<
	int arg1 = no_msg,	
	int arg2 = no_msg,	
	int arg3 = no_msg,	
	int arg4 = no_msg
	/// + arbitrary more ids 
>
class message_center_
{
public:
	explicit message_center_(std::stack<message>& arg) : ref(arg) 	{}
	
	void add_task (const message& arg)
	{
		ref.push(arg);
	}
private:
	std::stack<message>& ref;
};

Here i would like add_task to push the message on to the stack only if arg.id matches any of the int template parameters. I.E

message_center_<timer_msg, update_msg\> abc;
// sorry don't know how to display <,> without source tags here

abc.add_task() will push only timer_msg, update_msg and will just ignore all other messages. Just wondering if there is any possible way i can do this through template machinery instead of maintaing a runtime checks.
"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim
nmi
nmi
Use enumerators:

class message_center_{public:  enum Type { TYPE_1, TYPE_2, TYPE_3 };	explicit message_center_(std::stack<message>& arg) : ref(arg) 	{}		void add_task (Type type, const message& arg)	{		ref.push(arg);	}private:	std::stack<message>& ref;};


Maybe you can put the enumerator inside a class, then make this class a template paramter for message_center_

Topic Locked

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

Sign in to reply to this topic.