diff --git a/.gitignore b/.gitignore index 3e76d235..ab40f8c1 100644 --- a/.gitignore +++ b/.gitignore @@ -122,3 +122,4 @@ mu-*-coverage mu*tar.xz compile_commands.json /lib/utils/test-sexp +/lib/utils/test-option diff --git a/lib/utils/Makefile.am b/lib/utils/Makefile.am index d8cc0d5c..13aa93b9 100644 --- a/lib/utils/Makefile.am +++ b/lib/utils/Makefile.am @@ -116,6 +116,14 @@ test_command_parser_SOURCES= \ test_command_parser_LDADD= \ libmu-utils.la +TEST_PROGS+= \ + test-option +test_option_SOURCES= \ + test-option.cc +test_option_LDADD= \ + libmu-utils.la + + TESTS=$(TEST_PROGS) include $(top_srcdir)/aminclude_static.am diff --git a/lib/utils/mu-option.hh b/lib/utils/mu-option.hh index 06bb3a3e..cc042e8d 100644 --- a/lib/utils/mu-option.hh +++ b/lib/utils/mu-option.hh @@ -19,8 +19,11 @@ namespace Mu { -/// Either a value of type T, or nothing. +/// Either a value of type T, or None template using Option=tl::optional; +template Option Some(T&& t) { return t; } +constexpr auto Nothing = tl::nullopt; // 'None' is take already + } #endif /*MU_OPTION__*/ diff --git a/lib/utils/mu-result.hh b/lib/utils/mu-result.hh index b871353f..3b1e1538 100644 --- a/lib/utils/mu-result.hh +++ b/lib/utils/mu-result.hh @@ -29,10 +29,19 @@ namespace Mu { /** * A Result is _either_ some value of type T, _or_ an error. - * */ template using Result = tl::expected; +template typename Result::expected_type +Ok(T&& t) { + return Result::expected(std::move(t)); +} + +template typename Result::unexpected_type +Err(Error&& err) { + return Result::unexpected(std::move(err)); +} + } // namespace Mu diff --git a/lib/utils/test-option.cc b/lib/utils/test-option.cc new file mode 100644 index 00000000..1826aef3 --- /dev/null +++ b/lib/utils/test-option.cc @@ -0,0 +1,59 @@ +/* +** Copyright (C) 2020 Dirk-Jan C. Binnema +** +** This library is free software; you can redistribute it and/or +** modify it under the terms of the GNU Lesser General Public License +** as published by the Free Software Foundation; either version 2.1 +** of the License, or (at your option) any later version. +** +** This library is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** Lesser General Public License for more details. +** +** You should have received a copy of the GNU Lesser General Public +** License along with this library; if not, write to the Free +** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA +** 02110-1301, USA. +*/ + +#include "mu-utils.hh" +#include "mu-option.hh" + +using namespace Mu; + +static Option +get_opt_int (bool b) +{ + if (b) + return Some(123); + else + return Nothing; +} + +static void +test_option() +{ + { + const auto oi{get_opt_int(true)}; + g_assert_true(!!oi); + g_assert_cmpint(oi.value(),==,123); + } + + { + const auto oi{get_opt_int(false)}; + g_assert_false(!!oi); + g_assert_false(oi.has_value()); + g_assert_cmpint(oi.value_or(456),==,456); + } +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/option/option", test_option); + + return g_test_run (); +}