diff options
Diffstat (limited to 'xbmc/utils/EventStreamDetail.h')
| -rw-r--r-- | xbmc/utils/EventStreamDetail.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/xbmc/utils/EventStreamDetail.h b/xbmc/utils/EventStreamDetail.h new file mode 100644 index 0000000..c8eec67 --- /dev/null +++ b/xbmc/utils/EventStreamDetail.h | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2016-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #include "threads/CriticalSection.h" | ||
| 12 | #include "threads/SingleLock.h" | ||
| 13 | |||
| 14 | namespace detail | ||
| 15 | { | ||
| 16 | |||
| 17 | template<typename Event> | ||
| 18 | class ISubscription | ||
| 19 | { | ||
| 20 | public: | ||
| 21 | virtual void HandleEvent(const Event& event) = 0; | ||
| 22 | virtual void Cancel() = 0; | ||
| 23 | virtual bool IsOwnedBy(void* obj) = 0; | ||
| 24 | virtual ~ISubscription() = default; | ||
| 25 | }; | ||
| 26 | |||
| 27 | template<typename Event, typename Owner> | ||
| 28 | class CSubscription : public ISubscription<Event> | ||
| 29 | { | ||
| 30 | public: | ||
| 31 | typedef void (Owner::*Fn)(const Event&); | ||
| 32 | CSubscription(Owner* owner, Fn fn); | ||
| 33 | void HandleEvent(const Event& event) override; | ||
| 34 | void Cancel() override; | ||
| 35 | bool IsOwnedBy(void *obj) override; | ||
| 36 | |||
| 37 | private: | ||
| 38 | Owner* m_owner; | ||
| 39 | Fn m_eventHandler; | ||
| 40 | CCriticalSection m_criticalSection; | ||
| 41 | }; | ||
| 42 | |||
| 43 | template<typename Event, typename Owner> | ||
| 44 | CSubscription<Event, Owner>::CSubscription(Owner* owner, Fn fn) | ||
| 45 | : m_owner(owner), m_eventHandler(fn) | ||
| 46 | {} | ||
| 47 | |||
| 48 | template<typename Event, typename Owner> | ||
| 49 | bool CSubscription<Event, Owner>::IsOwnedBy(void* obj) | ||
| 50 | { | ||
| 51 | CSingleLock lock(m_criticalSection); | ||
| 52 | return obj != nullptr && obj == m_owner; | ||
| 53 | } | ||
| 54 | |||
| 55 | template<typename Event, typename Owner> | ||
| 56 | void CSubscription<Event, Owner>::Cancel() | ||
| 57 | { | ||
| 58 | CSingleLock lock(m_criticalSection); | ||
| 59 | m_owner = nullptr; | ||
| 60 | } | ||
| 61 | |||
| 62 | template<typename Event, typename Owner> | ||
| 63 | void CSubscription<Event, Owner>::HandleEvent(const Event& event) | ||
| 64 | { | ||
| 65 | CSingleLock lock(m_criticalSection); | ||
| 66 | if (m_owner) | ||
| 67 | (m_owner->*m_eventHandler)(event); | ||
| 68 | } | ||
| 69 | } | ||
