forked from BOT-Man-JL/ORM-Lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullable.h
118 lines (103 loc) · 2.82 KB
/
nullable.h
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
// Nullable module for ORM Lite
// ORM Lite - An ORM for SQLite in C++ 17
// https://github.com/BOT-Man-JL/ORM-Lite
// BOT Man, 2017
#ifndef BOT_ORM_NULLABLE_H
#define BOT_ORM_NULLABLE_H
// std::nullptr_t
#include <cstddef>
// Nullable Template
// https://stackoverflow.com/questions/2537942/nullable-values-in-c/28811646#28811646
namespace BOT_ORM
{
template <typename T>
class Nullable
{
template <typename T2>
friend bool operator== (const Nullable<T2> &op1,
const Nullable<T2> &op2);
template <typename T2>
friend bool operator== (const Nullable<T2> &op,
const T2 &value);
template <typename T2>
friend bool operator== (const T2 &value,
const Nullable<T2> &op);
template <typename T2>
friend bool operator== (const Nullable<T2> &op,
std::nullptr_t);
template <typename T2>
friend bool operator== (std::nullptr_t,
const Nullable<T2> &op);
public:
// Default or Null Construction
Nullable ()
: m_hasValue (false), m_value (T ())
{}
Nullable (std::nullptr_t)
: Nullable ()
{}
// Null Assignment
const Nullable<T> & operator= (std::nullptr_t)
{
m_hasValue = false;
m_value = T ();
return *this;
}
// Value Construction
template<typename T2>
Nullable (const T2 &value)
: m_hasValue (true), m_value (value)
{}
// Value Assignment
template<typename T2>
const Nullable<T> & operator= (const T2 &value)
{
m_hasValue = true;
m_value = value;
return *this;
}
private:
bool m_hasValue;
T m_value;
public:
const T &Value () const
{
return m_value;
}
};
// == varialbe
template <typename T2>
inline bool operator== (const Nullable<T2> &op1,
const Nullable<T2> &op2)
{
return op1.m_hasValue == op2.m_hasValue &&
(!op1.m_hasValue || op1.m_value == op2.m_value);
}
// == value
template <typename T2>
inline bool operator== (const Nullable<T2> &op,
const T2 &value)
{
return op.m_hasValue && op.m_value == value;
}
template <typename T2>
inline bool operator== (const T2 &value,
const Nullable<T2> &op)
{
return op.m_hasValue && op.m_value == value;
}
// == nullptr
template <typename T2>
inline bool operator== (const Nullable<T2> &op,
std::nullptr_t)
{
return !op.m_hasValue;
}
template <typename T2>
inline bool operator== (std::nullptr_t,
const Nullable<T2> &op)
{
return !op.m_hasValue;
}
}
#endif // !BOT_ORM_NULLABLE_H