TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkoptional.h
Go to the documentation of this file.
1 #ifndef TTKOPTIONAL_H
2 #define TTKOPTIONAL_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 "ttksmartptr.h"
23 
27 template <typename T>
29 {
31  : m_value(nullptr)
32  {
33 
34  }
35 
36  template <typename... Args>
37  static TTKOptional<T> make_optional(Args &&...args)
38  {
39  TTKOptional<T> res;
40  res.emplace(std::forward<Args>(args)...);
41  return res;
42  }
43 
44  TTKOptional(const TTKOptional<T> &other, typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0)
45  : m_value()
46  {
47  if(other.m_value)
48  {
49  m_value = std::make_unique<T>(*other.m_value);
50  }
51  }
52 
53  TTKOptional(const T &other)
54  : m_value(std::make_unique<T>(other))
55  {
56 
57  }
58 
60  {
61  m_value = std::move(other.m_value);
62  }
63 
65  {
66  m_value = std::move(other.m_value);
67  return *this;
68  }
69 
70  void operator=(const TTKOptional<T> &other)
71  {
72  if(other.m_value)
73  {
74  m_value = std::make_unique<T>(*other.m_value);
75  }
76  else
77  {
78  m_value.reset();
79  }
80  }
81 
82  bool operator<(const TTKOptional<T> &other) const
83  {
84  return m_value && other.m_value ? (*m_value < *other.m_value) : (!m_value && other.m_value);
85  }
86 
87  bool operator<=(const TTKOptional<T> &other) const
88  {
89  return m_value < other.m_value || m_value == other.m_value;
90  }
91 
92  bool operator>(const TTKOptional<T> &other) const
93  {
94  return m_value && other.m_value ? (*m_value > *other.m_value) : (m_value && !other.m_value);
95  }
96 
97  bool operator>=(const TTKOptional<T> &other) const
98  {
99  return m_value > other.m_value || m_value == other.m_value;
100  }
101 
102  template <typename... Args>
103  void emplace(Args &&...args)
104  {
105  m_value = std::make_unique<T>(std::forward<Args>(args)...);
106  }
107 
108  void reset() { m_value.reset(); }
109  bool has_value() const { return m_value.operator bool(); }
110  const T& value() const { return *m_value; }
111 
112  operator bool() const { return m_value.operator bool(); }
113  T* operator->() { return &*m_value; }
114  const T* operator->() const { return &*m_value; }
115 
116  const T& operator*() const
117  {
118  return *m_value;
119  }
120 
122  {
123  return *m_value;
124  }
125 
126  bool operator==(const TTKOptional &other) const
127  {
128  if(!m_value && !other)
129  {
130  return true;
131  }
132 
133  return m_value && other ? (*m_value == *other) : false;
134  }
135 
136  bool operator!=(const TTKOptional &other) const { return !operator==(other); }
137 
138 private:
139  std::unique_ptr<T> m_value;
140 };
141 
142 
143 template <typename T>
144 inline bool operator==(const TTKOptional<T> &v, const T &other)
145 {
146  return v.has_value() ? (*v == other) : false;
147 }
148 
149 template <typename T>
150 inline bool operator==(const T &other, const TTKOptional<T> &v)
151 {
152  return v.has_value() ? (*v == other) : false;
153 }
154 
155 template <typename T>
156 inline bool operator!=(const TTKOptional<T> &v, const T &other)
157 {
158  return !operator==(v, other);
159 }
160 
161 template <typename T>
162 inline bool operator!=(const T &other, const TTKOptional<T> &v)
163 {
164  return !operator==(other, v);
165 }
166 
167 
168 // compatiblity for std optional
169 namespace std
170 {
171 #if !TTK_HAS_CXX17
172 template <typename T>
174 #endif
175 }
176 
177 #endif // TTKOPTIONAL_H
void emplace(Args &&...args)
Definition: ttkoptional.h:103
#define T(v)
Definition: http_parser.c:237
TTKOptional(const TTKOptional< T > &other, typename std::enable_if< std::is_copy_constructible< T >::value, int >::type=0)
Definition: ttkoptional.h:44
void reset()
Definition: ttkoptional.h:108
const T & value() const
Definition: ttkoptional.h:110
T * operator->()
Definition: ttkoptional.h:113
void operator=(const TTKOptional< T > &other)
Definition: ttkoptional.h:70
TTKOptional & operator=(TTKOptional &&other)
Definition: ttkoptional.h:64
const T * operator->() const
Definition: ttkoptional.h:114
bool operator==(const TTKOptional< T > &v, const T &other)
Definition: ttkoptional.h:144
Definition: ttkcompat.h:39
bool operator!=(const TTKOptional &other) const
Definition: ttkoptional.h:136
_MakeUnique< _Tp >::__single_object make_unique(_Args &&...__args)
std::make_unique for single objects
Definition: ttksmartptr.h:49
TTKOptional(const T &other)
Definition: ttkoptional.h:53
const T & operator*() const
Definition: ttkoptional.h:116
TTKOptional(TTKOptional &&other)
Definition: ttkoptional.h:59
bool operator!=(const TTKOptional< T > &v, const T &other)
Definition: ttkoptional.h:156
bool operator>(const TTKOptional< T > &other) const
Definition: ttkoptional.h:92
bool has_value() const
Definition: ttkoptional.h:109
static TTKOptional< T > make_optional(Args &&...args)
Definition: ttkoptional.h:37
std::unique_ptr< T > m_value
Definition: ttkoptional.h:139
bool operator>=(const TTKOptional< T > &other) const
Definition: ttkoptional.h:97
The class of the optional module.
Definition: ttkoptional.h:28
bool operator==(const TTKOptional &other) const
Definition: ttkoptional.h:126
T & operator*()
Definition: ttkoptional.h:121