1 /*
2  *  Copyright (C) 2005-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 <assert.h>
12 #include <atomic>
13 
14 template<typename T> struct IDVDResourceCounted
15 {
IDVDResourceCountedIDVDResourceCounted16   IDVDResourceCounted() : m_refs(1) {}
17   virtual ~IDVDResourceCounted() = default;
18 
19   IDVDResourceCounted(const IDVDResourceCounted &) = delete;
20   IDVDResourceCounted &operator=(const IDVDResourceCounted &) = delete;
21 
AcquireIDVDResourceCounted22   virtual T*  Acquire()
23   {
24     ++m_refs;
25     return static_cast<T*>(this);
26   }
27 
ReleaseIDVDResourceCounted28   virtual long Release()
29   {
30     long count = --m_refs;
31     assert(count >= 0);
32     if (count == 0)
33       delete static_cast<T*>(this);
34     return count;
35   }
36   std::atomic<long> m_refs;
37 };
38