Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarMaybe.hpp
blob: 9576ee6fa81af14850623f3c6492fcd636e8d474 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#pragma once

#include "StarException.hpp"
#include "StarHash.hpp"

namespace Star {

STAR_EXCEPTION(InvalidMaybeAccessException, StarException);

template <typename T>
class Maybe {
public:
  typedef T* PointerType;
  typedef T const* PointerConstType;
  typedef T& RefType;
  typedef T const& RefConstType;

  Maybe();

  Maybe(T const& t);
  Maybe(T&& t);

  Maybe(Maybe const& rhs);
  Maybe(Maybe&& rhs);
  template <typename T2>
  Maybe(Maybe<T2> const& rhs);

  ~Maybe();

  Maybe& operator=(Maybe const& rhs);
  Maybe& operator=(Maybe&& rhs);
  template <typename T2>
  Maybe& operator=(Maybe<T2> const& rhs);

  bool isValid() const;
  bool isNothing() const;
  explicit operator bool() const;

  PointerConstType ptr() const;
  PointerType ptr();

  PointerConstType operator->() const;
  PointerType operator->();

  RefConstType operator*() const;
  RefType operator*();

  bool operator==(Maybe const& rhs) const;
  bool operator!=(Maybe const& rhs) const;
  bool operator<(Maybe const& rhs) const;

  RefConstType get() const;
  RefType get();

  // Get either the contents of this Maybe or the given default.
  T value(T def = T()) const;

  // Get either this value, or if this value is none the given value.
  Maybe orMaybe(Maybe const& other) const;

  // Takes the value out of this Maybe, leaving it Nothing.
  T take();

  // If this Maybe is set, assigns it to t and leaves this Maybe as Nothing.
  bool put(T& t);

  void set(T const& t);
  void set(T&& t);

  template <typename... Args>
  void emplace(Args&&... t);

  void reset();

  // Apply a function to the contained value if it is not Nothing.
  template <typename Function>
  void exec(Function&& function);

  // Functor map operator.  If this maybe is not Nothing, then applies the
  // given function to it and returns the result, otherwise returns Nothing (of
  // the type the function would normally return).
  template <typename Function>
  auto apply(Function&& function) const -> Maybe<typename std::decay<decltype(function(std::declval<T>()))>::type>;

  // Monadic bind operator.  Given function should return another Maybe.
  template <typename Function>
  auto sequence(Function function) const -> decltype(function(std::declval<T>()));

private:
  union {
    T m_data;
  };

