Files
scylladb/utils/result.hh
Piotr Dulikowski 6abeec6299 utils/result: split into combinators and loop file
Segregates result utilities into:

- result.hh - basic definitions related to results with exception
  containers,
- result_combinators.hh - combinators for working with results in
  conjunction with futures,
- result_loop.hh - loop-like combinators, currently has only
  result_parallel_for_each.

The motivation for the split is:

1. In headers, usually only result.hh will be needed, so no need to
   force most .cc files to compile definitions from other files,
2. Less files need to be recompiled when a combinator is added to
   result_combinators or result_loop.

As a bonus, `result_with_exception` was moved from `utils::internal` to
just `utils`.
2022-02-10 18:19:05 +01:00

48 lines
1.4 KiB
C++

/*
* Copyright 2022-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
// Basic utilities which allow to start working with boost::outcome::result
// in conjunction with our exception_container.
#include <boost/outcome/policy/base.hpp>
#include <boost/outcome/result.hpp>
#include "utils/exception_container.hh"
namespace bo = BOOST_OUTCOME_V2_NAMESPACE;
namespace utils {
// A policy which throws the container_error associated with the result
// if there was an attempt to access value while it was not present.
struct exception_container_throw_policy : bo::policy::base {
template<class Impl> static constexpr void wide_value_check(Impl&& self) {
if (!base::_has_value(self)) {
base::_error(self).throw_me();
}
}
template<class Impl> static constexpr void wide_error_check(Impl&& self) {
if (!base::_has_error(self)) {
throw bo::bad_result_access("no error");
}
}
};
template<typename T, typename... Exs>
using result_with_exception = bo::result<T, exception_container<Exs...>, exception_container_throw_policy>;
template<typename R>
concept ExceptionContainerResult = bo::is_basic_result<R>::value && ExceptionContainer<typename R::error_type>;
template<typename F>
concept ExceptionContainerResultFuture = seastar::is_future<F>::value && ExceptionContainerResult<typename F::value_type>;
}