aboutsummaryrefslogtreecommitdiff
path: root/libbutl/optional.mxx
blob: d32e14bf387292873499d75d81c25d8714d92d67 (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
// file      : libbutl/optional.mxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#pragma once
#endif

// C includes.

// Note: the Clang check must come before GCC since it also defines __GNUC__.
//
#if   defined(ODB_COMPILER)
   //
   // Make sure we use butl::optional during ODB compilation (has to be this
   // way until we completely switch to std::optional since we use the same
   // generated code for all compilers).
   //
#elif defined(_MSC_VER)
   //
   // Available from 19.10 (15.0). Except it (or the compiler) doesn't seem to
   // be constexpr-correct. Things appear to be fixed in 19.20 (16.0) but
   // optional is now only available in the C++17 mode or later.
   //
#  if _MSC_VER >= 1920
#    if defined(_MSVC_LANG) && _MSVC_LANG >= 201703L // See /Zc:__cplusplus
#      define LIBBUTL_STD_OPTIONAL
#    endif
#  endif
#elif defined(__clang__)
   //
   // Clang's libc++ has it since 4 but we might also be using libstdc++. For
   // the latter we will check for the presence of the <optional> header which
   // only appeared in GCC 7. Also assume both are only available in C++17.
   //
   // Note that on Mac OS it can still be <experimental/optional>.
   //
#  if __cplusplus >= 201703L
#    if __has_include(<__config>)
#      include <__config>          // _LIBCPP_VERSION
#      if _LIBCPP_VERSION >= 4000 && __has_include(<optional>)
#        define LIBBUTL_STD_OPTIONAL
#      endif
#    elif __has_include(<optional>)
#      define LIBBUTL_STD_OPTIONAL
#    endif
#  endif
#elif defined(__GNUC__)
   //
   // Available from 7 but only in the C++17 mode. Note also that from 8
   // <optional> defines __cpp_lib_optional.
   //
#  if __GNUC__ >= 7 && __cplusplus >= 201703L
#    define LIBBUTL_STD_OPTIONAL
#  endif
#endif

#ifndef __cpp_lib_modules_ts
#ifdef LIBBUTL_STD_OPTIONAL
#  include <optional>
#else
#  include <utility>     // move()
#  include <functional>  // hash
#  include <type_traits> // is_*
#endif
#endif

// Other includes.

#ifdef __cpp_modules_ts
export module butl.optional;
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
#endif

#include <libbutl/export.hxx>

#ifdef LIBBUTL_STD_OPTIONAL
LIBBUTL_MODEXPORT namespace butl
{
  template <typename T>
  using optional = std::optional<T>;

  using std::nullopt_t;
  using std::nullopt;
}
#else

LIBBUTL_MODEXPORT namespace butl
{
  // Simple optional class template while waiting for std::optional.
  //
  struct nullopt_t {constexpr explicit nullopt_t (int) {}};
  constexpr nullopt_t nullopt (1);

  namespace details
  {
    template <typename T, bool = std::is_trivially_destructible<T>::value>
    struct optional_data;

    template <typename T>
    struct optional_data<T, false>
    {
      struct empty {};

      union
      {
        empty e_;
        T d_;
      };
      bool v_;

#if !defined(_MSC_VER) || _MSC_VER > 1900
      constexpr optional_data ():           e_ (),              v_ (false) {}
      constexpr optional_data (nullopt_t):  e_ (),              v_ (false) {}
      constexpr optional_data (const T& v): d_ (v),             v_ (true)  {}
      constexpr optional_data (T&& v):      d_ (std::move (v)), v_ (true)  {}
#else
      optional_data ():           e_ (),              v_ (false) {}
      optional_data (nullopt_t):  e_ (),              v_ (false) {}
      optional_data (const T& v): d_ (v),             v_ (true)  {}
      optional_data (T&& v):      d_ (std::move (v)), v_ (true)  {}
#endif

#if (!defined(_MSC_VER) || _MSC_VER > 1900) &&  \
    (!defined(__GNUC__) || __GNUC__ > 4 || defined(__clang__))
      constexpr optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);}
      constexpr optional_data (optional_data&& o):      v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));}
#else
      optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);}
      optional_data (optional_data&& o):      v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));}
#endif

      optional_data& operator= (nullopt_t);
      optional_data& operator= (const T&);
      optional_data& operator= (T&&);

      optional_data& operator= (const optional_data&);
      optional_data& operator= (optional_data&&);

      ~optional_data ();
    };

    template <typename T>
    struct optional_data<T, true>
    {
      struct empty {};

      union
      {
        empty e_;
        T d_;
      };
      bool v_;

#if !defined(_MSC_VER) || _MSC_VER > 1900
      constexpr optional_data ():           e_ (),              v_ (false) {}
      constexpr optional_data (nullopt_t):  e_ (),              v_ (false) {}
      constexpr optional_data (const T& v): d_ (v),             v_ (true)  {}
      constexpr optional_data (T&& v):      d_ (std::move (v)), v_ (true)  {}
#else
      optional_data ():           e_ (),              v_ (false) {}
      optional_data (nullopt_t):  e_ (),              v_ (false) {}
      optional_data (const T& v): d_ (v),             v_ (true)  {}
      optional_data (T&& v):      d_ (std::move (v)), v_ (true)  {}
#endif

#if (!defined(_MSC_VER) || _MSC_VER > 1900) && \
    (!defined(__GNUC__) || __GNUC__ > 4 || defined(__clang__))
      constexpr optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);}
      constexpr optional_data (optional_data&& o):      v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));}
