expected.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
  3. #pragma once
  4. #include "qtcassert.h"
  5. #include "3rdparty/tl_expected/include/tl/expected.hpp"
  6. namespace Utils {
  7. using namespace tl;
  8. template<class T>
  9. using expected_str = tl::expected<T, QString>;
  10. } // namespace Utils
  11. //! If 'expected' has an error the error will be printed and the 'action' will be executed.
  12. #define QTC_ASSERT_EXPECTED(expected, action) \
  13. if (Q_LIKELY(expected)) { \
  14. } else { \
  15. ::Utils::writeAssertLocation(QString("%1:%2: %3") \
  16. .arg(__FILE__) \
  17. .arg(__LINE__) \
  18. .arg(expected.error()) \
  19. .toUtf8() \
  20. .data()); \
  21. action; \
  22. } \
  23. do { \
  24. } while (0)
  25. #define QTC_CHECK_EXPECTED(expected) \
  26. if (Q_LIKELY(expected)) { \
  27. } else { \
  28. ::Utils::writeAssertLocation( \
  29. QString("%1:%2: %3").arg(__FILE__).arg(__LINE__).arg(expected.error()).toUtf8().data()); \
  30. } \
  31. do { \
  32. } while (0)