8000 GitHub - fnz/ObserverManager: Observer pattern for C++11 based on variadic templates
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fnz/ObserverManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ObserverManager

Generalized observer pattern based on C++11 variadic templates.

This implementation of the Observer pattern allows you to group events in interfaces (further called protocols) with the ability to parametrize them as you like. A single observer can be subscribed to different groups of events simultaneously. All observers are automatically unsubscribed when their destructor is called, so no memory issues will arise. Observers are passed to manager as pointers, shared pointers are supported as well.

Here's a sample use:

#include <iostream>

#include "ObserverManager.h"

class MoneyProtocol : public BaseObserverProtocol {
public:
	virtual void add(unsigned int sum) {}
	virtual void withdraw(unsigned int sum, const std::string& item) {}
};

class Worker : public MoneyProtocol {
public:
	virtual void add(const unsigned int sum) override {
		std::cout << "Look, dear, I've earned " << std::to_string(sum) << "$ today!" << std::endl;
	}

	virtual void withdraw(unsigned int sum, const std::string& item) override {
		std::cout << "Oh, I've spent " <<  std::to_string(sum) << "$ on " << item << " today..." << std::endl;
	}
};

int main() {
	Worker* jim = new Worker();
	ObserverManager::subscribe<MoneyProtocol>(jim);

	ObserverManager::notify(&MoneyProtocol::add, 500);
	ObserverManager::notify(&MoneyProtocol::withdraw, 100, "SuperBurger");

	ObserverManager::unsubscribe(jim);

	delete jim;
}

To clone and run tests:

git clone --recursive https://g
4F79
ithub.com/fnz/ObserverManager.git
cd ObserverManager
./run_tests.sh

About

Observer pattern for C++11 based on variadic templates

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0