cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TOptional.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27namespace cppcore {
28
29//-------------------------------------------------------------------------------------------------
34//-------------------------------------------------------------------------------------------------
35template<class T>
36class TOptional {
37public:
39 TOptional() = delete;
40
43 explicit TOptional(T defaultValue);
44
46 ~TOptional() = default;
47
50 bool isInited() const;
51
54 T value();
55
58 void set(T value);
59
61 T &operator = (const T &value);
62
64 bool operator == (const TOptional<T> &rhs) const;
65
66private:
67 T mValue{};
68 bool mInited{false};
69};
70
71template<class T>
72inline TOptional<T>::TOptional(T defaultValue) : mValue(defaultValue) {
73 // empty
74}
75
76template<class T>
77inline bool TOptional<T>::isInited() const {
78 return mInited;
79}
80
81template<class T>
83 if (mInited) {
84 return mValue;
85 }
86
87 return T{};
88}
89
90template<class T>
91inline void TOptional<T>::set(T value) {
92 assert(!mInited);
93
94 mValue = value;
95 mInited = true;
96}
97
98template<class T>
99inline T &TOptional<T>::operator = (const T &value) {
100 mValue = value;
101 if(!mInited) {
102 mInited = true;
103 }
104
105 return *this;
106}
107
108template<class T>
109inline bool TOptional<T>::operator == (const TOptional<T> &rhs) const {
110 if (mInited == rhs.mInited) {
111 if (!mInited) {
112 return true;
113 }
114
115 return mValue == rhs.mValue;
116 }
117
118 return false;
119}
120
121} // namespace cppcore
T value()
Will return the value or the default value if no init was performed before.
Definition TOptional.h:82
bool operator==(const TOptional< T > &rhs) const
The copy constructor.
Definition TOptional.h:109
T & operator=(const T &value)
Default operators.
Definition TOptional.h:99
bool isInited() const
Will return false, if the value was not set,.
Definition TOptional.h:77
void set(T value)
Will set the value, the instance will be in init state true afterwards.
Definition TOptional.h:91
~TOptional()=default
The class destructor.
TOptional()=delete
The default class constructor is deleted.