fp
Functional Programming extensions to C++ for ROS projects.
|
32 #include <fmt/format.h>
38 #include <string_view>
40 #include "fp/_external/expected.hpp"
41 #include "fp/no_discard.hpp"
73 std::string what =
"";
76 return code == other.code && what == other.what;
79 return code != other.code || what != other.what;
83 constexpr
auto Unknown = [](
const std::string& what =
"") {
86 constexpr
auto Cancelled = [](
const std::string& what =
"") {
92 constexpr
auto Timeout = [](
const std::string& what =
"") {
95 constexpr
auto NotFound = [](
const std::string& what =
"") {
110 constexpr
auto Aborted = [](
const std::string& what =
"") {
113 constexpr
auto OutOfRange = [](
const std::string& what =
"") {
119 constexpr
auto Internal = [](
const std::string& what =
"") {
125 constexpr
auto DataLoss = [](
const std::string& what =
"") {
131 constexpr
auto Exception = [](
const std::string& what =
"") {
147 return "InvalidArgument";
153 return "AlreadyExists";
155 return "PermissionDenied";
157 return "ResourceExhausted";
159 return "FailedPrecondition";
165 return "Unimplemented";
169 return "Unavailable";
173 return "Unauthenticated";
177 __builtin_unreachable();
186 template <
typename T,
typename E = Error>
198 template <
typename T,
typename E = Error>
211 template <
typename T,
typename E>
212 constexpr
bool has_error(
const tl::expected<T, E>& exp) {
226 template <
typename E,
typename... Args>
227 constexpr std::optional<E>
maybe_error(tl::expected<Args, E>... args) {
228 auto maybe = std::optional<E>{std::nullopt};
231 if (maybe.has_value())
return;
250 template <
typename F,
typename Ret =
typename std::result_of<F()>::type,
251 typename Exp = Result<Ret>>
255 }
catch (
const std::exception& ex) {
256 return tl::make_unexpected(
Exception(fmt::format(
257 "[{}: {}]", abi::__cxa_current_exception_type()->name(), ex.what())));
267 struct fmt::formatter<
fp::Error> {
268 template <
typename ParseContext>
269 constexpr
auto parse(ParseContext& ctx) {
273 template <
typename FormatContext>
274 auto format(
const fp::Error& error, FormatContext& ctx) {
283 template <
typename T>
284 struct fmt::formatter<
fp::
Result<T>> {
285 template <
typename ParseContext>
286 constexpr
auto parse(ParseContext& ctx) {
290 template <
typename FormatContext>
291 auto format(
const fp::Result<T>& result, FormatContext& ctx) {
292 if (result.has_value()) {
293 return format_to(ctx.out(),
"[Result<T>: value={}]", result.value());
295 return format_to(ctx.out(),
"[Result<T>: {}]", result.error());
constexpr Result< T, E > make_result(T value)
Makes a Result<T> from a T value.
bool operator!=(const Error &other) const noexcept
constexpr std::optional< E > maybe_error(tl::expected< Args, E >... args)
ErrorCode
Enum for ErrorCodes inspired by absl::StatusCode.
bool operator==(const Error &other) const noexcept
constexpr auto AlreadyExists
constexpr std::string_view toStringView(const ErrorCode &code)
convert ErrorCode to string_view for easy formatting
constexpr auto PermissionDenied
constexpr auto OutOfRange
constexpr auto ResourceExhausted
constexpr auto FailedPrecondition
constexpr auto Unimplemented
constexpr auto Unauthenticated
Error type used by Result<T>
constexpr auto InvalidArgument
tl::expected< T, E > Result
constexpr auto Unavailable
Exp try_to_result(F f)
Try to Result<T>. Lifts a function that throws an excpetpion to one that returns a Result<T>
constexpr bool has_error(const tl::expected< T, E > &exp)
Filter function for testing if a result has an error.