blob: 16089a3395cb57d2d1396e5e6145f80dde7d6538 (
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
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include "utils_global.h"
#include "expected.h"
#include "qtcassert.h"
#include <QString>
namespace Utils {
template<typename T = void>
using Result = Utils::expected<T, QString>;
QTCREATOR_UTILS_EXPORT extern const Result<> ResultOk;
QTCREATOR_UTILS_EXPORT Result<> makeResult(bool ok, const QString &errorMessage);
enum ResultSpecialErrorCode {
ResultAssert,
ResultUnimplemented,
};
class QTCREATOR_UTILS_EXPORT ResultError
{
public:
ResultError(const QString &errorMessage);
ResultError(ResultSpecialErrorCode code, const QString &errorMessage = {});
template<typename T> operator Result<T>() { return tl::make_unexpected(m_error); }
private:
QString m_error;
};
#define QTC_ASSERT_AND_ERROR_OUT(cond) \
QTC_ASSERT(cond, \
return Result::Error(QString("The condition %1 failed unexpectedly in %2:%3") \
.arg(#cond).arg(__FILE__).arg(__LINE__)))
} // namespace Utils
//! If \a result has an error the error will be printed and the \a action will be executed.
#define QTC_ASSERT_RESULT(result, action) \
if (Q_LIKELY(result)) { \
} else { \
::Utils::writeAssertLocation(QString("%1:%2: %3") \
.arg(__FILE__) \
.arg(__LINE__) \
.arg(result.error()) \
.toUtf8() \
.data()); \
action; \
} \
do { \
} while (0)
#define QTC_CHECK_RESULT(result) \
if (Q_LIKELY(result)) { \
} else { \
::Utils::writeAssertLocation( \
QString("%1:%2: %3").arg(__FILE__).arg(__LINE__).arg(result.error()).toUtf8().data()); \
} \
do { \
} while (0)
#define QVERIFY_RESULT(result) \
QVERIFY2(result, result ? #result : static_cast<const char*>(result.error().toUtf8()))
|