|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "xrCommon/xr_smart_pointers.h" |
| 4 | +#include "xrCommon/xr_vector.h" |
| 5 | +#include "xrCommon/xr_array.h" |
| 6 | +#include "xrCore/Threading/Lock.hpp" |
| 7 | +#include "xrCore/Threading/ScopeLock.hpp" |
| 8 | + |
| 9 | +class CEventNotifierCallback |
| 10 | +{ |
| 11 | +public: |
| 12 | + using CID = size_t; |
| 13 | + static const CID INVALID_CID = std::numeric_limits<CID>::max(); |
| 14 | + |
| 15 | + virtual void ProcessEvent() = 0; |
| 16 | + virtual ~CEventNotifierCallback(){}; |
| 17 | +}; |
| 18 | + |
| 19 | +template <unsigned int CNT> |
| 20 | +class CEventNotifier |
| 21 | +{ |
| 22 | +private: |
| 23 | + class CCallbackStorage |
| 24 | + { |
| 25 | + class CCallbackWrapper |
| 26 | + { |
| 27 | + public: |
| 28 | + xr_unique_ptr<CEventNotifierCallback> callback; |
| 29 | + bool destroying = false; |
| 30 | + bool executing = false; |
| 31 | + |
| 32 | + CCallbackWrapper(CEventNotifierCallback* cb) : callback(cb){}; |
| 33 | + bool operator==(const CEventNotifierCallback* cb) { return cb == callback.get(); } |
| 34 | + void Reset() |
| 35 | + { |
| 36 | + callback.reset(nullptr); |
| 37 | + destroying = false; |
| 38 | + executing = false; |
| 39 | + } |
| 40 | + }; |
| 41 | + xr_vector<CCallbackWrapper> m_callbacks; |
| 42 | + Lock m_lock; |
| 43 | + |
| 44 | + public: |
| 45 | + CEventNotifierCallback::CID RegisterCallback(CEventNotifierCallback* cb) |
| 46 | + { |
| 47 | + ScopeLock lock(&m_lock); |
| 48 | + auto it = std::find(m_callbacks.begin(), m_callbacks.end(), nullptr); |
| 49 | + return (it == m_callbacks.end()) ? (m_callbacks.emplace_back(cb), m_callbacks.size() - 1) : |
| 50 | + (it->callback.reset(cb), std::distance(m_callbacks.begin(), it)); |
| 51 | + } |
| 52 | + |
| 53 | + bool UnregisterCallback(CEventNotifierCallback::CID cid) |
| 54 | + { |
| 55 | + bool result = false; |
| 56 | + ScopeLock lock(&m_lock); |
| 57 | + if (cid < m_callbacks.size() && m_callbacks[cid].callback != nullptr) |
| 58 | + { |
| 59 | + if (!m_callbacks[cid].destroying) |
| 60 | + { |
| 61 | + result = true; |
| 62 | + m_callbacks[cid].destroying = true; |
| 63 | + } |
| 64 | + |
| 65 | + if (!m_callbacks[cid].executing) |
| 66 | + { |
| 67 | + m_callbacks[cid].Reset(); |
| 68 | + } |
| 69 | + } |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + void ExecuteCallbacks() |
| 74 | + { |
| 75 | + ScopeLock lock(&m_lock); |
| 76 | + for (CEventNotifierCallback::CID i = 0; i < m_callbacks.size(); ++i) |
| 77 | + { |
| 78 | + auto& cb = m_callbacks[i]; |
| 79 | + if (cb.callback != nullptr && !cb.destroying) |
| 80 | + { |
| 81 | + cb.executing = true; |
| 82 | + cb.callback->ProcessEvent(); |
| 83 | + cb.executing = false; |
| 84 | + |
| 85 | + if (cb.destroying) |
| 86 | + { |
| 87 | + UnregisterCallback(i); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + xr_array<CCallbackStorage, CNT> m_callbacks; |
| 95 | + |
| 96 | +public: |
| 97 | + CEventNotifierCallback::CID RegisterCallback(CEventNotifierCallback* cb, unsigned int event_id) |
| 98 | + { |
| 99 | + R_ASSERT(event_id < CNT); |
| 100 | + return m_callbacks[event_id].RegisterCallback(cb); |
| 101 | + } |
| 102 | + |
| 103 | + bool UnregisterCallback(CEventNotifierCallback::CID cid, unsigned int event_id) |
| 104 | + { |
| 105 | + R_ASSERT(event_id < CNT); |
| 106 | + return m_callbacks[event_id].UnregisterCallback(cid); |
| 107 | + } |
| 108 | + |
| 109 | + void FireEvent(unsigned int event_id) |
| 110 | + { |
| 111 | + R_ASSERT(event_id < CNT); |
| 112 | + m_callbacks[event_id].ExecuteCallbacks(); |
| 113 | + } |
| 114 | +}; |
0 commit comments