danicoin/src/Common/ObserverManager.h

132 lines
3.2 KiB
C
Raw Normal View History

// Copyright (c) 2011-2015 The Cryptonote developers
2014-06-25 17:21:42 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
2014-07-18 10:49:01 +00:00
#include <algorithm>
2014-06-25 17:21:42 +00:00
#include <mutex>
#include <vector>
2015-07-30 15:22:07 +00:00
namespace Tools {
2014-06-25 17:21:42 +00:00
template<typename T>
class ObserverManager {
public:
bool add(T* observer) {
std::unique_lock<std::mutex> lock(m_observersMutex);
auto it = std::find(m_observers.begin(), m_observers.end(), observer);
if (m_observers.end() == it) {
m_observers.push_back(observer);
return true;
} else {
return false;
}
}
bool remove(T* observer) {
std::unique_lock<std::mutex> lock(m_observersMutex);
2015-07-30 15:22:07 +00:00
2014-06-25 17:21:42 +00:00
auto it = std::find(m_observers.begin(), m_observers.end(), observer);
if (m_observers.end() == it) {
return false;
} else {
m_observers.erase(it);
return true;
}
}
void clear() {
std::unique_lock<std::mutex> lock(m_observersMutex);
m_observers.clear();
}
#if defined(_MSC_VER)
template<typename F>
void notify(F notification) {
std::vector<T*> observersCopy;
{
std::unique_lock<std::mutex> lock(m_observersMutex);
observersCopy = m_observers;
}
for (T* observer : observersCopy) {
(observer->*notification)();
}
}
template<typename F, typename Arg0>
void notify(F notification, const Arg0& arg0) {
std::vector<T*> observersCopy;
{
std::unique_lock<std::mutex> lock(m_observersMutex);
observersCopy = m_observers;
}
for (T* observer : observersCopy) {
(observer->*notification)(arg0);
}
}
template<typename F, typename Arg0, typename Arg1>
void notify(F notification, const Arg0& arg0, const Arg1& arg1) {
std::vector<T*> observersCopy;
{
std::unique_lock<std::mutex> lock(m_observersMutex);
observersCopy = m_observers;
}
for (T* observer : observersCopy) {
(observer->*notification)(arg0, arg1);
}
}
template<typename F, typename Arg0, typename Arg1, typename Arg2>
void notify(F notification, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2) {
std::vector<T*> observersCopy;
{
std::unique_lock<std::mutex> lock(m_observersMutex);
observersCopy = m_observers;
}
for (T* observer : observersCopy) {
(observer->*notification)(arg0, arg1, arg2);
}
}
template<typename F, typename Arg0, typename Arg1, typename Arg2, typename Arg3>
void notify(F notification, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) {
std::vector<T*> observersCopy;
{
std::unique_lock<std::mutex> lock(m_observersMutex);
observersCopy = m_observers;
}
for (T* observer : observersCopy) {
(observer->*notification)(arg0, arg1, arg2, arg3);
}
}
2014-06-25 17:21:42 +00:00
#else
template<typename F, typename... Args>
void notify(F notification, Args... args) {
std::vector<T*> observersCopy;
{
std::unique_lock<std::mutex> lock(m_observersMutex);
observersCopy = m_observers;
}
for (T* observer : observersCopy) {
(observer->*notification)(args...);
}
}
#endif
private:
std::vector<T*> m_observers;
std::mutex m_observersMutex;
};
}