summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/EventStreamDetail.h
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
committermanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
commitbe933ef2241d79558f91796cc5b3a161f72ebf9c (patch)
treefe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/EventStreamDetail.h
parent5f8335c1e49ce108ef3481863833c98efa00411b (diff)
downloadkodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip
sync with upstream
Diffstat (limited to 'xbmc/utils/EventStreamDetail.h')
-rw-r--r--xbmc/utils/EventStreamDetail.h69
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
14namespace detail
15{
16
17template<typename Event>
18class ISubscription
19{
20public:
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
27template<typename Event, typename Owner>
28class CSubscription : public ISubscription<Event>
29{
30public:
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
37private:
38 Owner* m_owner;
39 Fn m_eventHandler;
40 CCriticalSection m_criticalSection;
41};
42
43template<typename Event, typename Owner>
44CSubscription<Event, Owner>::CSubscription(Owner* owner, Fn fn)
45 : m_owner(owner), m_eventHandler(fn)
46{}
47
48template<typename Event, typename Owner>
49bool CSubscription<Event, Owner>::IsOwnedBy(void* obj)
50{
51 CSingleLock lock(m_criticalSection);
52 return obj != nullptr && obj == m_owner;
53}
54
55template<typename Event, typename Owner>
56void CSubscription<Event, Owner>::Cancel()
57{
58 CSingleLock lock(m_criticalSection);
59 m_owner = nullptr;
60}
61
62template<typename Event, typename Owner>
63void 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}