  bool m_initialized;
};

template <typename T>
std::ostream& operator<<(std::ostream& os, Maybe<T> const& v);

template <typename T>
struct hash<Maybe<T>> {
  size_t operator()(Maybe<T> const& m) const;
  hash<T> hasher;
};

template <typename T>
Maybe<T>::Maybe()
  : m_initialized(false) {}

template <typename T>
Maybe<T>::Maybe(T const& t)
  : Maybe() {
  new (&m_data) T(t);
  m_initialized = true;
}

template <typename T>
Maybe<T>::Maybe(T&& t)
  : Maybe() {
  new (&m_data) T(std::forward<T>(t));
  m_initialized = true;
}

template <typename T>
Maybe<T>::Maybe(Maybe const& rhs)
  : Maybe() {
  if (rhs.m_initialized) {
    new (&m_data) T(rhs.m_data);
    m_initialized = true;
  }
}

template <typename T>
Maybe<T>::Maybe(Maybe&& rhs)
  : Maybe() {
  if (rhs.m_initialized) {
    new (&m_data) T(std::move(rhs.m_data));
    m_initialized = true;
    rhs.reset();
  }
}

template <typename T>
template <typename T2>
Maybe<T>::Maybe(Maybe<T2> const& rhs)
  : Maybe() {
  if (rhs) {
    new (&m_data) T(*rhs);
    m_initialized = true;
  }
}

template <typename T>
Maybe<T>::~Maybe() {
  reset();
}

template <typename T>
Maybe<T>& Maybe<T>::operator=(Maybe const& rhs) {
  if (&rhs == this)
    return *this;

  if (rhs)
    emplace(*rhs);
  else
    reset();

  return *this;
}

template <typename T>
template <typename T2>
Maybe<T>& Maybe<T>::operator=(Maybe<T2> const& rhs) {
  if (rhs)
    emplace(*rhs);
  else
    reset();

  return *this;
}

template <typename T>
Maybe<T>& Maybe<T>::operator=(Maybe&& rhs) {
  if (&rhs == this)
    return *this;

  if (rhs)
    emplace(rhs.take());
  else
    reset();

  return *this;
}

template <typename T>
bool Maybe<T>::isValid() const {
  return m_initialized;
}

template <typename T>
bool Maybe<T>::isNothing() const {
  return !m_initialized;
}

template <typename T>
Maybe<T>::operator bool() const {
  return m_initialized;
}

template <typename T>
auto Maybe<T>::ptr() const -> PointerConstType {
  if (m_initialized)
    return &m_data;
  return nullptr;
}

template <typename T>
auto Maybe<T>::ptr() -> PointerType {
  if (m_initialized)
    return &m_data;
  return nullptr;
}

template <typename T>
auto Maybe<T>::operator-> () const -> PointerConstType {
  if (!m_initialized)
    throw InvalidMaybeAccessException();

  return &m_data;
}

template <typename T>
auto Maybe<T>::operator->() -> PointerType {
  if (!m_initialized)
    throw InvalidMaybeAccessException();

  return &m_data;
}

template <typename T>
auto Maybe<T>::operator*() const -> RefConstType {
  return get();
}

template <typename T>
auto Maybe<T>::operator*() -> RefType {
  return get();
}

template <typename T>
bool Maybe<T>::operator==(Maybe const& rhs) const {
  if (!m_initialized && !rhs.m_initialized)
    return true;
  if (m_initialized && rhs.m_initialized)
    return get() == rhs.get();
  return false;
}

template <typename T>
bool Maybe<T>::operator!=(Maybe const& rhs) const {
  return !operator==(rhs);
}

template <typename T>
bool Maybe<T>::operator<(Maybe const& rhs) const {
  if (m_initialized && rhs.m_initialized)
    return get() < rhs.get();
  if (!m_initialized && rhs.m_initialized)
    return true;
  return false;
}

template <typename T>
auto Maybe<T>::get() const -> RefConstType {
  if (!m_initialized)
    throw InvalidMaybeAccessException();

  return m_data;
}

template <typename T>
auto Maybe<T>::get() -> RefType {
  if (!m_initialized)
    throw InvalidMaybeAccessException();

  return m_data;
}

template <typename T>
T Maybe<T>::value(T def) const {
  if (m_initialized)
    return m_data;
  else
    return def;
}

template <typename T>
Maybe<T> Maybe<T>::orMaybe(Maybe const& other) const {
  if (m_initialized)
    return *this;
  else
    return other;
}

template <typename T>
T Maybe<T>::take() {
  if (!m_initialized)
    throw InvalidMaybeAccessException();

  T val(std::move(m_data));

  reset();

  return val;
}

template <typename T>
bool Maybe<T>::put(T& t) {
  if (m_initialized) {
    t = std::move(m_data);

    reset();

    return true;
  } else {
    return false;
  }
}

template <typename T>
void Maybe<T>::set(T const& t) {
  emplace(t);
}

template <typename T>
void Maybe<T>::set(T&& t) {
  emplace(std::forward<T>(t));
}

template <typename T>
template <typename... Args>
void Maybe<T>::emplace(Args&&... t) {
  reset();

  new (&m_data) T(std::forward<Args>(t)...);
  m_initialized = true;
}

template <typename T>
void Maybe<T>::reset() {
  if (m_initialized) {
    m_initialized = false;
    m_data.~T();
  }
}

template <typename T>
template <typename Function>
auto Maybe<T>::apply(Function&& function) const
    -> Maybe<typename std::decay<decltype(function(std::declval<T>()))>::type> {
  if (!isValid())
    return {};
  return function(get());
}

template <typename T>
template <typename Function>
void Maybe<T>::exec(Function&& function) {
  if (isValid())
    function(get());
}

template <typename T>
template <typename Function>
auto Maybe<T>::sequence(Function function) const -> decltype(function(std::declval<T>())) {
  if (!isValid())
    return {};
  return function(get());
}

template <typename T>
std::ostream& operator<<(std::ostream& os, Maybe<T> const& v) {
  if (v)
    return os << "Just (" << *v << ")";
  else
    return os << "Nothing";
}

template <typename T>
size_t hash<Maybe<T>>::operator()(Maybe<T> const& m) const {
  if (!m)
    return 0;
  else
    return hasher(*m);
}

}

template <typename T>
struct fmt::formatter<Star::Maybe<T>> : ostream_formatter {};