1 | // Copyright (C) 2021 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 QMULTIMEDIAUTILS_P_H |
5 | #define QMULTIMEDIAUTILS_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 <QtMultimedia/qtmultimediaglobal.h> |
19 | #include <QtCore/private/qglobal_p.h> |
20 | #include <qstring.h> |
21 | #include <utility> |
22 | #include <optional> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | struct QUnexpect |
27 | { |
28 | }; |
29 | |
30 | static constexpr QUnexpect unexpect{}; |
31 | |
32 | template<typename Value, typename Error = QString> |
33 | class QMaybe |
34 | { |
35 | public: |
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 Error& error) : m_error(error) { } |
55 | |
56 | template<class... Args> |
57 | QMaybe(QUnexpect, Args &&...args) : m_error{ std::forward<Args>(args)... } |
58 | { |
59 | static_assert(std::is_constructible_v<Error, Args &&...>, |
60 | "Invalid arguments for creating an error type" ); |
61 | } |
62 | |
63 | constexpr explicit operator bool() const noexcept { return m_value.has_value(); } |
64 | |
65 | constexpr Value &value() |
66 | { |
67 | Q_ASSERT(m_value.has_value()); |
68 | return *m_value; |
69 | } |
70 | |
71 | constexpr const Value &value() const |
72 | { |
73 | Q_ASSERT(m_value.has_value()); |
74 | return *m_value; |
75 | } |
76 | |
77 | constexpr Value *operator->() noexcept { return &value(); } |
78 | constexpr const Value *operator->() const noexcept { return &value(); } |
79 | |
80 | constexpr Value &operator*() & noexcept { return value(); } |
81 | constexpr const Value &operator*() const & noexcept { return value(); } |
82 | |
83 | constexpr const Error &error() const { return m_error; } |
84 | |
85 | private: |
86 | std::optional<Value> m_value; |
87 | const Error m_error; |
88 | }; |
89 | |
90 | struct Fraction { |
91 | int numerator; |
92 | int denominator; |
93 | }; |
94 | |
95 | Q_MULTIMEDIA_EXPORT Fraction qRealToFraction(qreal value); |
96 | |
97 | QT_END_NAMESPACE |
98 | |
99 | #endif // QMULTIMEDIAUTILS_P_H |
100 | |
101 | |