This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.hh
857 lines (631 loc) · 20.4 KB
/
option.hh
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
* Copyright Noel Cower 2013 - 2014.
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#pragma once
#include <snow/config.hh>
#include <type_traits>
#include <ostream>
namespace snow {
/*==============================================================================
Classes for a basic Scala-like option. All options have one of two states:
- defined
- empty
When an option is defined, it has a value that can be acquired either by
dereferencing the option or calling its get() function. If an option is empty,
then trying do either will raise an exception.
A simple example of an option is as follows:
option_t<int> maybe_int = none; // Declare an empty int option
maybe_int = some(24); // Define its value to be 24
maybe_int = none; // and make it empty again
auto easier = some(24); // this will also work.
If using it to hold a pointer, keep in mind that options do not manage any
memory other than their own -- if its value is allocated memory, you will
have to free it yourself, as usual. In that case, you probably want to use a
shared_ptr.
That said, if you do declare a pointer option and assign nullptr to it, take
care when using auto to provide the option's type by doing either of the
following:
auto pointer_one = some<int *>(nullptr);
option_t<int *> pointer_two = some(nullptr);
Both of these work, but using auto and attempting to assign just some(nullptr)
will of course result in an option_t<std::nullptr_t>, which isn't very useful.
==============================================================================*/
struct none_t;
template <typename T>
class option_t;
/**
Struct contianing type aliases for an option's value_type and self type.
*/
template <typename T>
struct optional_bits
{
/// An option of type T's value_type. An alias for the decayed form of T.
using value_type = typename std::decay<T>::type;
/// An option of type T's self type, otherwise an option of type
/// option_t<value_type>.
using type = option_t<value_type>;
};
/**
Type alias using the decayed form of T for an option. Ensures that you don't
try to create an option of a reference, volatile, etc. type.
*/
template <typename T>
using optional = typename optional_bits<T>::type;
template <typename T>
optional<T> some(T &&val);
template <typename T, typename... ARGS>
optional<T> make_optional(ARGS&&... args);
/**
@brief Optional value type. Holds a possibly-defined value.
Options may be either defined (has some value) or undefined, in which case
it has no value. All options hold storage for their own value.
*/
template <typename T>
class option_t final
{
public:
// Occasionally useful if you need the value type of an option, but this is
// mainly here so flat_map can infer the value type of its returned option_t.
// Recommended that you use the optional<T> type alias to avoid issues with
// incompatible types despite using the same value_type.
/// @brief The bits structure used to determine this option's value_type.
using bits = optional_bits<T>;
/// @brief The value_type of the option, obtained from bits::value_type.
using value_type = typename bits::value_type;
private:
/// Bit of a hack to keep the private emplace-like constructor. By passing a
/// ctor_bit_t as the constructor's first argument, this basically ensures it
/// knows which ctor to use.
enum ctor_bit_t
{
/// CTOR enum argument used to disambiguate which constructor to use when
/// constructing optional values in place.
CTOR
};
enum store_bit_t
{
SIZE = sizeof(value_type),
ALIGNMENT = std::alignment_of<value_type>::value
};
using store_t = typename std::aligned_storage<SIZE, ALIGNMENT>::type;
/// Whether the option has a defined value (true if so, otherwise false).
bool _defined;
/// Storage for any value to be held by the option. Is aligned according to
/// the value provided by std::alignment_of.
store_t _data;
/// Returns a value_type pointer to the data used by the option. May have
/// an undefined value, so dereferencing it isn't guaranteed to be safe
/// unless _defined is true.
value_type *typed()
{
return reinterpret_cast<value_type *>(&_data);
}
/// Returns a value_type pointer to the data used by the option. May have
/// an undefined value, so dereferencing it isn't guaranteed to be safe
/// unless _defined is true.
value_type const *typed() const
{
return reinterpret_cast<value_type const *>(&_data);
}
/// Throws an std::runtime_error if the option is undefined.
void throw_if_undefined() const
{
if (!_defined) {
s_throw(std::runtime_error, "Attempt to access empty option");
}
}
/// Destroys any value held by the option and makes it undefined.
void dispose();
friend class option_t<std::nullptr_t>;
/**
In-place constructor for options with defined values. Must pass CTOR
enum value as the first argument in order for the compiler to properly
disambiguate the constructor.
*/
template <typename... ARGS>
option_t(ctor_bit_t, ARGS&&... args);
public:
/**
@brief Constructs a new instance of an option with a defined value in-place.
Returns an option with a defined value constructed in-place rather than by
copying or moving an existing defined (or undefined) option). Passes any
arguments received to one of the optional value type's constructors,
assuming such a constructor exists.
The reason for make vs. just providing a forwarding ctor is that the
forwarding ctor could be option_t's ctor, in which case multiple
definitions of an option_t ctor are created. So, this keeps things
sort of normal.
*/
template <typename... ARGS>
static option_t make(ARGS&&... args);
/**
@brief Default constructor.
Constructs an empty optional.
*/
option_t() noexcept;
~option_t();
/// @brief Constructs an empty optional by copying nothing.
option_t(none_t const &) noexcept; // same as default ctor
/// @brief Copy constructor.
option_t(option_t const &copied);
/// @brief Move constructor.
option_t(option_t &&moved);
/// @brief None-assignment operator. Assigns nothing, making the option
/// undefined.
option_t &operator = (none_t const &);
/// @brief Copy-assignment operator.
option_t &operator = (option_t const &);
/// @brief Move-assignment operator.
option_t &operator = (option_t &&);
/// @brief None-assignment. Assigns nothing, making the option undefined.
option_t &assign(none_t const &);
/// @brief Copy-assignment.
option_t &assign(option_t const &);
/// @brief Move-assignment.
option_t &assign(option_t &&);
/// @brief Returns whether the option is defined.
bool is_defined() const noexcept { return _defined; }
/// @brief Returns whether the option is empty (undefined). Opposite of
/// is_defined.
bool is_empty() const noexcept { return !is_defined(); }
/// @brief Gets the value of the option if defined, otherwise throws an
/// std::runtime_error exception.
value_type &get()
{
throw_if_undefined();
return *typed();
}
/// @brief Gets the value of the option if defined, otherwise throws an
/// std::runtime_error exception.
value_type const &get() const
{
throw_if_undefined();
return *typed();
}
/// @brief Dereference operator. Gets the value of the option if defined,
/// otherwise throws an std::runtime_error exception.
value_type &operator * () { return get(); }
/// @brief Dereference operator. Gets the value of the option if defined,
/// otherwise throws an std::runtime_error exception.
value_type const &operator * () const { return get(); }
/// @brief Dereference operator -- returns a pointer to the value of the
/// option.
value_type *operator -> () const { return get(); }
/// @brief Implicit bool conversion -- returns whether the option is defined.
operator bool () const { return is_defined(); }
/// @brief Unary logical not operator -- returns whether the option is empty.
bool operator ! () const { return is_empty(); }
/// @brief Returns an empty (default constructed) option_t.
static auto empty() noexcept -> option_t;
/**
@brief Apply function -- calls function f, passing the value of the option
to f, if and only if the option has a defined value. Receives a const
reference to or copy of the option's value.
Given an application function that takes a reference of the type held by
the underlying option, the call operator will only call the given function
if the value of the option is defined. If empty, it returns early.
*/
template <typename F>
void operator () (F &&f) const
{
if (is_defined()) {
f(get());
}
}
/**
@brief Apply function -- calls function f, passing the value of the option
to f, if and only if the option has a defined value. Receives a reference to
or copy of the option's value.
Given an application function that takes a reference of the type held by
the underlying option, the call operator will only call the given function
if the value of the option is defined. If empty, it returns early.
*/
template <typename F>
void operator () (F &&f)
{
if (is_defined()) {
f(get());
}
}
/**
@brief Maps the value of this option to another value, returning a new
option with the result of function f.
This is a simple map function -- given a function that takes T and returns
type U (may be anything), it returns an optional<U> with a defined value
if the option is defined, otherwise returns an empty option. Contrast this
with flat_map's behavior, which is the same, but uses a function that
returns an optional<U> instead of just U. If your map function may map
from a defined value to an empty value, then use flat_map. If your map
function always has a defined value, use map.
*/
template <typename F>
auto map(F &&f) const -> optional<decltype(f(get()))>;
/**
@brief Maps the value of this option to another value, returning the result
of function f. Function f must return an optional type.
Similar to map, but the map function must return an optional<U> instead of
just a U value. This allows it to map any defined value to an empty value.
If your function always returns a defined value, use map.
*/
template <typename F>
auto flat_map(F &&f) const -> optional<typename decltype(f(get()))::value_type>;
};
template <typename T>
template <typename... ARGS>
auto option_t<T>::make(ARGS&&... args) -> option_t
{
return option_t(CTOR, std::forward<ARGS>(args)...);
}
template <typename T>
auto option_t<T>::empty() noexcept -> option_t
{
return option_t();
}
template <typename T>
void option_t<T>::dispose()
{
if (_defined) {
typed()->~T();
}
_defined = false;
}
template <typename T>
template <typename... ARGS>
option_t<T>::option_t(ctor_bit_t, ARGS&&... args)
: _defined(true)
{
new(typed()) T(std::forward<ARGS>(args)...);
}
template <typename T>
option_t<T>::option_t() noexcept
: _defined(false)
{
/* nop */
}
template <typename T>
option_t<T>::option_t(none_t const &n) noexcept
: _defined(false)
{
/* nop */
}
template <typename T>
option_t<T>::~option_t()
{
dispose();
}
template <typename T>
option_t<T>::option_t(option_t const &copied)
: _defined(false)
{
assign(copied);
}
template <typename T>
option_t<T>::option_t(option_t &&moved)
: _defined(false)
{
assign(moved);
}
template <typename T>
auto option_t<T>::operator = (none_t const &n) -> option_t &
{
dispose();
return *this;
}
template <typename T>
auto option_t<T>::operator = (option_t const &copied) -> option_t &
{
assign(copied);
return *this;
}
template <typename T>
auto option_t<T>::operator = (option_t &&moved) -> option_t &
{
assign(std::forward<option_t>(moved));
return *this;
}
template <typename T>
auto option_t<T>::assign(none_t const &n) -> option_t &
{
dispose();
return *this;
}
template <typename T>
auto option_t<T>::assign(option_t const &other) -> option_t &
{
dispose();
if (other.is_defined()) {
new(typed()) T(other.get());
_defined = true;
}
return *this;
}
template <typename T>
auto option_t<T>::assign(option_t &&other) -> option_t &
{
dispose();
if (other.is_defined()) {
new(typed()) T(std::move(other.get()));
_defined = true;
other.dispose();
}
return *this;
}
template <typename T>
template <typename F>
auto option_t<T>::map(F &&f) const -> optional<decltype(f(get()))>
{
using U = decltype(f(get()));
if (is_defined()) {
return some<U>(f(get()));
} else {
return optional<U>();
}
}
template <typename T>
template <typename F>
auto option_t<T>::flat_map(F &&f) const -> optional<typename decltype(f(get()))::value_type>
{
using U = typename decltype(f(get()))::value_type;
if (is_defined()) {
return f(get());
} else {
return optional<U>();
}
}
template<typename T, typename U>
bool operator == (option_t<T> const &lhs, option_t<U> const &rhs)
{
return (lhs.is_defined() && rhs.is_defined()) && (lhs.get() == rhs.get());
}
template<typename T, typename U>
bool operator != (option_t<T> const &lhs, option_t<U> const &rhs)
{
return !(lhs == rhs);
}
template<typename T>
bool operator == (option_t<T> const &lhs, none_t const &rhs) noexcept
{
return lhs.is_empty();
}
template<typename T>
bool operator == (none_t const &lhs, option_t<T> const &rhs) noexcept
{
return rhs.is_empty();
}
template<typename T>
bool operator != (option_t<T> const &lhs, none_t const &rhs) noexcept
{
return lhs.is_defined();
}
template<typename T>
bool operator != (none_t const &lhs, option_t<T> const &rhs) noexcept
{
return rhs.is_defined();
}
constexpr bool operator == (none_t const &lhs, none_t const &rhs) noexcept
{
return true;
}
constexpr bool operator != (none_t const &lhs, none_t const &rhs) noexcept
{
return false;
}
template<typename T>
std::ostream &operator << (std::ostream &out, option_t<T> const &opt)
{
if (opt.is_defined()) {
return out << "some(" << opt.get() << ')';
} else {
return out << "none";
}
}
/**
@brief Class representing an empty type -- i.e., a unit value, or nothing.
May be implicitly cast to an empty optional<T> of any kind.
none_t is an empty type that has no value and is mostly only ever used when
assigning the constexpr value none to options. It really has no purpose
outside of that use.
*/
struct none_t final
{
struct value_type final {};
constexpr none_t() { /* nop */ }
constexpr bool is_defined() const { return false; }
constexpr bool is_empty() const { return true; }
constexpr operator bool () const { return false; }
constexpr bool operator ! () const { return true; }
template <typename T>
operator option_t<T> () const
{
return option_t<T>();
}
};
/// @brief Global constant none -- may be used to assign no value to an optional
/// or otherwise passed around to indicate no value, since it can be implicitly
/// converted to an optional of any type.
constexpr none_t none {};
inline std::ostream &operator << (std::ostream &out, none_t const &non)
{
return out << "none";
}
inline std::ostream &operator << (std::ostream &out, none_t::value_type const &)
{
return out << "()";
}
/**
@brief nullptr_t specialization of option_t.
Generally a bad idea to use this, but the specialization's needed to avoid
attempts to do horrible things like `new std::nullptr_t` which as we all know
is bad. Implements a compatible subset of option_t's interface, though less,
as this shouldn't be used except to do things like assign some(nullptr) to
an option_t<T != nullptr_t>.
*/
template <>
class option_t<std::nullptr_t> final
{
bool _defined;
enum ctor_bit_t { CTOR };
option_t(ctor_bit_t, std::nullptr_t) : _defined(true) { /* nop */ }
public:
using value_type = std::nullptr_t;
static option_t empty() noexcept { return option_t(); }
bool is_defined() const noexcept { return _defined; }
bool is_empty() const noexcept { return !is_defined(); }
/**
@brief Default constructor.
Constructs an empty optional of nullptr_t.
*/
option_t() noexcept : _defined(false) { /* nop */ }
/// @brief Copy constructor.
option_t(option_t const &opt) noexcept
: _defined(opt._defined) { /* nop */ }
/// @brief Move constructor.
option_t(option_t &&opt) noexcept
: _defined(opt._defined)
{
opt._defined = false;
}
/// @brief Copy-assignment operator.
option_t &operator = (option_t const &opt) noexcept
{
return assign(opt);
}
/// @brief Move-assignment operator.
option_t &operator = (option_t &&opt) noexcept
{
return assign(std::forward<option_t>(opt));
}
/// @brief Assign nothing to the option.
option_t &operator = (none_t const &non) noexcept
{
return assign(non);
}
/// @brief Assign nothing to the option.
option_t &assign(none_t const &non) noexcept
{
_defined = false;
return *this;
}
/// @brief Copy assignment.
option_t &assign(option_t const &opt) noexcept
{
_defined = opt._defined;
return *this;
}
/// @brief Move assignment.
option_t &assign(option_t &&opt) noexcept
{
_defined = opt._defined;
opt._defined = false;
return *this;
}
/// @brief Gets the value of the option if defined, otherwise throws an
/// std::runtime_error exception.
std::nullptr_t get() const
{
if (is_empty()) {
s_throw(std::runtime_error, "Attempt to access empty option");
}
return nullptr;
}
/// @brief Gets the value of the option if defined, otherwise throws an
/// std::runtime_error exception.
std::nullptr_t operator * () const
{
if (is_empty()) {
s_throw(std::runtime_error, "Attempt to access empty option");
}
return nullptr;
}
/**
@brief Recast option and its value ti an option of type U.
This attempts to construct a new object of type U by passing a const
reference or the value of this option to the new option's constructor.
The new option's value is constructed in place.
*/
template <typename U>
operator option_t<U> () const
{
if (is_defined()) {
return option_t<U>(option_t<U>::CTOR, U { **this });
} else {
return option_t<U>();
}
}
/**
@brief Apply the function f to the value of the option if and only if the
option has a defined value.
*/
template <typename F>
void operator () (F &&f) const
{
if (is_defined()) {
f(nullptr);
}
}
/**
@brief Apply the function f to the value of the option if and only if the
option has a defined value and return a new option with the result of the
function.
The function f must return a value of type U (whatever type the function
returns), which will be stored as the value of an optional<U>. The function
must return something. If you need to be able to return nothing for
possibly unwanted values, use flat_map to return either some(U) or none.
*/
template <typename F>
auto map(F &&f) const -> optional<decltype(f(nullptr))>;
/**
@brief Apply the function f to the value of the option if and only if the
option has a defined value and return a new option with the result of the
function.
Similar to map, except that function f must return an optional<U> instead
of just a U. As such, the function may choose to return either nothing or
something.
*/
template <typename F>
auto flat_map(F &&f) const -> optional<typename decltype(f(nullptr))::value_type>;
};
template <typename F>
auto option_t<std::nullptr_t>::map(F &&f) const -> optional<decltype(f(nullptr))>
{
using U = decltype(f(nullptr));
if (is_defined()) {
return some<U>(f(nullptr));
} else {
return optional<U>();
}
}
template <typename F>
auto option_t<std::nullptr_t>::flat_map(F &&f) const -> optional<typename decltype(f(nullptr))::value_type>
{
using U = typename decltype(f(nullptr))::value_type;
if (is_defined()) {
return f(nullptr);
} else {
return optional<U>();
}
}
/**
@brief Creates a new option_t<T> and returns it, either copying or moving the
value provided.
*/
template <typename T>
optional<T> some(T &&val)
{
return make_optional<T>(std::forward<T>(val));
}
/**
@brief Creates a new optional and constructs its value in-place using the
given arguments, forwarded to T's cnstructor. Just an alias around
optional<T>::make(...).
*/
template <typename T, typename... ARGS>
optional<T> make_optional(ARGS&&... args)
{
return optional<T>::make(std::forward<ARGS>(args)...);
}
} // namespace snow