Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmaybe_p.h
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMAYBE_P_H
5#define QMAYBE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include <qstring.h>
20#include <optional>
21#include <utility>
22#include <memory>
23
24QT_BEGIN_NAMESPACE
25
26struct QUnexpect
27{
28};
29
30static constexpr QUnexpect unexpect{};
31
32template <typename Value, typename Error = QString>
33class QMaybe
34{
35public:
36 QMaybe(const Value &v)
37 {
38 if constexpr (std::is_pointer_v<Value>) {
39 if (!v)
40 return; // nullptr is treated as nullopt (for raw pointer types only)
41 }
42 m_value = v;
43 }
44
45 QMaybe(Value &&v)
46 {
47 if constexpr (std::is_pointer_v<Value>) {
48 if (!v)
49 return; // nullptr is treated as nullopt (for raw pointer types only)
50 }
51 m_value = std::move(v);
52 }
53
54 QMaybe(const QMaybe &other) = default;
55
56 QMaybe &operator=(const QMaybe &other) = default;
57
58 template <class... Args>
59 QMaybe(QUnexpect, Args &&...args) : m_error{ std::forward<Args>(args)... }
60 {
61 static_assert(std::is_constructible_v<Error, Args &&...>,
62 "Invalid arguments for creating an error type");
63 }
64
65 // NOTE: Returns false if holding a nullptr value, even if no error is set.
66 // This is different from std::expected, where a nullptr is a valid value, not
67 // an error.
68 constexpr explicit operator bool() const noexcept { return m_value.has_value(); }
69
70 constexpr Value &value()
71 {
72 Q_ASSERT(m_value.has_value());
73 return *m_value;
74 }
75
76 constexpr const Value &value() const
77 {
78 Q_ASSERT(m_value.has_value());
79 return *m_value;
80 }
81
82 constexpr Value *operator->() noexcept { return std::addressof(value()); }
83 constexpr const Value *operator->() const noexcept { return std::addressof(value()); }
84
85 constexpr Value &operator*() &noexcept { return value(); }
86 constexpr const Value &operator*() const &noexcept { return value(); }
87
88 constexpr const Error &error() const { return m_error; }
89
90private:
91 std::optional<Value> m_value;
92 Error m_error{};
93};
94
95QT_END_NAMESPACE
96
97#endif // QMAYBE_P_H
QMaybe(QUnexpect, Args &&...args)
Definition qmaybe_p.h:59
constexpr Value * operator->() noexcept
Definition qmaybe_p.h:82
QMaybe(Value &&v)
Definition qmaybe_p.h:45
constexpr Value & value()
Definition qmaybe_p.h:70
QMaybe & operator=(const QMaybe &other)=default
constexpr const Error & error() const
Definition qmaybe_p.h:88
QMaybe(const QMaybe &other)=default
constexpr Value & operator*() &noexcept
Definition qmaybe_p.h:85
constexpr const Value & value() const
Definition qmaybe_p.h:76
QMaybe(const Value &v)
Definition qmaybe_p.h:36
constexpr const Value * operator->() const noexcept
Definition qmaybe_p.h:83
constexpr operator bool() const noexcept
Definition qmaybe_p.h:68
constexpr const Value & operator*() const &noexcept
Definition qmaybe_p.h:86
static constexpr QUnexpect unexpect
Definition qmaybe_p.h:30