summaryrefslogtreecommitdiffstats
path: root/framework/src/errors.h
blob: a0b27084cdf24093c52fa898f2a468f90d2a41bc (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
/*
    Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/
#pragma once

#include <memory>
#include <type_traits>
#include <utility>

#include <QtGlobal>

// A somewhat implementation of the expected monad, proposed here:
// https://isocpp.org/files/papers/n4015.pdf

// A class used to differentiate errors and values when they are of the same type.
template <typename Error>
class Unexpected
{

    static_assert(!std::is_same<Error, void>::value, "Cannot have an Unexpected void");

public:
    Unexpected() = delete;

    constexpr explicit Unexpected(const Error &error) : mValue(error) {}
    constexpr explicit Unexpected(Error &&error) : mValue(std::move(error)) {}

    // For implicit conversions when doing makeUnexpected(other)
    template <typename Other>
    constexpr explicit Unexpected(const Unexpected<Other> &error) : mValue(error.value())
    {
    }
    template <typename Other>
    constexpr explicit Unexpected(Unexpected<Other> &&error) : mValue(std::move(error.value()))
    {
    }

    constexpr const Error &value() const &
    {
        return mValue;
    }
    Error &value() &
    {
        return mValue;
    }

    constexpr const Error &&value() const &&
    {
        return std::move(mValue);
    }
    Error &&value() &&
    {
        return std::move(mValue);
    }

private:
    Error mValue;
};

template <class Error>
Unexpected<typename std::decay<Error>::type> makeUnexpected(Error &&e)
{
    return Unexpected<typename std::decay<Error>::type>(std::forward<Error>(e));
}

template <typename Error>
bool operator==(const Unexpected<Error> &lhs, const Unexpected<Error> &rhs)
{
    return lhs.value() == rhs.value();
}

template <typename Error>
bool operator!=(const Unexpected<Error> &lhs, const Unexpected<Error> &rhs)
{
    return lhs.value() != rhs.value();
}

namespace detail {

namespace tags {
struct Expected
{};
struct Unexpected
{};
} // namespace tags

// Write functions here when storage related and when Type != void
template <typename Error, typename Type>
struct StorageBase
{
protected:

    // To be able to define a copy constructor in a child class
    StorageBase() {}

    // Rule of 5 (copy constructors defined in StorageCopyConstructor) {{{

    StorageBase(StorageBase &&other) : mIsValue(other.mIsValue)
    {
        // This is a constructor, you have to construct object, not assign them
        // (hence the placement new)
        //
        // Here's the problem:
        //
        // Object that are part of a union are not initialized (which is
        // normal). If we replaced the placement new by a line like this:
        //
        // ```
        // mValue = other.mValue;
        // ```
        //
        // If overloaded, this will call `mValue.operator=(other.mValue);`, but
        // since we're in the constructor, mValue is not initialized. This can
        // cause big issues if `Type` / `Error` is not trivially (move)
        // assignable.
        //
        // And so, the placement new allows us to call the constructor of
        // `Type` or `Error` instead of its assignment operator.
        if (mIsValue) {
            new (std::addressof(mValue)) Type(std::move(other.mValue));
        } else {
            new (std::addressof(mError)) Unexpected<Error>(std::move(other.mError));
        }
    }

    constexpr StorageBase &operator=(StorageBase &&other)
    {
        this->~StorageBase();
        mIsValue = other.mIsValue;
        if (mIsValue) {
            mValue = std::move(other.mValue);
        } else {
            mError = std::move(other.mError);
        }
        return *this;
    }

    ~StorageBase()
    {
        if (mIsValue) {
            mValue.~Type();
        } else {
            mError.~Unexpected<Error>();
        }
    }

    // }}}

    template <typename... Args>
    constexpr StorageBase(tags::Expected, Args &&... args)
        : mValue(std::forward<Args>(args)...), mIsValue(true)
    {
    }

    template <typename... Args>
    constexpr StorageBase(tags::Unexpected, Args &&... args)
        : mError(std::forward<Args>(args)...), mIsValue(false)
    {
    }

    union
    {
        Unexpected<Error> mError;
        Type mValue;
    };
    bool mIsValue;
};

// Write functions here when storage related and when Type == void
template <typename Error>
struct StorageBase<Error, void>
{
protected:
    constexpr StorageBase(tags::Expected) : mIsValue(true) {}

    template <typename... Args>
    constexpr StorageBase(tags::Unexpected, Args &&... args)
        : mError(std::forward<Args>(args)...), mIsValue(false)
    {
    }

    Unexpected<Error> mError;
    bool mIsValue;
};

// Struct used to add the copy constructor / assignment only if both types are copy constructible
template <typename Error, typename Type,
    bool both_copy_constructible = std::is_copy_constructible<Error>::value &&std::is_copy_constructible<Type>::value>
struct StorageCopyConstructor;

template <typename Error, typename Type>
struct StorageCopyConstructor<Error, Type, true> : StorageBase<Error, Type>
{
protected:
    using StorageBase<Error, Type>::StorageBase;

    StorageCopyConstructor(const StorageCopyConstructor &other) : StorageBase<Error, Type>()
    {
        // If you're thinking WTF, see the comment in the move constructor above.
        this->mIsValue = other.mIsValue;
        if (this->mIsValue) {
            new (std::addressof(this->mValue)) Type(other.mValue);
        } else {
            new (std::addressof(this->mError)) Unexpected<Error>(other.mError);
        }
    }

    StorageCopyConstructor &operator=(const StorageCopyConstructor &other)
    {
        this->mIsValue = other.mIsValue;
        if (this->mIsValue) {
            this->mValue = other.mValue;
        } else {
            this->mError = other.mError;
        }
        return *this;
    }

};

template <typename Error, typename Type>
struct StorageCopyConstructor<Error, Type, false> : StorageBase<Error, Type>
{
protected:
    using StorageBase<Error, Type>::StorageBase;
};


// Write functions here when storage related, whether Type is void or not
template <typename Error, typename Type>
struct Storage : StorageCopyConstructor<Error, Type>
{
protected:
    // Forward the construction to StorageBase
    using StorageCopyConstructor<Error, Type>::StorageCopyConstructor;
};

// Write functions here when dev API related and when Type != void
template <typename Error, typename Type>
struct ExpectedBase : detail::Storage<Error, Type>
{
    constexpr ExpectedBase() : detail::Storage<Error, Type>(detail::tags::Expected{}) {}

    template <typename OtherError>
    constexpr ExpectedBase(const Unexpected<OtherError> &error)
        : detail::Storage<Error, Type>(detail::tags::Unexpected{}, error)
    {
    }
    template <typename OtherError>
    constexpr ExpectedBase(Unexpected<OtherError> &&error)
        : detail::Storage<Error, Type>(detail::tags::Unexpected{}, std::move(error))
    {
    }

    constexpr ExpectedBase(const Type &value)
        : detail::Storage<Error, Type>(detail::tags::Expected{}, value)
    {
    }
    constexpr ExpectedBase(Type &&value)
        : detail::Storage<Error, Type>(detail::tags::Expected{}, std::move(value))
    {
    }

    // Warning: will crash if this is an error. You should always check this is
    // an expected value before calling `.value()`
    constexpr const Type &value() const &
    {
        Q_ASSERT(this->mIsValue);
        return this->mValue;
    }
    Type &&value() &&
    {
        Q_ASSERT(this->mIsValue);
        return std::move(this->mValue);
    }
};

// Write functions here when dev API related and when Type == void
template <typename Error>
struct ExpectedBase<Error, void> : Storage<Error, void>
{
    // Rewrite constructors for unexpected because Expected doesn't have direct access to it.
    template <typename OtherError>
    constexpr ExpectedBase(const Unexpected<OtherError> &error)
        : Storage<Error, void>(tags::Unexpected{}, error)
    {
    }
    template <typename OtherError>
    constexpr ExpectedBase(Unexpected<OtherError> &&error)
        : Storage<Error, void>(tags::Unexpected{}, std::move(error))
    {
    }
};

} // namespace detail

// Write functions here when dev API related, whether Type is void or not
template <typename Error, typename Type = void>
class Expected : public detail::ExpectedBase<Error, Type>
{
    static_assert(!std::is_same<Error, void>::value, "Expected with void Error is not implemented");

public:
    using detail::ExpectedBase<Error, Type>::ExpectedBase;

    constexpr const Error &error() const &
    {
        return this->mError.value();
    }

    constexpr bool isValue() const
    {
        return this->mIsValue;
    }
    constexpr explicit operator bool() const
    {
        return this->mIsValue;
    }
};