ipfs-chromium
Loading...
Searching...
No Matches
expected.h
1#ifndef IPFS_EXPECTED_H_
2#define IPFS_EXPECTED_H_
3
4// std::expected isn't available until C++23 and we need to support C++17
5// boost::outcome isn't available inside the Chromium tree
6// absl::StatusOr doesn't allow templating or extending the error type, and
7// translating the specific error codes into generic ones isn't great.
8
9#if __has_include("base/types/expected.h")
10#include "base/types/expected.h"
11namespace ipfs {
12template <class Value, class Error>
13using expected = base::expected<Value, Error>;
14template <class Error>
15using unexpected = base::unexpected<Error>;
16} // namespace ipfs
17#elif __has_cpp_attribute(__cpp_lib_expected)
18
19#include <expected>
20namespace ipfs {
21template <class Value, class Error>
22using expected = std::expected<Value, Error>;
23template <class Error>
24using unexpected = std::unexpected<Error>;
25} // namespace ipfs
26
27#elif __has_include(<boost/outcome.hpp>)
28
29// If the API differences between std::expected and boost::outcome::checked
30// become a problem, consider wrapping as proposed in the FAQ:
31// https://www.boost.org/doc/libs/master/libs/outcome/doc/html/faq.html#how-far-away-from-the-proposed-std-expected-t-e-is-outcome-s-checked-t-e
32#include <boost/outcome.hpp>
33namespace ipfs {
34template <class Value, class Error>
35using expected = boost::outcome_v2::checked<Value, Error>;
36template <class Error>
37using unexpected = Error;
38} // namespace ipfs
39
40#else
41#error Get an expected implementation
42#endif
43
44#endif // IPFS_EXPECTED_H_