TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttksmartptr.h
Go to the documentation of this file.
1 #ifndef TTKSMARTPTR_H
2 #define TTKSMARTPTR_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2025 Greedysky Studio
7 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17 
18  * You should have received a copy of the GNU Lesser General Public License along
19  * with this program; If not, see <http://www.gnu.org/licenses/>.
20  ***************************************************************************/
21 
22 #include <memory>
23 #include "ttkcompat.h"
24 
25 namespace std
26 {
27 // compatiblity for std make_unique
28 #if !TTK_HAS_CXX14
29 template <typename _Tp>
31 {
32  typedef unique_ptr<_Tp> __single_object;
33 };
34 
35 template <typename _Tp>
36 struct _MakeUnique<_Tp[]>
37 {
38  typedef unique_ptr<_Tp[]> __array;
39 };
40 
41 template <typename _Tp, size_t _Bound>
42 struct _MakeUnique<_Tp[_Bound]>
43 {
44  struct __invalid_type { };
45 };
46 
48 template <typename _Tp, typename... _Args>
49 inline typename _MakeUnique<_Tp>::__single_object make_unique(_Args &&...__args)
50 {
51  return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
52 }
53 
55 template <typename _Tp>
56 inline typename _MakeUnique<_Tp>::__array make_unique(size_t __num)
57 {
58  return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]());
59 }
60 
62 template <typename _Tp, typename... _Args>
63 inline typename _MakeUnique<_Tp>::__invalid_type make_unique(_Args &&...) = delete;
64 #endif
65 }
66 
67 #if !TTK_HAS_CXX11
69 {
70 public:
71  _SharedCount() noexcept
72  : m_count(1)
73  {
74 
75  }
76 
77  std::atomic<int> m_count;
78 };
79 
80 template <typename T>
82 {
83 public:
84  TTKSharedPtr() noexcept
85  : m_ptr(nullptr),
87  {
88 
89  }
90 
91  TTKSharedPtr(T* ptr) noexcept
92  : m_ptr(ptr),
94  {
95 
96  }
97 
98  ~TTKSharedPtr() noexcept
99  {
100  clean();
101  }
102 
103  template <typename U>
104  friend class TTKSharedPtr;
105 
106  template <typename U>
107  TTKSharedPtr(const TTKSharedPtr<U> &p, T* ptr) noexcept
108  {
109  m_ptr = ptr;
110  m_ref_count = p.m_ref_count;
111  m_ref_count->m_count++;
112  }
113 
114  TTKSharedPtr(const TTKSharedPtr &p) noexcept
115  {
116  m_ptr = p.m_ptr;
117  m_ref_count = p.m_ref_count;
118  m_ref_count->m_count++;
119  }
120 
121  TTKSharedPtr& operator=(const TTKSharedPtr &p) noexcept
122  {
123  clean();
124  m_ptr = p.m_ptr;
125  m_ref_count = p.m_ref_count;
126  m_ref_count->m_count++;
127  return *this;
128  }
129 
131  {
132  m_ptr = p.m_ptr;
133  m_ref_count = p.m_ref_count;
134  p.m_ptr = nullptr;
135  p.m_ref_count = nullptr;
136  }
137 
139  {
140  clean();
141  m_ptr = p.m_ptr;
142  m_ref_count = p.m_ref_count;
143  p.m_ptr = nullptr;
144  p.m_ref_count = nullptr;
145  return *this;
146  }
147 
148  inline int use_count() noexcept { return m_ref_count->m_count; }
149  inline T* get() const noexcept { return m_ptr; }
150  inline T* operator->() const noexcept { return m_ptr; }
151  inline T& operator*() const noexcept { return *m_ptr; }
152  inline operator bool() const noexcept { return m_ptr; }
153 
154 private:
155  void clean() noexcept
156  {
157  if(m_ref_count)
158  {
159  m_ref_count->m_count--;
160  if(m_ref_count->m_count == 0)
161  {
162  delete m_ptr;
163  delete m_ref_count;
164  }
165  }
166  }
167 
170 };
171 
172 namespace TTK
173 {
174 template <typename T, typename... Args>
175 inline TTKSharedPtr<T> make_shared(Args &&...args)
176 {
177  return TTKSharedPtr<T>(std::forward<Args>(args)...);
178 }
179 
180 template <typename T, typename U>
182 {
183  T* ptr = static_cast<T*>(p.get());
184  return TTKSharedPtr<T>(p, ptr);
185 }
186 
187 template <typename T, typename U>
189 {
190  T* ptr = const_cast<T*>(p.get());
191  return TTKSharedPtr<T>(p, ptr);
192 }
193 
194 template <typename T, typename U>
196 {
197  T* ptr = dynamic_cast<T*>(p.get());
198  return ptr == nullptr ? TTKSharedPtr<T>() : TTKSharedPtr<T>(p, ptr);
199 }
200 
201 template <typename T, typename U>
203 {
204  T* ptr = reinterpret_cast<T*>(p.get());
205  return TTKSharedPtr<T>(p, ptr);
206 }
207 }
208 
209 namespace std
210 {
211 // compatiblity for std shared_ptr
212 template <typename T>
214 using namespace TTK;
215 }
216 #endif
217 
218 namespace TTK
219 {
220 template <typename T>
221 inline std::shared_ptr<T> makeCopy(const T* &source)
222 {
223  return std::make_shared<T>(*source);
224 }
225 
226 template <typename T>
228 {
229  return std::make_shared<T>(*source);
230 }
231 }
232 
233 #endif // TTKSMARTPTR_H
unique_ptr< _Tp > __single_object
Definition: ttksmartptr.h:32
#define T(v)
Definition: http_parser.c:237
std::shared_ptr< T > makeCopy(const std::shared_ptr< T > &source)
Definition: ttksmartptr.h:227
TTKSharedPtr(const TTKSharedPtr &p) noexcept
Definition: ttksmartptr.h:114
TTKSharedPtr(T *ptr) noexcept
Definition: ttksmartptr.h:91
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
Definition: ttkcompat.h:44
T & operator*() const noexcept
Definition: ttksmartptr.h:151
int use_count() noexcept
Definition: ttksmartptr.h:148
TTKSharedPtr & operator=(const TTKSharedPtr &p) noexcept
Definition: ttksmartptr.h:121
TTKSharedPtr(TTKSharedPtr &&p) noexcept
Definition: ttksmartptr.h:130
_SharedCount() noexcept
Definition: ttksmartptr.h:71
unique_ptr< _Tp[]> __array
Definition: ttksmartptr.h:38
TTKSharedPtr< T > make_shared(Args &&...args)
Definition: ttksmartptr.h:175
TTKSharedPtr & operator=(TTKSharedPtr &&p) noexcept
Definition: ttksmartptr.h:138
Definition: ttkcompat.h:39
void clean() noexcept
Definition: ttksmartptr.h:155
std::atomic< int > m_count
Definition: ttksmartptr.h:77
_MakeUnique< _Tp >::__single_object make_unique(_Args &&...__args)
std::make_unique for single objects
Definition: ttksmartptr.h:49
TTKSharedPtr< T > const_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:188
TTKSharedPtr< T > reinterpret_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:202
T * operator->() const noexcept
Definition: ttksmartptr.h:150
TTKSharedPtr< T > dynamic_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:195
_SharedCount * m_ref_count
Definition: ttksmartptr.h:169
The namespace of the application object.
Definition: ttkcompat.h:24
~TTKSharedPtr() noexcept
Definition: ttksmartptr.h:98
TTKSharedPtr< T > static_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:181
TTKSharedPtr() noexcept
Definition: ttksmartptr.h:84
TTKSharedPtr(const TTKSharedPtr< U > &p, T *ptr) noexcept
Definition: ttksmartptr.h:107
#define const
Definition: zconf.h:233