Qx v0.5.8
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 <type_traits>
6
7// Extra-component Includes
9
10namespace Qx
11{
12
13template<typename T, class CompareEq = std::equal_to<T>>
14 requires std::is_assignable_v<T&, T> && Qx::defines_call_for_s<CompareEq, bool, T, T>
16{
17//-Instance Members----------------------------------------------------------------------------------------------------
18private:
19 CompareEq mComparator;
20 bool mSet;
21 T mDefaultValue;
22 T mValue;
23
24//-Constructor-------------------------------------------------------------------------------------------------------
25public:
26 SetOnce(T initial, const CompareEq& comp = CompareEq()) :
27 mComparator(comp),
28 mSet(false),
29 mDefaultValue(initial),
30 mValue(initial)
31 {}
32
33//-Instance Functions--------------------------------------------------------------------------------------------------
34public:
35 bool isSet() const { return mSet; }
36 const T& value() const { return mValue; }
37
38 void reset()
39 {
40 mSet = false;
41 mValue = mDefaultValue;
42 }
43
45 {
46 if(!mSet && !mComparator(mDefaultValue, value))
47 {
48 mValue = value;
49 mSet = true;
50 }
51
52 return *this;
53 }
54};
55
56}
57
58#endif // QX_SETONCE_H
The SetOnce template class acts as a container for a value that can only be set once.
Definition qx-setonce.h:16
SetOnce< T, CompareEq > & operator=(const T &value)
Definition qx-setonce.h:44
SetOnce(T initial, const CompareEq &comp=CompareEq())
Definition qx-setonce.h:26
void reset()
Definition qx-setonce.h:38
const T & value() const
Definition qx-setonce.h:36
bool isSet() const
Definition qx-setonce.h:35
Specifies that a type defines a call operator for the specified argument types (with strict return).
Definition qx-concepts.h:253
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-processwaiter.cpp:5
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...