Eventhandling macro for classes source code

This snippet submitted by ToolsDevler on 2012-12-25. It has been viewed 91944 times.
Rating of 7.4 with 499 votes

 struct EventArguments {
	PObject* sender;
};

#define PEVENT(name, argument) \
private: \
	typedef void (*##name##_callback) (##argument); \
	\
	std::vector<##name##_callback> ##name##_callbacks; \
	\
	void fire_##name##(##argument arg) { \
		for(std::vector<##name##_callback>::const_iterator i = ##name##_callbacks.begin(); i != ##name##_callbacks.end(); i++) { \
			##name##_callback current = (*i);\
			current(arg); \
		}\
	}\
	public: \
	void attach_##name##_event(##name##_callback f) { \
		##name##_callbacks.push_back(f); \
	}\
	\
	void detach_##name##_event(##name##_callback f) { \
		for(std::vector<##name##_callback>::const_iterator i = ##name##_callbacks.begin(); i != ##name##_callbacks.end(); i++) { \
			if((*i) == f) { \
				##name##_callbacks.erase(i); \
				break; \
			} \
		}\
	}\
	\

 
 




More C and C++ source code snippets

Add a snippet!