Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Generic Observer Pattern and Events in C++

, 5 Apr 2004
Efficient and generic way to implement Events in C++. Easy and intuitive way to connect an event to any member function of any class with no limitations.
cppevents_src.zip
cpp_event_article
// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>
#include <vector>

using namespace System;
using namespace std;

#include "CppEvent.h"

class MySubject
{
public:

	CppEvent1<bool,int> int_event;
	CppEvent2<bool,double,int> double_event;
	CppEvent3<bool,double,int,const char*> triple_event;

	void submit_int()
	{
		int_event.notify(1);
	}

	void submit_double()
	{
		double_event.notify(10.5,100);
	}

	void submit_triple()
	{
		triple_event.notify(10.5,100,"Oh ye");
	}
};

class MyListener1
{
public:

	bool update_int(int p)
	{
		Console::WriteLine(S"int update listener 1");
		return true;
	}

	bool update_double(double p,int p1)
	{
		Console::WriteLine(S"double update listener 1");
		return true;
	}

	bool update_triple(double p,int p1,const char* str)
	{
		Console::WriteLine(S"triple update listener 1");
		return true;
	}
};

class MyListener2
{
public:

	bool fun(int p)
	{
		Console::WriteLine(S"fun update listener 2");
		return true;
	}
};

// This is the entry point for this application
int _tmain(void)
{	 
	// TODO: Please replace the sample code below with your own.
    Console::WriteLine(S"Hello World");
	MyListener1* listener1 = new MyListener1;
	MyListener2* listener2 = new MyListener2;

	MySubject subject;
	
	CppEventHandler h1 = subject.int_event.attach(listener1,&MyListener1::update_int);
	CppEventHandler h2 = subject.int_event.attach(listener2,&MyListener2::fun);
	
	CppEventHandler h3 = subject.double_event.attach(listener1,&MyListener1::update_double);
    CppEventHandler h4 = subject.triple_event.attach(listener1,&MyListener1::update_triple);

	subject.submit_int();
	subject.submit_double();
	subject.submit_triple();

	// detach handlers
	subject.int_event.detach(h1);
	subject.int_event.detach(h2);
	subject.double_event.detach(h3);
	subject.triple_event.detach(h4);

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Eduard Baranovsky
Web Developer
Canada Canada
Baranovsky Eduard has been a software developer for more then 10 years. He has an experence in image processing, computer graphics and distributed systems design.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 6 Apr 2004
Article Copyright 2004 by Eduard Baranovsky
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid