Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
qx-setonce.h
1#ifndef QX_SETONCE_H
2#define QX_SETONCE_H
3
4// Standard Library Includes
5#include <concepts>
6
7// Extra-component Includes
9
10namespace Qx
11{
12
13/* I'd prefer doing this in a way where we don't need to repeat the common implementation
14 * for each class, but using a base can screw up doxygen, and having optional data members
15 * is fuggy as of C++20, so for now we just duplicate as needed.
16 */
17
18template<typename T, class C = void>
19class SetOnce;
20
23template<typename T>
24 requires std::assignable_from<T&, T>
25class SetOnce<T, void>
26{
27//-Instance Members----------------------------------------------------------------------------------------------------
28private:
29 bool mSet;
30 T mDefaultValue;
31 T mValue;
32
33//-Constructor-------------------------------------------------------------------------------------------------------
34public:
35 SetOnce(T initial = T()) :
36 mSet(false),
37 mDefaultValue(initial),
38 mValue(initial)
39 {}
40
41//-Instance Functions--------------------------------------------------------------------------------------------------
42public:
43 bool isSet() const { return mSet; }
44 const T& value() const { return mValue; }
45
46 void reset()
47 {
48 mSet = false;
49 mValue = mDefaultValue;
50 }
51
52 SetOnce<T, void>& operator=(const T& value)
53 {
54 if(!mSet)
55 {
56 mValue = value;
57 mSet = true;
58 }
59
60 return *this;
61 }
62
63//-Operators--------------------------------------------------------------------------------------------------
64 const T& operator*() const { return mValue; }
65 const T* operator->() const { return &mValue; }
66 explicit operator bool() const { return mSet; }
67};
70template<typename T, class C>
71 requires std::assignable_from<T&, T> && comparator<C, T>
72class SetOnce<T, C>
73{
74//-Instance Members----------------------------------------------------------------------------------------------------
75private:
76 C mComparator;
77 bool mSet;
78 T mDefaultValue;
79 T mValue;
80
81//-Constructor-------------------------------------------------------------------------------------------------------
82public:
83 SetOnce(T initial, C&& comp = C()) :
84 mComparator(comp),
85 mSet(false),
86 mDefaultValue(initial),
87 mValue(initial)
88 {}
89
90//-Instance Functions--------------------------------------------------------------------------------------------------
91public:
92 bool isSet() const { return mSet; }
93 const T& value() const { return mValue; }
94
95 void reset()
96 {
97 mSet = false;
98 mValue = mDefaultValue;
99 }
100
101 SetOnce<T, C>& operator=(const T& value)
102 {
103 if(!mSet && !mComparator(mDefaultValue, value))
104 {
105 mValue = value;
106 mSet = true;
107 }
108
109 return *this;
110 }
111
112//-Operators--------------------------------------------------------------------------------------------------
113 const T& operator*() const { return mValue; }
114 const T* operator->() const { return &mValue; }
115 explicit operator bool() const { return mSet; }
116};
117
118
119
120}
121
122#endif // QX_SETONCE_H
SetOnce< T, C > & operator=(const T &value)
Definition qx-setonce.h:101
bool isSet() const
Definition qx-setonce.h:92
const T & value() const
Definition qx-setonce.h:93
SetOnce(T initial, C &&comp=C())
Definition qx-setonce.h:83
const T & operator*() const
Definition qx-setonce.h:113
const T * operator->() const
Definition qx-setonce.h:114
void reset()
Definition qx-setonce.h:95
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-abstracterror.cpp:13
QTextStream & reset(QTextStream &stream)
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...