blob: f6cc15e363c10e7a98bdd9a8eda8927ddf78f08b (
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
|
// file : butl/optional -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#ifndef BUTL_OPTIONAL
#define BUTL_OPTIONAL
#include <utility> // move()
#include <functional> // hash
namespace butl
{
// Simple optional class template while waiting for std::optional.
//
struct nullopt_t {constexpr explicit nullopt_t (int) {}};
constexpr const nullopt_t nullopt (1);
template <typename T>
class optional
{
public:
typedef T value_type;
constexpr optional (): value_ (), null_ (true) {} // VC14 needs value_().
constexpr optional (nullopt_t): value_ (), null_ (true) {}
constexpr optional (const T& v): value_ (v), null_ (false) {}
constexpr optional (T&& v): value_ (std::move (v)), null_ (false) {}
optional& operator= (nullopt_t) {value_ = T (); null_ = true; return *this;}
optional& operator= (const T& v) {value_ = v; null_ = false; return *this;}
optional& operator= (T&& v) {value_ = std::move (v); null_ = false; return *this;}
T& value () {return value_;}
const T& value () const {return value_;}
T* operator-> () {return &value_;}
const T* operator-> () const {return &value_;}
T& operator* () {return value_;}
const T& operator* () const {return value_;}
bool has_value () const {return !null_;}
explicit operator bool () const {return !null_;}
private:
T value_;
bool null_;
};
template <typename T>
inline auto
operator== (const optional<T>& x, const optional<T>& y) -> decltype (*x == *y)
{
return static_cast<bool> (x) == static_cast<bool> (y) && (!x || *x == *y);
}
template <typename T>
inline auto
operator!= (const optional<T>& x, const optional<T>& y) -> decltype (x == y)
{
return !(x == y);
}
template <typename T>
inline auto
operator< (const optional<T>& x, const optional<T>& y) -> decltype (*x < *y)
{
bool px (x), py (y);
return px < py || (px && py && *x < *y);
}
template <typename T>
inline auto
operator> (const optional<T>& x, const optional<T>& y) -> decltype (*x > *y)
{
return y < x;
}
}
namespace std
{
template <typename T>
struct hash<butl::optional<T>>: hash<T>
{
using argument_type = butl::optional<T>;
size_t
operator() (const butl::optional<T>& o) const
noexcept (noexcept (hash<T> {} (*o)))
{
return o ? hash<T>::operator() (*o) : static_cast<size_t> (-3333);
}
};
}
#endif // BUTL_OPTIONAL
|