Merge pull request #26220 from 78andyp/blurayfixes
[xbmc.git] / xbmc / utils / EventStreamDetail.h
blob31cd0b50cccbaa10541f51d5b44747440a5dcb81
1 /*
2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "threads/CriticalSection.h"
13 #include <mutex>
15 namespace detail
18 template<typename Event>
19 class ISubscription
21 public:
22 virtual void HandleEvent(const Event& event) = 0;
23 virtual void Cancel() = 0;
24 virtual bool IsOwnedBy(void* obj) = 0;
25 virtual ~ISubscription() = default;
28 template<typename Event, typename Owner>
29 class CSubscription : public ISubscription<Event>
31 public:
32 typedef void (Owner::*Fn)(const Event&);
33 CSubscription(Owner* owner, Fn fn);
34 void HandleEvent(const Event& event) override;
35 void Cancel() override;
36 bool IsOwnedBy(void *obj) override;
38 private:
39 Owner* m_owner;
40 Fn m_eventHandler;
41 CCriticalSection m_criticalSection;
44 template<typename Event, typename Owner>
45 CSubscription<Event, Owner>::CSubscription(Owner* owner, Fn fn)
46 : m_owner(owner), m_eventHandler(fn)
49 template<typename Event, typename Owner>
50 bool CSubscription<Event, Owner>::IsOwnedBy(void* obj)
52 std::unique_lock<CCriticalSection> lock(m_criticalSection);
53 return obj != nullptr && obj == m_owner;
56 template<typename Event, typename Owner>
57 void CSubscription<Event, Owner>::Cancel()
59 std::unique_lock<CCriticalSection> lock(m_criticalSection);
60 m_owner = nullptr;
63 template<typename Event, typename Owner>
64 void CSubscription<Event, Owner>::HandleEvent(const Event& event)
66 std::unique_lock<CCriticalSection> lock(m_criticalSection);
67 if (m_owner)
68 (m_owner->*m_eventHandler)(event);