aboutsummaryrefslogtreecommitdiff
path: root/option.h
blob: a3ad60a3260b429f727d45e80f9b7d4584a49b23 (plain) (blame)
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
#ifndef UTILS_OPTION_H
#define UTILS_OPTION_H

#include <cassert>
#include <new>  // placement new
#include <type_traits>
#include <utility>  // move

namespace option {
namespace impl {
/// Definition of std::is_trivially_destructible_v for older c++ std versions.
template <typename T>
constexpr bool is_trivially_destructible_v =
    std::is_trivially_destructible<T>::value;

/// Definition of std::enable_if_t for older c++ std versions.
template <bool Cond, typename T = bool>
using enable_if_t = typename std::enable_if<Cond, T>::type;
}  // namespace impl

/// The NONE type.
struct none {};

/// The OPTION type.
template <typename T>
struct option {
  static_assert(!std::is_reference<T>::value, "T must not be a reference type");

  // -- CONSTRUCTOR - NONE -----------------------------------------------------

  constexpr option() = default;
  constexpr option(none) : option() {}

  // -- CONSTRUCTOR - VALUE ----------------------------------------------------

  constexpr option(const option& op) : m_has_value{op.has_value()} {
    if (op.has_value()) {
      // Copy construct from inner VALUE of OP.
      new (&m_value) T(op.value());
    }
  }

  constexpr option(option&& op) noexcept : m_has_value{op.has_value()} {
    if (op.m_has_value) {
      // Move construct from inner VALUE of OP.
      new (&m_value) T(std::move(op.take()));
    }
  }

  template <typename U = T>
  constexpr option(T&& val) : m_has_value{true} {
    new (&m_value) T(std::move(val));
  }

  // -- DESTRUCTOR -------------------------------------------------------------

  ~option() {
    reset();
  }

  // -- MODIFIER ---------------------------------------------------------------

  template <typename... Params>
  constexpr T& emplace(Params&&... params) {
    reset();
    new (&m_value) T(std::forward<Params>(params)...);
    m_has_value = true;
    return value();
  }

  // -- OPERATOR ---------------------------------------------------------------

  /// Conversion to BOOL, true iff option holds a VALUE.
  ///
  /// Marked as explicit to disable implicit conversion of option objects to
  /// bool in the following case:
  ///
  ///   if (opt1 == opt2) {
  ///       ...
  ///   }
  constexpr explicit operator bool() const {
    return has_value();
  }

  // -- ACCESSOR ---------------------------------------------------------------

  constexpr bool has_value() const {
    return m_has_value;
  }

  constexpr T& value() & {
    assert(m_has_value);
    // Launder pointer, this informs the compiler that certain optimizations are
    // not applicable such as CONSTPROP. This is required because every use
    // of PLACEMENT new on M_VALUE starts a new LIFETIME and returning a
    // pointer based on the LIFETIME of M_VALUE is UB [1][2].
    //
    // Notes:
    // * Obtaining a pointer to an object created by placement new from a
    //   pointer to an object providing storage for that object [1].
    // * See example in [1] and [2].
    //
    // [1]: https://en.cppreference.com/w/cpp/utility/launder
    // [2]: https://en.cppreference.com/w/cpp/types/aligned_storage
    return *__builtin_launder(reinterpret_cast<T*>(m_value));
  }

  constexpr const T& value() const& {
    assert(m_has_value);
    // Launder, see other value().
    return *__builtin_launder(reinterpret_cast<const T*>(m_value));
  }

  constexpr T value() && {
    return take();
  }

  constexpr T take() {
    assert(m_has_value);
    T val = std::move(value());
    reset();
    return val;
  }

  // -- INTERNAL ---------------------------------------------------------------

 private:
  template <typename U = T,
            impl::enable_if_t<!impl::is_trivially_destructible_v<U>> = true>
  constexpr void reset() {
    if (m_has_value) {
      value().~T();
      m_has_value = false;
    }
  }

  template <typename U = T,
            impl::enable_if_t<impl::is_trivially_destructible_v<U>> = true>
  constexpr void reset() {
    m_has_value = false;
  }

  alignas(T) char m_value[sizeof(T)];
  bool m_has_value{false};
};
}  // namespace option

#endif