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.
11 #include "threads/CriticalSection.h"
18 template<typename Event
>
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
>
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
;
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
);
63 template<typename Event
, typename Owner
>
64 void CSubscription
<Event
, Owner
>::HandleEvent(const Event
& event
)
66 std::unique_lock
<CCriticalSection
> lock(m_criticalSection
);
68 (m_owner
->*m_eventHandler
)(event
);