#else
      optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);}
      optional_data (optional_data&& o):      v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));}
#endif

      optional_data& operator= (nullopt_t);
      optional_data& operator= (const T&);
      optional_data& operator= (T&&);

      optional_data& operator= (const optional_data&);
      optional_data& operator= (optional_data&&);
    };

    template <typename T,
              bool = std::is_copy_constructible<T>::value,
              bool = std::is_move_constructible<T>::value>
    struct optional_ctors: optional_data<T>
    {
      using optional_data<T>::optional_data;
    };

    template <typename T>
    struct optional_ctors<T, false, true>: optional_ctors<T, true, true>
    {
      using optional_ctors<T, true, true>::optional_ctors;

#if !defined(_MSC_VER) || _MSC_VER > 1900
      constexpr optional_ctors () = default;
#else
      optional_ctors () = default;
#endif

      optional_ctors (const optional_ctors&) = delete;

#if (!defined(_MSC_VER) || _MSC_VER > 1900) &&                  \
    (!defined(__GNUC__) || __GNUC__ > 4 || defined(__clang__))
      constexpr optional_ctors (optional_ctors&&) = default;
#else
      optional_ctors (optional_ctors&&) = default;
#endif

      optional_ctors& operator= (const optional_ctors&) = default;
      optional_ctors& operator= (optional_ctors&&) = default;
    };

    template <typename T>
    struct optional_ctors<T, true, false>: optional_ctors<T, true, true>
    {
      using optional_ctors<T, true, true>::optional_ctors;

#if !defined(_MSC_VER) || _MSC_VER > 1900
      constexpr optional_ctors () = default;
#else
      optional_ctors () = default;
#endif

#if (!defined(_MSC_VER) || _MSC_VER > 1900) &&                  \
    (!defined(__GNUC__) || __GNUC__ > 4 || defined(__clang__))
      constexpr optional_ctors (const optional_ctors&) = default;
#else
      optional_ctors (const optional_ctors&) = default;
#endif

      optional_ctors (optional_ctors&&) = delete;

      optional_ctors& operator= (const optional_ctors&) = default;
      optional_ctors& operator= (optional_ctors&&) = default;
    };

    template <typename T>
    struct optional_ctors<T, false, false>: optional_ctors<T, true, true>
    {
      using optional_ctors<T, true, true>::optional_ctors;

#if !defined(_MSC_VER) || _MSC_VER > 1900
      constexpr optional_ctors () = default;
#else
      optional_ctors () = default;
#endif

      optional_ctors (const optional_ctors&) = delete;
      optional_ctors (optional_ctors&&) = delete;

      optional_ctors& operator= (const optional_ctors&) = default;
      optional_ctors& operator= (optional_ctors&&) = default;
    };
  }

  template <typename T>
  class optional: private details::optional_ctors<T>
  {
    using base = details::optional_ctors<T>;

  public:
    using value_type = T;

#if !defined(_MSC_VER) || _MSC_VER > 1900
    constexpr optional ()                                 {}
    constexpr optional (nullopt_t)                        {}
    constexpr optional (const T& v): base (v)             {}
    constexpr optional (T&& v):      base (std::move (v)) {}
#else
    optional ()                                 {}
    optional (nullopt_t)                        {}
    optional (const T& v): base (v)             {}
    optional (T&& v):      base (std::move (v)) {}
#endif

#if (!defined(_MSC_VER) || _MSC_VER > 1900) &&  \
    (!defined(__GNUC__) || __GNUC__ > 4 || defined(__clang__))
    constexpr optional (const optional&) = default;
    constexpr optional (optional&&)      = default;
#else
    optional (const optional&) = default;
    optional (optional&&)      = default;
#endif

    optional& operator= (nullopt_t v) {static_cast<base&> (*this) = v;             return *this;}
    optional& operator= (const T& v)  {static_cast<base&> (*this) = v;             return *this;}
    optional& operator= (T&& v)       {static_cast<base&> (*this) = std::move (v); return *this;}

    optional& operator= (const optional&) = default;
    optional& operator= (optional&&)      = default;

    T&       value ()       {return this->d_;}
    const T& value () const {return this->d_;}

    T*       operator-> ()       {return &this->d_;}
    const T* operator-> () const {return &this->d_;}

    T&       operator* ()       {return this->d_;}
    const T& operator* () const {return this->d_;}

    bool         has_value () const {return this->v_;}
    explicit operator bool () const {return this->v_;}
  };

  template <typename T>
  inline auto
  operator== (const optional<T>& x, const optional<T>& y)
  {
    bool px (x), py (y);
    return px == py && (!px || *x == *y);
  }

  template <typename T>
  inline auto
  operator!= (const optional<T>& x, const optional<T>& y)
  {
    return !(x == y);
  }

  template <typename T>
  inline auto
  operator< (const optional<T>& x, const optional<T>& 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)
  {
    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);
    }
  };
}

#include <libbutl/optional.ixx>

#endif // !LIBBUTL_STD_OPTIONAL