KCGroups

optionalgadget.h
1// SPDX-FileCopyrightText: 2020 Henri Chain <henri.chain@enioka.com>
2// SPDX-FileCopyrightText: 2020 Kevin Ottens <kevin.ottens@enioka.com>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#ifndef OPTIONALGADGET_H
7#define OPTIONALGADGET_H
8
9#ifdef STD_OPTIONAL_AVAILABLE
10#include <optional>
11#endif
12
13#ifdef __cpp_lib_optional
14namespace KCGroups
15{
16template<typename T>
17using optional = std::optional<T>;
18}
19#else
20#include "optional.h"
21#endif
22
23#define OPTIONAL_GADGET(T, name) \
24 /** \
25 @brief An optional T. \
26 The parameterless constructor creates a name without value. \n \
27 Use name(const T &val) to create a name with a value. \n \
28 Can be implicitly converted to T (undefined behavior when no value) and to bool (same has hasValue()) \
29 */ \
30 class KCGROUPS_EXPORT name : public KCGroups::optional<T> \
31 { \
32 Q_GADGET \
33 \
34 /** \
35 @brief The underlying T value. Returns a default T if hasValue() is false. \n \
36 Has a reset() method to remove the value, which can be reached in QML via x.value = undefined \
37 @accessors getValue(), setValue(), reset() \
38 */ \
39 Q_PROPERTY(T value READ getValue WRITE setValue RESET reset) \
40 \
41 /** \
42 @brief Whether there is currently a value defined \
43 @accessors hasValue() \
44 */ \
45 Q_PROPERTY(bool hasValue READ hasValue) \
46 \
47 public: \
48 /** \
49 The underlying value type. Equal to T \
50 */ \
51 using value_type = T; \
52 \
53 using KCGroups::optional<T>::optional; \
54 \
55 /** \
56 @brief get value or default \
57 @return underlying value if hasValue() is true, else default T value \
58 */ \
59 T getValue() \
60 { \
61 return *this ? **this : T(); \
62 } \
63 \
64 /** \
65 @brief set a T value \
66 @param val: the value to set \
67 */ \
68 void setValue(const T &val) \
69 { \
70 *reinterpret_cast<KCGroups::optional<T> *>(this) = val; \
71 } \
72 \
73 /** \
74 @brief whether a value is currently set \
75 @return true if a value is present, false otherwise \
76 */ \
77 bool hasValue() \
78 { \
79 return static_cast<bool>(*this); \
80 } \
81 }; \
82 \
83 inline bool operator==(const name &lhs, const name &rhs) \
84 { \
85 return static_cast<KCGroups::optional<typename name::value_type>>(lhs) == \
86 static_cast<KCGroups::optional<typename name::value_type>>(rhs); \
87 } \
88 \
89 inline bool operator!=(const name &lhs, const name &rhs) \
90 { \
91 return static_cast<KCGroups::optional<typename name::value_type>>(lhs) != \
92 static_cast<KCGroups::optional<typename name::value_type>>(rhs); \
93 } \
94 \
95 template<typename U> \
96 bool operator==(const name &lhs, const KCGroups::optional<U> &rhs) \
97 { \
98 return static_cast<KCGroups::optional<typename name::value_type>>(lhs) == rhs; \
99 } \
100 \
101 template<typename U> \
102 bool operator!=(const name &lhs, const KCGroups::optional<U> &rhs) \
103 { \
104 return static_cast<KCGroups::optional<typename name::value_type>>(lhs) != rhs; \
105 } \
106 \
107 template<typename T> \
108 bool operator==(const KCGroups::optional<T> &lhs, const name &rhs) \
109 { \
110 return lhs == static_cast<KCGroups::optional<typename name::value_type>>(rhs); \
111 } \
112 \
113 template<typename T> \
114 bool operator!=(const KCGroups::optional<T> &lhs, const name &rhs) \
115 { \
116 return lhs != static_cast<KCGroups::optional<typename name::value_type>>(rhs); \
117 } \
118 \
119 Q_DECLARE_METATYPE(name) \
120 static_assert(true, "")
121
122#endif // OPTIONALGADGET_H
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.