TTKMusicPlayer  4.2.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 - 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 <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 &&value) noexcept
43  : m_ptr(new _Derived<typename std::decay<T>::type>(std::forward<T>(value))),
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 bad_any_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  class bad_any_cast : public std::bad_cast
102  {
103  public:
104  virtual const char *what() const noexcept override final
105  {
106  return "bad any cast";
107  }
108  };
109 
110 private:
111  struct _Base;
112  using _BasePtr = std::unique_ptr<_Base>;
113 
114  struct _Base
115  {
116  virtual ~_Base() = default;
117 
118  virtual _BasePtr clone() const noexcept = 0;
119  };
120 
121  template <typename T>
122  struct _Derived : public _Base
123  {
124  template <typename U>
125  _Derived(U &&value) noexcept
126  : m_value(std::forward<U>(value))
127  {
128 
129  }
130 
131  virtual _BasePtr clone() const noexcept override final
132  {
133  return _BasePtr(new _Derived<T>(m_value));
134  }
135 
137  };
138 
142  _BasePtr clone() const noexcept
143  {
144  return m_ptr ? m_ptr->clone() : nullptr;
145  }
146 
148  std::type_index m_type;
149 
150 };
151 
152 
153 #ifdef TTK_CAST
154 # define TTKAnyCast(x, y) (TTK::any_cast<x>(y))
155 #else
156 # define TTKAnyCast(x, y) ((x)(y))
157 #endif
158 
159 
163 namespace TTK
164 {
168  template <typename T, typename... Args>
169  inline TTKAny make_any(Args &&...args)
170  {
171  return TTKAny(T(std::forward<Args>(args)...));
172  }
173 
174  template <typename T>
175  using remove_cvr = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
176 
180  template <typename T, typename _TYPE = remove_cvr<T>>
181  inline _TYPE any_cast(const TTKAny &other)
182  {
183  return other.isSame<_TYPE>() ? other.cast<_TYPE>() : _TYPE();
184  }
188  template <typename T, typename _TYPE = remove_cvr<T>>
189  inline _TYPE any_cast(TTKAny &other)
190  {
191  return other.isSame<_TYPE>() ? other.cast<_TYPE>() : _TYPE();
192  }
196  template <typename T, typename _TYPE = remove_cvr<T>>
197  inline _TYPE any_cast(TTKAny &&other)
198  {
199  return other.isSame<_TYPE>() ? other.cast<_TYPE>() : _TYPE();
200  }
204  template <typename T, typename _TYPE = remove_cvr<T>>
205  inline _TYPE any_cast(const TTKAny *const other) noexcept
206  {
207  return other->isSame<_TYPE>() ? other->cast<_TYPE>() : _TYPE();
208  }
212  template <typename T, typename _TYPE = remove_cvr<T>>
213  inline _TYPE any_cast(TTKAny *const other) noexcept
214  {
215  return other->isSame<_TYPE>() ? other->cast<_TYPE>() : _TYPE();
216  }
217 }
218 
219 
220 // compatiblity for std any
221 namespace std
222 {
223 // Non-member functions [any.nonmembers]
224 inline void swap(TTKAny &left, TTKAny &right) noexcept
225 {
226  left.swap(right);
227 }
228 
229 #if !TTK_HAS_CXX17
230 using any = TTKAny;
231 using namespace TTK;
232 #endif
233 }
234 
235 #endif // TTKANY_H
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:231
#define TTK_MODULE_EXPORT
#define TTKConstCast(x, y)
Definition: ttkglobal.h:213
#define T(v)
Definition: http_parser.c:237
T & cast()
Definition: ttkany.h:67
std::unique_ptr< _Base > _BasePtr
Definition: ttkany.h:112
const T & cast() const
Definition: ttkany.h:76
_Derived(U &&value) noexcept
Definition: ttkany.h:125
void swap(TTKAny &other) noexcept
Definition: ttkany.h:95
_BasePtr clone() const noexcept
Definition: ttkany.h:142
bool isSame() const noexcept
Definition: ttkany.h:58
Definition: ttkcompat.h:39
void swap(TTKAny &left, TTKAny &right) noexcept
Definition: ttkany.h:224
TTKAny(T &&value) noexcept
Definition: ttkany.h:42
typename std::remove_cv< typename std::remove_reference< T >::type >::type remove_cvr
Definition: ttkany.h:175
_Tp exchange(_Tp &__obj, _Up &&__new_val) noexcept
Assign __new_val to __obj and return its previous value.
Definition: ttkcompat.h:54
The namespace of the application object.
Definition: ttkcompat.h:24
_BasePtr m_ptr
Definition: ttkany.h:147
TTKAny make_any(Args &&...args)
Definition: ttkany.h:169
_TYPE any_cast(const TTKAny &other)
Definition: ttkany.h:181
The class of the ttk any module.
Definition: ttkany.h:30
virtual _BasePtr clone() const noexceptoverridefinal
Definition: ttkany.h:131
std::type_index m_type
Definition: ttkany.h:148
#define const
Definition: zconf.h:233
virtual const char * what() const noexceptoverridefinal
Definition: ttkany.h:104
#define TTKDynamicCast(x, y)
Definition: ttkglobal.h:219