forked from sttp/cppapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullable.h
More file actions
352 lines (288 loc) · 9.15 KB
/
Nullable.h
File metadata and controls
352 lines (288 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//******************************************************************************************************
// Nullable.h - Gbtc
//
// Copyright © 2019, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 12/03/2018 - J. Ritchie Carroll
// Generated original version of source code based on following stack overflow discussion:
// https://stackoverflow.com/questions/2537942/nullable-values-in-c
//
//******************************************************************************************************
#pragma once
#include "Convert.h"
namespace sttp
{
template<class T>
class Nullable final // NOLINT
{
public:
Nullable();
Nullable(const T& value);
Nullable(nullptr_t nullpointer);
const Nullable<T>& operator=(const Nullable<T>& value); // NOLINT
const Nullable<T>& operator=(const T& value); // NOLINT
const Nullable<T>& operator=(nullptr_t nullpointer); // NOLINT
bool HasValue() const;
const T& GetValueOrDefault() const;
const T& GetValueOrDefault(const T& def) const;
bool TryGetValue(T& value) const;
class NullableValue final // NOLINT
{
private:
NullableValue();
NullableValue(const T& value);
void checkHasValue() const;
bool m_hasValue;
T m_value;
public:
NullableValue& operator=(const NullableValue&) = delete;
operator const T&() const;
const T& operator*() const;
const T* operator&() const; // NOLINT
// https://stackoverflow.com/questions/42183631/inability-to-overload-dot-operator-in-c
const T* operator->() const;
friend class Nullable;
};
NullableValue Value;
};
template<class T>
Nullable<T>::NullableValue::NullableValue() : // NOLINT
m_hasValue(false),
m_value(T())
{
}
template<class T>
Nullable<T>::NullableValue::NullableValue(const T& value):
m_hasValue(true),
m_value(value)
{
}
template<class T>
Nullable<T>::NullableValue::operator const T&() const
{
checkHasValue();
return m_value;
}
template<class T>
const T& Nullable<T>::NullableValue::operator*() const
{
checkHasValue();
return m_value;
}
template<class T> // NOLINT
const T* Nullable<T>::NullableValue::operator&() const // NOLINT
{
checkHasValue();
return &m_value;
}
template<class T>
const T* Nullable<T>::NullableValue::operator->() const
{
checkHasValue();
return &m_value;
}
template<class T>
void Nullable<T>::NullableValue::checkHasValue() const
{
if (!m_hasValue)
throw std::runtime_error("Nullable object must have a value");
}
template<class T>
bool Nullable<T>::HasValue() const { return Value.m_hasValue; }
template<class T>
const T& Nullable<T>::GetValueOrDefault() const
{
return Value.m_value;
}
template<class T>
const T& Nullable<T>::GetValueOrDefault(const T& def) const
{
if (Value.m_hasValue)
return Value.m_value;
return def;
}
template<class T>
bool Nullable<T>::TryGetValue(T& value) const
{
value = Value.m_value;
return Value.m_hasValue;
}
template<class T>
Nullable<T>::Nullable()
{
}
template<class T>
Nullable<T>::Nullable(nullptr_t nullpointer)
{
(void)nullpointer;
}
template<class T>
Nullable<T>::Nullable(const T& value)
: Value(value)
{
}
template<class T2>
bool operator==(const Nullable<T2> &op1, const Nullable<T2> &op2)
{
if (op1.Value.m_hasValue != op2.Value.m_hasValue)
return false;
if (op1.Value.m_hasValue)
return op1.Value.m_value == op2.Value.m_value;
return true;
}
template<class T2>
bool operator==(const Nullable<T2> &op, const T2 &value)
{
if (!op.Value.m_hasValue)
return false;
return op.Value.m_value == value;
}
template<class T2>
bool operator==(const T2 &value, const Nullable<T2> &op)
{
if (!op.Value.m_hasValue)
return false;
return op.Value.m_value == value;
}
template<class T2>
bool operator==(const Nullable<T2> &op, nullptr_t nullpointer)
{
(void)nullpointer;
return !op.Value.m_hasValue;
}
template<class T2>
bool operator==(nullptr_t nullpointer, const Nullable<T2> &op)
{
(void)nullpointer;
return !op.Value.m_hasValue;
}
template<class T2>
bool operator!=(const Nullable<T2> &op1, const Nullable<T2> &op2)
{
if (op1.Value.m_hasValue != op2.Value.m_hasValue)
return true;
if (op1.Value.m_hasValue)
return op1.Value.m_value != op2.Value.m_value;
return false;
}
template<class T2>
bool operator!=(const Nullable<T2> &op, const T2 &value)
{
if (!op.Value.m_hasValue)
return true;
return op.Value.m_value != value;
}
template<class T2>
bool operator!=(const T2 &value, const Nullable<T2> &op)
{
if (!op.Value.m_hasValue)
return false;
return op.Value.m_value != value;
}
template<class T2>
bool operator!=(const Nullable<T2> &op, nullptr_t nullpointer)
{
(void)nullpointer;
return op.Value.m_hasValue;
}
template<class T2>
bool operator!=(nullptr_t nullpointer, const Nullable<T2> &op)
{
(void)nullpointer;
return op.Value.m_hasValue;
}
template<class T> // NOLINT
const Nullable<T>& Nullable<T>::operator=(const Nullable<T>& value)
{
Value.m_hasValue = value.Value.m_hasValue;
Value.m_value = value.Value.m_value;
return *this;
}
template<class T> // NOLINT
const Nullable<T>& Nullable<T>::operator=(const T& value)
{
Value.m_hasValue = true;
Value.m_value = value;
return *this;
}
template<class T> // NOLINT
const Nullable<T>& Nullable<T>::operator=(nullptr_t nullpointer)
{
(void)nullpointer;
Value.m_hasValue = false;
Value.m_value = T();
return *this;
}
template<class T, class U>
Nullable<T> CastAsNullable(const Nullable<U>& source)
{
if (source.HasValue())
return static_cast<T>(source.GetValueOrDefault());
return nullptr;
}
template<class T>
std::string ToString(const Nullable<T>& value)
{
if (value.HasValue())
return ToString(value.GetValueOrDefault());
return {};
}
inline std::string ToString(const Nullable<std::string>& value)
{
if (value.HasValue())
return value.GetValueOrDefault();
return {};
}
inline std::string ToString(const Nullable<bool>& value)
{
if (value.HasValue())
return value.GetValueOrDefault() ? "true" : "false";
return {};
}
inline std::string ToString(const Nullable<decimal_t>& value)
{
if (value.HasValue())
return value.GetValueOrDefault().str();
return {};
}
inline std::string ToString(const Nullable<Guid>& value)
{
if (value.HasValue())
return ToString(value.GetValueOrDefault());
return {};
}
inline std::string ToString(const Nullable<datetime_t>& value, const char* fmt = "%Y-%m-%d %H:%M:%S%F")
{
if (value.HasValue())
return ToString(value.GetValueOrDefault(), fmt);
return {};
}
template<class T>
static int32_t CompareValues(const Nullable<T>& leftNullable, const Nullable<T>& rightNullable)
{
const bool leftHasValue = leftNullable.HasValue();
const bool rightHasValue = rightNullable.HasValue();
if (leftHasValue && rightHasValue)
{
const T& leftValue = leftNullable.GetValueOrDefault();
const T& rightValue = rightNullable.GetValueOrDefault();
return leftValue < rightValue ? -1 : (leftValue > rightValue ? 1 : 0);
}
if (!leftHasValue && !rightHasValue)
return 0;
return leftHasValue ? 1 : -1;
}
}