soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SEmptyable.hpp
1#ifndef _SEMPTYABLE_H_
2#define _SEMPTYABLE_H_
3
4#include <sdef.h>
5
6SNSBEGIN
7
8/**
9 * @class SEmptyable
10 * @brief A template class that can represent a value or be empty (NULL).
11 * @details This class allows a variable to either hold a value of type `T` or be in an empty state.
12 * @tparam T The type of the value to be stored.
13 */
14template <class T>
16{
17public:
18 /**
19 * @brief Constructor with initial value.
20 * @param val The initial value to store.
21 */
23 : m_val(val), m_empty(false)
24 {
25 }
26
27 /**
28 * @brief Default constructor.
29 * Initializes the object in an empty state.
30 */
32 : m_empty(true)
33 {
34 }
35
36 /**
37 * @brief Checks if the object is empty.
38 * @return TRUE if the object is empty, FALSE otherwise.
39 */
40 bool isEmpty() const
41 {
42 return m_empty;
43 }
44
45 /**
46 * @brief Equality operator with another SEmptyable object.
47 * @param right The SEmptyable object to compare with.
48 * @return TRUE if both objects are equal, FALSE otherwise.
49 */
50 bool operator==(const SEmptyable &right) const
51 {
52 if (this->isEmpty() != right.isEmpty())
53 {
54 return false;
55 }
56
57 // both are empty
58 if (this->isEmpty())
59 {
60 return true;
61 }
62
63 return this->m_val == right.m_val;
64 }
65
66 /**
67 * @brief Equality operator with a value of type T.
68 * @param right The value to compare with.
69 * @return TRUE if the object holds the same value, FALSE otherwise.
70 */
71 bool operator==(const T &right) const
72 {
73 if (this->isEmpty())
74 {
75 return false;
76 }
77
78 return this->m_val == right;
79 }
80
81 /**
82 * @brief Inequality operator with another SEmptyable object.
83 * @param right The SEmptyable object to compare with.
84 * @return TRUE if the objects are not equal, FALSE otherwise.
85 */
86 bool operator!=(const SEmptyable &right) const
87 {
88 return !(operator==(right));
89 }
90
91 /**
92 * @brief Inequality operator with a value of type T.
93 * @param right The value to compare with.
94 * @return TRUE if the object does not hold the same value, FALSE otherwise.
95 */
96 bool operator!=(const T &right) const
97 {
98 return !(operator==(right));
99 }
100
101 /**
102 * @brief Less-than operator with another SEmptyable object.
103 * @param right The SEmptyable object to compare with.
104 * @return TRUE if this object is less than the other object, FALSE otherwise.
105 */
106 bool operator<(const SEmptyable &right) const
107 {
108 if (this->isEmpty() != right.isEmpty())
109 {
110 return this->isEmpty();
111 }
112
113 if (this->isEmpty())
114 {
115 return false;
116 }
117
118 return this->m_val < right.m_val;
119 }
120
121 /**
122 * @brief Less-than operator with a value of type T.
123 * @param right The value to compare with.
124 * @return TRUE if this object holds a value less than the given value, FALSE otherwise.
125 */
126 bool operator<(const T &right) const
127 {
128 if (this->isEmpty())
129 {
130 return true;
131 }
132
133 return this->m_val < right;
134 }
135
136 /**
137 * @brief Assignment operator from another SEmptyable object.
138 * @param right The SEmptyable object to assign from.
139 * @return Reference to the current object.
140 */
142 {
143 m_val = right.m_val;
144 m_empty = right.m_empty;
145 return *this;
146 }
147
148 /**
149 * @brief Assignment operator from a value of type T.
150 * @param val The value to assign.
151 * @return Reference to the current object.
152 */
153 SEmptyable &operator=(const T &val)
154 {
155 m_val = val;
156 m_empty = false;
157 return *this;
158 }
159
160 /**
161 * @brief Conversion operator to type T.
162 * @return The stored value.
163 * @note Asserts that the object is not empty.
164 */
165 operator T() const
166 {
167 assert(m_empty == false);
168 return m_val;
169 }
170
171 /**
172 * @brief Resets the object to an empty state.
173 */
174 void reset()
175 {
176 m_empty = true;
177 }
178
179private:
180 T m_val; /**< The stored value. */
181 bool m_empty; /**< Flag indicating if the object is empty. */
182};
183
184SNSEND
185
186#endif // _SEMPTYABLE_H_
void reset()
Resets the object to an empty state.
SEmptyable()
Default constructor. Initializes the object in an empty state.
SEmptyable(T val)
Constructor with initial value.
bool isEmpty() const
Checks if the object is empty.
bool operator<(const T &right) const
Less-than operator with a value of type T.
SEmptyable & operator=(const T &val)
Assignment operator from a value of type T.
bool operator!=(const SEmptyable &right) const
Inequality operator with another SEmptyable object.
SEmptyable & operator=(const SEmptyable &right)
Assignment operator from another SEmptyable object.
bool operator<(const SEmptyable &right) const
Less-than operator with another SEmptyable object.
bool operator==(const T &right) const
Equality operator with a value of type T.
bool operator!=(const T &right) const
Inequality operator with a value of type T.
bool operator==(const SEmptyable &right) const
Equality operator with another SEmptyable object.