TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkany.h
Go to the documentation of this file.
1 #ifndef TTKANY_H
2 #define TTKANY_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2024 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 <typeindex>
24 #include "ttkcompat.h"
25 #include "ttkmoduleexport.h"
26 
31 {
32 public:
36  TTKAny() noexcept;
37  TTKAny(const TTKAny &other) noexcept;
38  TTKAny(TTKAny &&other) noexcept;
39 
40  template <typename T,
41  typename = typename std::enable_if<!std::is_same<typename std::decay<T>::type, TTKAny>::value, T>::type>
42  TTKAny(T &&t) noexcept
43  : m_ptr(new _Derived<typename std::decay<T>::type>(std::forward<T>(t))),
44  m_type(typeid(typename std::decay<T>::type))
45  {
46 
47  }
48 
52  bool isNull() const noexcept;
53 
57  template <typename T>
58  bool isSame() const noexcept
59  {
60  return m_type == std::type_index(typeid(T));
61  }
62 
66  template <typename T>
67  T& cast()
68  {
69  return TTKConstCast(T&, TTKStaticCast(const TTKAny*, this)->cast<T>());
70  }
71 
75  template <typename T>
76  const T& cast() const
77  {
78  if(!isSame<T>())
79  {
80  throw std::bad_cast();
81  }
82 
83  auto ptr = TTKDynamicCast(_Derived<T>*, m_ptr.get());
84  return ptr->m_value;
85  }
86 
90  TTKAny& operator=(const TTKAny &other) noexcept;
91 
95  void swap(TTKAny &other) noexcept
96  {
97  other = std::exchange(*this, std::move(other));
98  }
99 
100 private:
101  struct _Base;
102  using _BasePtr = std::unique_ptr<_Base>;
103 
104  struct _Base
105  {
106  virtual ~_Base() noexcept = default;
107 
108  virtual _BasePtr clone() const noexcept = 0;
109  };
110 
111  template <typename T>
112  struct _Derived : public _Base
113  {
114  template <typename U>
115  _Derived(U &&value) noexcept
116  : m_value(std::forward<U>(value))
117  {
118  }
119 
120  virtual _BasePtr clone() const noexcept override final
121  {
122  return _BasePtr(new _Derived<T>(m_value));
123  }
124 
126  };
127 
131  _BasePtr clone() const noexcept
132  {
133  return m_ptr ? m_ptr->clone() : nullptr;
134  }
135 
137  std::type_index m_type;
138 
139 };
140 
141 
142 #ifdef TTK_CAST
143 # define TTKAnyCast(x, y) (TTK::any_cast<x>(y))
144 #else
145 # define TTKAnyCast(x, y) ((x)(y))
146 #endif
147 
148 
152 namespace TTK
153 {
157  template <typename T, typename... Args>
158  inline TTKAny make_any(Args &&...args)
159  {
160  return TTKAny(T(std::forward<Args>(args)...));
161  }
162 
163  template <typename T>
164  using remove_cvr = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
165 
169  template <typename T, typename _TYPE = remove_cvr<T>>
170  inline _TYPE any_cast(const TTKAny &other)
171  {
172  return other.isSame<_TYPE>() ? other.cast<_TYPE>() : _TYPE();
173  }
177  template <typename T, typename _TYPE = remove_cvr<T>>
178  inline _TYPE any_cast(TTKAny &other)
179  {
180  return other.isSame<_TYPE>() ? other.cast<_TYPE>() : _TYPE();
181  }
185  template <typename T, typename _TYPE = remove_cvr<T>>
186  inline _TYPE any_cast(TTKAny &&other)
187  {
188  return other.isSame<_TYPE>() ? other.cast<_TYPE>() : _TYPE();
189  }
193  template <typename T, typename _TYPE = remove_cvr<T>>
194  inline _TYPE any_cast(const TTKAny *const other) noexcept
195  {
196  return other->isSame<_TYPE>() ? other->cast<_TYPE>() : _TYPE();
197  }
201  template <typename T, typename _TYPE = remove_cvr<T>>
202  inline _TYPE any_cast(TTKAny *const other) noexcept
203  {
204  return other->isSame<_TYPE>() ? other->cast<_TYPE>() : _TYPE();
205  }
206 }
207 
208 
209 namespace std
210 {
211 // Non-member functions [any.nonmembers]
212 inline void swap(TTKAny &left, TTKAny &right) noexcept
213 {
214  left.swap(right);
215 }
216 
217 #if !TTK_HAS_CXX17
218 // compatiblity for std any
219 using any = TTKAny;
220 using namespace::TTK;
221 #endif
222 }
223 
224 #endif // TTKANY_H
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:159
#define TTK_MODULE_EXPORT
#define TTKConstCast(x, y)
Definition: ttkglobal.h:141
#define T(v)
Definition: http_parser.c:237
T & cast()
Definition: ttkany.h:67
std::unique_ptr< _Base > _BasePtr
Definition: ttkany.h:102
const T & cast() const
Definition: ttkany.h:76
_Derived(U &&value) noexcept
Definition: ttkany.h:115
void swap(TTKAny &other) noexcept
Definition: ttkany.h:95
_BasePtr clone() const noexcept
Definition: ttkany.h:131
bool isSame() const noexcept
Definition: ttkany.h:58
Definition: ttkcompat.h:39
void swap(TTKAny &left, TTKAny &right) noexcept
Definition: ttkany.h:212
typename std::remove_cv< typename std::remove_reference< T >::type >::type remove_cvr
Definition: ttkany.h:164
_Tp exchange(_Tp &__obj, _Up &&__new_val) noexcept
Assign __new_val to __obj and return its previous value.
Definition: ttkcompat.h:44
The namespace of the process utils.
Definition: ttkcompat.h:24
_BasePtr m_ptr
Definition: ttkany.h:136
TTKAny(T &&t) noexcept
Definition: ttkany.h:42
TTKAny make_any(Args &&...args)
Definition: ttkany.h:158
_TYPE any_cast(const TTKAny &other)
Definition: ttkany.h:170
The class of the ttk any module.
Definition: ttkany.h:30
virtual _BasePtr clone() const noexceptoverridefinal
Definition: ttkany.h:120
std::type_index m_type
Definition: ttkany.h:137
#define const
Definition: zconf.h:233
#define TTKDynamicCast(x, y)
Definition: ttkglobal.h:147