blob: 8b0fceea694d67cdb1b515180769ec5a4719e463 (
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
120
121
122
123
124
125
126
127
128
129
130
|
/*
* Copyright (C) 2015 by Multi-Tech Systems
*
* This file is part of libmts.
*
* libmts is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* libmts 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libmts. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*! \file MTS_Publisher.h
\brief An abstract publisher
\date 28MAR12
\author Sean Godinez
A publisher base class. Requires derived class to handle publishing.
*/
#ifndef _MTS_PUBLISHER_H_
#define _MTS_PUBLISHER_H_
#include <mts/MTS_Subscriber.h>
#include <mts/MTS_AutoPtr.h>
#include <mts/MTS_Lock.h>
#include <mts/MTS_Logger.h>
#include <mts/MTS_NonCopyable.h>
#include <set>
namespace MTS {
//! An abstract publisher
/*!
A template abstract publisher class
\sa Subscriber
*/
template<class T> class Publisher: NonCopyable {
public:
virtual ~Publisher(); //!< Destructs subscriber
void addSubscriber(Subscriber<T>& subscriber); //!< Adds a subscriber
void removeSubscriber(Subscriber<T>& subscriber); //!< Removes a subscriber
protected:
Publisher(); //!< Constructs publisher
virtual void publish(const T& object); //!< Protects publishing operation
private:
typedef std::set<Subscriber<T>*> SubscriberSet;
AutoPtr<Lock> m_apLock; //!< Guards for thread safety
SubscriberSet m_sSubscribers; //!< Set of subscribers
};
template<class T> Publisher<T>::Publisher() {
m_apLock.reset(new Lock());
}
template<class T> Publisher<T>::~Publisher() {
m_apLock.reset();
}
template<class T> void Publisher<T>::addSubscriber(
Subscriber<T>& subscriber) {
m_apLock->lock();
m_sSubscribers.insert(&subscriber);
/*
//WARNING IF ALREADY PRESENT IN SET
pair<set<T>::iterator,bool> ret;
ret = m_sSubscribers.insert(&subscriber);
if (ret.second==false) {
Logger::printWarning("subscriber already in set %s (%p)",
subscriber->getName().c_str(), subscriber);
}
*/
m_apLock->unlock();
}
template<class T> void Publisher<T>::removeSubscriber(
Subscriber<T>& subscriber) {
m_apLock->lock();
m_sSubscribers.erase(&subscriber);
/*
//WARNING IF SUBSCRIBER WAS NOT IN SET
uint32_t count = m_sSubscribers.erase(&subscriber);
if (count == 0) {
Logger::printWarning("subscriber was not in set %s (%p)",
subscriber->getName().c_str(), subscriber);
}
*/
m_apLock->unlock();
}
template<class T> void Publisher<T>::publish(const T& object) {
m_apLock->lock();
SubscriberSet set(m_sSubscribers);
for (typename SubscriberSet::iterator i = set.begin(); i != set.end(); i++) {
Subscriber<T>* subscriber = *i;
try {
subscriber->update(object);
} catch (...) {
printWarning("Publisher| exception caught while updating subscriber %s (%p)",
subscriber->getName().c_str(), subscriber);
}
}
m_apLock->unlock();
}
}
#endif
|