Utils: Add std::expected implementation

Adds a std::expected implementation that is compatible with >= C++11.

FilePath::fileContents and FilePath::writeFileContents as well as
FilePath::copyFile are changed to return std::expected.

A couple of macros have been added to aid in using the expected types.

An auto test was added showing how to use the library.

Change-Id: Ibe3aecfc1029a0cf13b45bf5184ff03a04a2393b
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marcus Tillmanns
2022-11-24 08:52:47 +01:00
parent 13c7283c0e
commit eca7044361
35 changed files with 2986 additions and 265 deletions

47
src/libs/utils/expected.h Normal file
View File

@@ -0,0 +1,47 @@
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#pragma once
#include "qtcassert.h"
#include "../3rdparty/tl_expected/include/tl/expected.hpp"
namespace Utils {
template<class T, class E>
using expected = tl::expected<T, E>;
template<class T>
using expected_str = tl::expected<T, QString>;
template<class E>
using unexpected = tl::unexpected<E>;
using unexpect_t = tl::unexpect_t;
static constexpr unexpect_t unexpect{};
template<class E>
constexpr unexpected<std::decay_t<E>> make_unexpected(E &&e)
{
return tl::make_unexpected(e);
}
} // namespace Utils
//! If 'expected' has an error the error will be printed and the 'action' will be executed.
#define QTC_ASSERT_EXPECTED(expected, action) \
{ \
if (Q_LIKELY(expected)) { \
} else { \
::Utils::writeAssertLocation(QString("%1:%2: %3") \
.arg(__FILE__) \
.arg(__LINE__) \
.arg(expected.error()) \
.toUtf8() \
.data()); \
action; \
} \
do { \
} while (0); \
}