aboutsummaryrefslogtreecommitdiff
path: root/source/lib/include/wanda/expected.hpp
blob: fff0d815636b78e5d6f50a83b0389ed39025f403 (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
/**
 * @file   expected.hpp
 * @author Felix Morgner (felix.morgner@gmail.com)
 * @since  1.0.0
 */

#ifndef WANDA_EXPECTED_HPP
#define WANDA_EXPECTED_HPP

#include <initializer_list>
#include <type_traits>
#include <utility>

namespace wanda
{
  /**
    * @brief A type to represent the error case of a computation based on #wanda::expected
    */
  template<typename ErrorType>
  struct unexpected
  {
    static_assert(!std::is_same_v<ErrorType, void>, "ErrorType can not be 'void'!");
    static_assert(!std::is_array_v<ErrorType>, "ErrorType can not be an array type!");

    /**
     * @brief Copy construct a new @p unexpected from another @p unexpected
     */
    constexpr unexpected(unexpected const &) = default;

    /**
     * @brief Move construct a new @p unexpected from another @p unexpected
     */
    constexpr unexpected(unexpected &&) = default;

    /**
     * @brief Construct a new @p unexpected by direct initializing the error object from @p args
     */
    template<typename... Args>
    constexpr explicit unexpected(std::in_place_t, Args &&... args)
        : m_error(std::forward<Args>(args)...)
    {
    }

    /**
     * @brief Construct a new @p unexpected by direct initializing the error object from @p il and @p args
     */
    template<
        typename U,
        typename... Args,
        std::enable_if_t<std::is_constructible_v<ErrorType, std::initializer_list<U>, Args...>> * = nullptr>
    constexpr explicit unexpected(std::in_place_t, std::initializer_list<U> il, Args &&... args)
        : m_error(il, std::forward<Args>(args)...)
    {
    }

    /**
     * @brief Construct a new @p unexpected by direct initializing the error object from @p error
     */
    template<
        typename Err = ErrorType,
        std::enable_if_t<std::is_constructible_v<ErrorType, Err> &&
                         !std::is_same_v<std::remove_cv_t<std::remove_reference_t<Err>>, std::in_place_t> &&
                         !std::is_same_v<std::remove_cv_t<std::remove_reference_t<Err>>, unexpected>> * = nullptr>
    constexpr explicit unexpected(Err && error)
        : m_error(std::forward<Err>(error))
    {
    }

    /**
     * @brief Construct a new @p unexpected by copying the value of another @p unexpected of different error type
     */
    template<
        typename Err,
        std::enable_if_t<!(
            std::is_constructible_v<ErrorType, Err> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err>> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const> &&
            !std::is_convertible_v<unexpected<Err> &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err>, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const, ErrorType>)> * = nullptr,
        std::enable_if_t<!std::is_convertible_v<Err, ErrorType>> * = nullptr>
    constexpr explicit unexpected(unexpected<Err> const & error)
        : m_error(error.m_error)
    {
    }

    /**
     * @brief Construct a new @p unexpected by copying the value of another @p unexpected of different error type
     */
    template<
        typename Err,
        std::enable_if_t<!(
            std::is_constructible_v<ErrorType, Err> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err>> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const> &&
            !std::is_convertible_v<unexpected<Err> &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err>, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const, ErrorType>)> * = nullptr,
        std::enable_if_t<std::is_convertible_v<Err, ErrorType>> * = nullptr>
    constexpr unexpected(unexpected<Err> const & error)
        : m_error(error.m_error)
    {
    }

    /**
     * @brief Construct a new @p unexpected by moving the value of another @p unexpected of different error type
     */
    template<
        typename Err,
        std::enable_if_t<!(
            std::is_constructible_v<ErrorType, Err> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err>> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const> &&
            !std::is_convertible_v<unexpected<Err> &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err>, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const, ErrorType>)> * = nullptr,
        std::enable_if_t<!std::is_convertible_v<Err, ErrorType>> * = nullptr>
    constexpr explicit unexpected(unexpected<Err> && error)
        : m_error(std::move(error.m_error))
    {
    }

    /**
     * @brief Construct a new @p unexpected by moving the value of another @p unexpected of different error type
     */
    template<
        typename Err,
        std::enable_if_t<!(
            std::is_constructible_v<ErrorType, Err> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err>> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const &> &&
            !std::is_constructible_v<ErrorType, unexpected<Err> const> &&
            !std::is_convertible_v<unexpected<Err> &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err>, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const &, ErrorType> &&
            !std::is_convertible_v<unexpected<Err> const, ErrorType>)> * = nullptr,
        std::enable_if_t<std::is_convertible_v<Err, ErrorType>> * = nullptr>
    constexpr unexpected(unexpected<Err> && error)
        : m_error(std::move(error.m_error))
    {
    }

    /**
     * @brief Get the error value contained in this @p unexpected instance
     */
    constexpr ErrorType const & value() const &
    {
      return m_error;
    }

    /**
     * @brief Get the error value contained in this @p unexpected instance
     */
    constexpr ErrorType & value() &
    {
      return m_error;
    }

    /**
     * @brief Get the error value contained in this @p unexpected instance
     */
    constexpr ErrorType && value() &&
    {
      return std::move(m_error);
    }

    /**
     * @brief Get the error value contained in this @p unexpected instance
     */
    constexpr ErrorType const && value() const &&
    {
      return std::move(m_error);
    }

    /**
     * @brief Swap the error value of this @p unexpected instance with the one of @p other
     */
    void swap(unexpected & other) noexcept(std::is_nothrow_swappable_v<ErrorType>)
    {
      using std::swap;
      swap(m_error, other.m_error);
    }

    template<typename ErrorType1, typename ErrorType2>
    friend constexpr bool operator==(unexpected<ErrorType1> const & lhs, unexpected<ErrorType2> const & rhs);

    template<typename ErrorType1, typename ErrorType2>
    friend constexpr bool operator!=(unexpected<ErrorType1> const & lhs, unexpected<ErrorType2> const & rhs);

    template<
        typename Err,
        std::enable_if_t<std::is_swappable_v<Err>> *>
    friend void swap(unexpected<Err> & lhs, unexpected<Err> & rhs);

  private:
    ErrorType m_error;
  };

  template<typename ErrorType>
  unexpected(ErrorType)->unexpected<ErrorType>;

  /**
 * @brief Compare two @p unexpected instances for equality
 */
  template<typename ErrorType1, typename ErrorType2>
  constexpr bool operator==(unexpected<ErrorType1> const & lhs, unexpected<ErrorType2> const & rhs)
  {
    return lhs.m_error == rhs.m_error;
  }

  /**
 * @brief Compare two @p unexpected instances for inequality
 */
  template<typename ErrorType1, typename ErrorType2>
  constexpr bool operator!=(unexpected<ErrorType1> const & lhs, unexpected<ErrorType2> const & rhs)
  {
    return lhs.m_error != rhs.m_error;
  }

  /**
 * @brief Swap the error values of two @p unexpected instances
 */
  template<
      typename Err,
      std::enable_if_t<std::is_swappable_v<Err>> * = nullptr>
  void swap(unexpected<Err> & lhs, unexpected<Err> & rhs)
  {
    lhs.swap(rhs);
  }

  /**
 * @brief A tag type for @p unexpected
 */
  struct unexpect_t
  {
    explicit unexpect_t() = default;
  };

  /**
 * @brief A tap for @p unexpected
 */
  inline constexpr unexpect_t unexpect{};

}  // namespace wanda

#endif