-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.h
75 lines (68 loc) · 2.11 KB
/
observer.h
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
/**
* $DateTime: 2015/08/23 13:00:12 $
* $Id: //depot/sircond/observer.h#1 $
* $Change: 2154 $
*
* Copyright (C) 2015 Swarga Research (http://www.swarga-research.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _OBSERVER_H_
#define _OBSERVER_H_
//!
//! \file OBSERVER.H
//!
//! \brief A minimalist implementation of the Observer design pattern.
//!
//! The Observer design pattern is a publisher/subscriber model wherein
//! "observers" register for event notifications from a "subject"
//!
#include <vector>
#include <algorithm>
//!
//! \brief The Observer class
//!
template <typename T>
class IObserver
{
public:
virtual ~IObserver () { }
//! \brief Called by the Subject when an event occurs
virtual void Update (T& t) = 0;
};
//!
//! \brief The Subject class
//!
//! The Subject is the source of the event notifications being
//! monitored by the Observer. When an event occurs, the Update
//! method of each Observer is invoked in turn.
//!
template <typename T>
class Subject
{
public:
//! \brief Add an Observer to the notification list
void Attach (IObserver<T>* o) { m_list.push_back(o); }
//! \brief Remove an Observer from the notification list
void Detach (IObserver<T>* o) { m_list.remove(o); }
//! \brief Send an update to all attached Observers
void Notify(T& t)
{
std::for_each(m_list.begin(), m_list.end(), [&t](IObserver<T>* o) { o->Update(t); });
}
private:
std::vector<IObserver<T>*> m_list; //!< The list of registered Observers
};
#endif