forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.hh
62 lines (48 loc) · 2.09 KB
/
result.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright 2022-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
// Basic utilities which allow to start working with boost::outcome::result
// in conjunction with our exception_container.
#include <boost/outcome/bad_access.hpp>
#include <boost/outcome/basic_result.hpp>
#include <boost/outcome/config.hpp>
#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>;
template<typename L, typename R>
concept ResultRebindableTo =
bo::is_basic_result<L>::value &&
bo::is_basic_result<R>::value &&
std::same_as<typename L::error_type, typename R::error_type> &&
std::same_as<typename L::no_value_policy_type, typename R::no_value_policy_type>;
// Creates a result type which has the same error type as R, but has a different value type.
// The name was inspired by std::allocator::rebind.
template<typename T, ExceptionContainerResult R>
using rebind_result = bo::result<T, typename R::error_type, exception_container_throw_policy>;
}