Moving delegate to poly.

This commit is contained in:
Ben Vanik
2014-12-20 08:49:11 -08:00
parent a0eebf8898
commit c1df273600
6 changed files with 31 additions and 61 deletions

50
src/poly/delegate.h Normal file
View File

@@ -0,0 +1,50 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_DELEGATE_H_
#define POLY_DELEGATE_H_
#include <functional>
#include <mutex>
#include <vector>
namespace poly {
// TODO(benvanik): go lockfree, and don't hold the lock while emitting.
template <typename... Args>
class Delegate {
public:
typedef std::function<void(Args&...)> Listener;
void AddListener(Listener const& listener) {
std::lock_guard<std::mutex> guard(lock_);
listeners_.push_back(listener);
}
void RemoveAllListeners() {
std::lock_guard<std::mutex> guard(lock_);
listeners_.clear();
}
void operator()(Args&... args) {
std::lock_guard<std::mutex> guard(lock_);
for (auto& listener : listeners_) {
listener(args...);
}
}
private:
std::mutex lock_;
std::vector<Listener> listeners_;
};
} // namespace poly
#endif // POLY_DELEGATE_H_

View File

@@ -5,6 +5,7 @@
'atomic.h',
'byte_order.h',
'debugging.h',
'delegate.h',
'config.h',
'cxx_compat.h',
'logging.cc',