TTKMusicPlayer  4.1.3.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:
72 
73  std::atomic<int> m_count;
74 };
75 
76 template <typename T>
78 {
79 public:
80  TTKSharedPtr() noexcept : m_ptr(nullptr), m_ref_count(new _SharedCount) {}
81  TTKSharedPtr(T* ptr) noexcept : m_ptr(ptr), m_ref_count(new _SharedCount) {}
83  {
84  clean();
85  }
86 
87  template <typename U>
88  friend class TTKSharedPtr;
89 
90  template <typename U>
91  TTKSharedPtr(const TTKSharedPtr<U> &p, T* ptr)
92  {
93  m_ptr = ptr;
96  }
98  {
99  m_ptr = p.m_ptr;
101  m_ref_count->m_count++;
102  }
104  {
105  clean();
106  m_ptr = p.m_ptr;
108  m_ref_count->m_count++;
109  return *this;
110  }
112  {
113  m_ptr = p.m_ptr;
114  m_ref_count = p.m_ref_count;
115  p.m_ptr = nullptr;
116  p.m_ref_count = nullptr;
117  }
119  {
120  clean();
121  m_ptr = p.m_ptr;
122  m_ref_count = p.m_ref_count;
123  p.m_ptr = nullptr;
124  p.m_ref_count = nullptr;
125  return *this;
126  }
127 
128  int use_count() noexcept { return m_ref_count->m_count; }
129  T* get() const noexcept { return m_ptr; }
130  T* operator->() const noexcept { return m_ptr; }
131  T& operator*() const noexcept { return *m_ptr; }
132  operator bool() const noexcept { return m_ptr; }
133 
134 private:
135  void clean()
136  {
137  if(m_ref_count)
138  {
139  m_ref_count->m_count--;
140  if(m_ref_count->m_count == 0)
141  {
142  delete m_ptr;
143  delete m_ref_count;
144  }
145  }
146  }
147 
150 };
151 
152 namespace TTK
153 {
154 template <typename T, typename... Args>
155 inline TTKSharedPtr<T> make_shared(Args &&...args)
156 {
157  return TTKSharedPtr<T>(std::forward<Args>(args)...);
158 }
159 
160 template <typename T, typename U>
162 {
163  T* ptr = static_cast<T*>(p.get());
164  return TTKSharedPtr<T>(p, ptr);
165 }
166 
167 template <typename T, typename U>
169 {
170  T* ptr = const_cast<T*>(p.get());
171  return TTKSharedPtr<T>(p, ptr);
172 }
173 
174 template <typename T, typename U>
176 {
177  T* ptr = dynamic_cast<T*>(p.get());
178  return ptr == nullptr ? TTKSharedPtr<T>() : TTKSharedPtr<T>(p, ptr);
179 }
180 
181 template <typename T, typename U>
183 {
184  T* ptr = reinterpret_cast<T*>(p.get());
185  return TTKSharedPtr<T>(p, ptr);
186 }
187 }
188 
189 namespace std
190 {
191 // compatiblity for std shared_ptr
192 template <typename T>
194 using namespace TTK;
195 }
196 #endif
197 
198 namespace TTK
199 {
200 template <typename T>
201 inline std::shared_ptr<T> makeCopy(const T* &source)
202 {
203  return std::make_shared<T>(*source);
204 }
205 
206 template <typename T>
208 {
209  return std::make_shared<T>(*source);
210 }
211 }
212 
213 #endif // TTKSMARTPTR_H
unique_ptr< _Tp > __single_object
Definition: ttksmartptr.h:32
#define T(v)
Definition: http_parser.c:237
void clean()
Definition: ttksmartptr.h:135
std::shared_ptr< T > makeCopy(const std::shared_ptr< T > &source)
Definition: ttksmartptr.h:207
TTKSharedPtr(T *ptr) noexcept
Definition: ttksmartptr.h:81
typename remove_extent< _Tp >::type remove_extent_t
Alias template for remove_extent.
Definition: ttkcompat.h:44
T & operator*() const noexcept
Definition: ttksmartptr.h:131
int use_count() noexcept
Definition: ttksmartptr.h:128
TTKSharedPtr(TTKSharedPtr &&p)
Definition: ttksmartptr.h:111
unique_ptr< _Tp[]> __array
Definition: ttksmartptr.h:38
TTKSharedPtr< T > make_shared(Args &&...args)
Definition: ttksmartptr.h:155
Definition: ttkcompat.h:39
TTKSharedPtr(const TTKSharedPtr &p)
Definition: ttksmartptr.h:97
TTKSharedPtr & operator=(TTKSharedPtr &&p)
Definition: ttksmartptr.h:118
std::atomic< int > m_count
Definition: ttksmartptr.h:73
_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:168
TTKSharedPtr< T > reinterpret_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:182
T * operator->() const noexcept
Definition: ttksmartptr.h:130
TTKSharedPtr< T > dynamic_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:175
_SharedCount * m_ref_count
Definition: ttksmartptr.h:149
The namespace of the process utils.
Definition: ttkcompat.h:24
TTKSharedPtr(const TTKSharedPtr< U > &p, T *ptr)
Definition: ttksmartptr.h:91
TTKSharedPtr< T > static_pointer_cast(const TTKSharedPtr< U > &p) noexcept
Definition: ttksmartptr.h:161
TTKSharedPtr() noexcept
Definition: ttksmartptr.h:80
TTKSharedPtr & operator=(const TTKSharedPtr &p)
Definition: ttksmartptr.h:103
#define const
Definition: zconf.h:233