From: Joel Rosdahl Date: Thu, 8 Sep 2022 11:26:25 +0000 (+0200) Subject: enhance: Add prefix parameter to util::value_or_throw X-Git-Tag: v4.7~65 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9bdf5410660f3be84018642dd60be30dba1816c1;p=thirdparty%2Fccache.git enhance: Add prefix parameter to util::value_or_throw --- diff --git a/src/util/expected.hpp b/src/util/expected.hpp index 95926f92f..e0320da8b 100644 --- a/src/util/expected.hpp +++ b/src/util/expected.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Joel Rosdahl and other contributors +// Copyright (C) 2021-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,6 +18,9 @@ #pragma once +#include + +#include #include namespace util { @@ -31,6 +34,12 @@ typename T::value_type value_or_throw(const T& value); template typename T::value_type value_or_throw(T&& value); +// As above for with `prefix` added to the error message. +template +typename T::value_type value_or_throw(const T& value, std::string_view prefix); +template +typename T::value_type value_or_throw(T&& value, std::string_view prefix); + #define TRY(x_) \ do { \ const auto result = x_; \ @@ -63,4 +72,26 @@ value_or_throw(T&& value) } } +template +inline typename T::value_type +value_or_throw(const T& value, std::string_view prefix) +{ + if (value) { + return *value; + } else { + throw E(FMT("{}{}", prefix, value.error())); + } +} + +template +inline typename T::value_type +value_or_throw(T&& value, std::string_view prefix) +{ + if (value) { + return std::move(*value); + } else { + throw E(FMT("{}{}", prefix, value.error())); + } +} + } // namespace util diff --git a/unittest/test_util_expected.cpp b/unittest/test_util_expected.cpp index 74f852703..723f92efd 100644 --- a/unittest/test_util_expected.cpp +++ b/unittest/test_util_expected.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Joel Rosdahl and other contributors +// Copyright (C) 2021-2022 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -50,6 +50,37 @@ TEST_CASE("util::value_or_throw") const std::string value = "value"; nonstd::expected, const char*> with_value = std::make_unique(value); + const nonstd::expected without_value = + nonstd::make_unexpected("no value"); + CHECK(*value_or_throw(std::move(with_value)) == value); + CHECK_THROWS_WITH(value_or_throw(std::move(without_value)), + "no value"); + } + + SUBCASE("const ref with prefix") + { + const nonstd::expected with_value = 42; + const nonstd::expected without_value = + nonstd::make_unexpected("no value"); + + CHECK(value_or_throw(with_value, "prefix: ") == 42); + CHECK_THROWS_WITH(value_or_throw(without_value, "prefix: "), + "prefix: no value"); + } + + SUBCASE("move with prefix") + { + const std::string value = "value"; + nonstd::expected, const char*> with_value = + std::make_unique(value); + const nonstd::expected without_value = + nonstd::make_unexpected("no value"); + + CHECK(*value_or_throw(std::move(with_value), "prefix: ") + == value); + CHECK_THROWS_WITH( + value_or_throw(std::move(without_value), "prefix: "), + "prefix: no value"); } }