aboutsummaryrefslogtreecommitdiffstats
path: root/signal-composer-binding/observer-pattern.hpp
blob: d3e478b5153b8767c27d13db63014f987234d6f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2015, 2016 "IoT.bzh"
 * Author "Romain Forlot" <romain.forlot@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

/* This is a classic observer design pattern a bit enhanced to be
 able to pull and push info in 2 ways */

#include <list>
#include <mutex>
#include <algorithm>

template<class T>
class Observable;

template<class T>
class Observer
{
private:
	std::mutex observableListMutex_;

protected:
	virtual ~Observer()
	{
		std::lock_guard<std::mutex> lock(observableListMutex_);
		for(const auto& sig: observableList_)
		{
				sig->delObserver(this);
		}
	}

	std::list<Observable<T>*> observableList_;
	typedef typename std::list<Observable<T>*>::iterator iterator_;
	typedef typename std::list<Observable<T>*>::const_iterator const_iterator_;

public:
	virtual void update(T* observable) = 0;

	void addObservable(Observable<T>* observable)
	{
		std::lock_guard<std::mutex> lock(observableListMutex_);
		for (auto& obs : observableList_)
		{
			if (obs == observable)
				{return;}
		}

		observableList_.push_back(observable);
	}

	void delObservable(Observable<T>* observable)
	{
		std::lock_guard<std::mutex> lock(observableListMutex_);
		const_iterator_ it = std::find(observableList_.cbegin(), observableList_.cend(), observable);
		if(it != observableList_.cend())
			{observableList_.erase(it);}
	}
};

template <class T>
class Observable
{
public:
	void addObserver(Observer<T>* observer)
	{
		std::lock_guard<std::mutex> lock(observerListMutex_);
		observerList_.push_back(observer);
		observer->addObservable(this);
	}

	void delObserver(Observer<T>* observer)
	{
		std::lock_guard<std::mutex> lock(observerListMutex_);
		const_iterator_ it = find(observerList_.cbegin(), observerList_.cend(), observer);
		if(it != observerList_.cend())
			{observerList_.erase(it);}
	}

	virtual int initialRecursionCheck() = 0;
	virtual int recursionCheck(T* obs) = 0;

	virtual ~Observable()
	{
		std::lock_guard<std::mutex> lock(observerListMutex_);
		for(const auto& obs: observerList_)
		{
			obs->delObservable(this);
		}
	}

protected:
	std::list<Observer<T>*> observerList_;
	typedef typename std::list<Observer<T>*>::iterator iterator_;
	typedef typename std::list<Observer<T>*>::const_iterator const_iterator_;

	void notify()
	{
		std::lock_guard<std::mutex> lock(observerListMutex_);
		for(const auto& obs: observerList_)
		{
			obs->update(static_cast<T*>(this));
		}
	}

private:
	std::mutex observerListMutex_;
};