-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptional.h
More file actions
201 lines (164 loc) · 5.16 KB
/
optional.h
File metadata and controls
201 lines (164 loc) · 5.16 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
#ifndef OPTIONAL_H_jjj54444409vgfdgkj234
#define OPTIONAL_H_jjj54444409vgfdgkj234
#include <memory>
#include <ostream>
template<typename T>
class FreeStorePolicy {
std::unique_ptr<T> data;
public:
FreeStorePolicy() {}
explicit FreeStorePolicy(T const & value): data(new T(value)) {}
FreeStorePolicy(FreeStorePolicy const & policy): data( policy.data ? new T(*policy.data) : 0 ) {}
FreeStorePolicy(FreeStorePolicy &&) = default;
FreeStorePolicy & operator=(FreeStorePolicy const & policy) {
swap(FreeStorePolicy(policy));
return *this;
}
FreeStorePolicy & operator=(FreeStorePolicy &&) = default;
void swap(FreeStorePolicy && other) throw() { data.swap(other.data); }
void setValue(T const & value) {
if( data ) {
*data = value;
} else {
data.reset(new T(value));
}
}
T & operator*() { return *data; }
T const & operator*() const { return *data; }
T * operator->() { return data.get(); }
T const * operator->() const { return data.get(); }
};
template<typename T>
class AutomaticStoragePolicy {
bool hasValue;
T data;
public:
AutomaticStoragePolicy(): hasValue(false) {}
explicit AutomaticStoragePolicy(T const & value): hasValue(true), data(value) {}
void swap(AutomaticStoragePolicy && other) throw() {
std::swap(hasValue,other.hasValue);
std::swap(data,std::move(other.data));
}
void setValue(T const & value) {
hasValue = true;
data = value;
}
T & operator*() { return data; }
T const & operator*() const { return data; }
T * operator->() { return hasValue ? &data : 0; }
T const * operator->() const { return hasValue ? &data : 0; }
};
// TODO make conversion operators
/*
template<typename T, bool freestore>
struct StoragePolicyTraits {
template <typename U = T> struct type : FreeStorePolicy<U> { type() {}; type(U const & value): FreeStorePolicy<U>(value) {} };
};
template<typename T>
struct StoragePolicyTraits<T,false> {
template <typename U = T> struct type : AutomaticStoragePolicy<U> { type() {}; type(U const & value): AutomaticStoragePolicy<U>(value) {} };
};
template<typename T, template<typename> class StoragePolicy>
class basic_optional {
StoragePolicy<T> storage;
public:
basic_optional() {}
basic_optional(T const & value):
storage(value)
{}
void swap(basic_optional && opt) throw()
{
storage.swap(std::move(opt.storage));
}
basic_optional & operator=(T const & value)
{
storage.setValue(value);
return *this;
}
T & operator*() { return *storage; }
T const & operator*() const { return *storage; }
T * operator->() { return storage.operator->(); }
T const * operator->() const { return storage.operator->(); }
operator void const * () const { return operator->(); }
//operator bool const () { return operator->() != 0; }
bool operator!() const { return operator->() == 0; }
bool operator==(basic_optional const & opt) const
{
auto hasValue = bool(operator const void *());
if( hasValue != bool(opt.operator const void *()) ) return false;
if( ! hasValue ) return true;
return operator*() == opt.operator*();
}
bool operator!=(basic_optional const & opt) const { return ! (*this == opt); }
};
template<typename T>
class optional: public basic_optional<T, StoragePolicyTraits<T,(sizeof(T) > sizeof(T*))>::template type> {
public:
optional() {}
optional(T const & value):
basic_optional<T, StoragePolicyTraits<T,(sizeof(T) > sizeof(T*))>::template type>(value)
{}
optional & operator=(T const & value)
{
basic_optional<T, StoragePolicyTraits<T,(sizeof(T) > sizeof(T*))>::template type>::operator=(value);
return *this;
}
bool operator==(optional const & opt) const
{
return basic_optional<T, StoragePolicyTraits<T,(sizeof(T) > sizeof(T*))>::template type>::operator==(opt);
}
bool operator!=(optional const & opt) const
{
return basic_optional<T, StoragePolicyTraits<T,(sizeof(T) > sizeof(T*))>::template type>::operator!=(opt);
}
};
*/
template<bool big>
struct StoragePolicyTraits {
template <class T> using type = FreeStorePolicy<T>;
};
template<>
struct StoragePolicyTraits<false> {
template <class T> using type = AutomaticStoragePolicy<T>;
};
template<typename T, template<typename> class StoragePolicy = StoragePolicyTraits<(sizeof(T) > sizeof(T*))>::template type>
class optional {
StoragePolicy<T> storage;
public:
optional() {}
optional(T const & value):
storage(value)
{}
void swap(optional && opt) throw()
{
storage.swap(std::move(opt.storage));
}
optional & operator=(T const & value)
{
storage.setValue(value);
return *this;
}
T & operator*() { return *storage; }
T const & operator*() const { return *storage; }
T * operator->() { return storage.operator->(); }
T const * operator->() const { return storage.operator->(); }
operator void const * () const { return operator->(); }
bool operator!() const { return operator->() == 0; }
bool operator==(optional const & opt) const
{
auto hasValue = bool(operator const void *());
if( hasValue != bool(opt.operator const void *()) ) return false;
if( ! hasValue ) return true;
return operator*() == opt.operator*();
}
};
template<typename T>
std::ostream & operator<<(std::ostream & os, optional<T> const & opt) {
if( opt ) {
os << *opt;
} else {
os << "nil";
}
return os;
}
#endif