farm-ng-core
expected.h File Reference
#include "farm_ng/core/logging/logger.h"
#include <tl/expected.hpp>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
Include dependency graph for expected.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  farm_ng::Error
 
struct  farm_ng::Success
 
struct  farm_ng::details::UnwrapImpl< tl::expected< TT, TE > >
 

Namespaces

 farm_ng
 
 farm_ng::details
 

Macros

#define FARM_ERROR_DETAIL(...)
 
#define FARM_ERROR_REPORT(cstr, ...)
 
#define FARM_UNEXPECTED(cstr, ...)   ::tl::make_unexpected(FARM_ERROR_REPORT(cstr, ##__VA_ARGS__))
 
#define FARM_TRY(Type, var, expression)
 Assigns *expression to var of Type, but returns error if there is one. More...
 
#define FARM_ASSERT_OR_ERROR(condition, ...)
 

Typedefs

template<class TT , class TE = Error>
using farm_ng::Expected = tl::expected< TT, TE >
 

Functions

auto farm_ng::operator<< (std::ostream &os, Error const &error) -> std::ostream &
 
template<class TT , class TE = Error>
std::optional< TT > farm_ng::fromExpected (Expected< TT, TE > expected)
 
template<class TT >
Expected< TT > farm_ng::fromOptional (std::optional< TT > optional)
 

Macro Definition Documentation

◆ FARM_ASSERT_OR_ERROR

#define FARM_ASSERT_OR_ERROR (   condition,
  ... 
)
Value:
if (!(condition)) { \
return FARM_UNEXPECTED( \
"bool({}) not true.\n{}", #condition, FARM_FORMAT(__VA_ARGS__)); \
} \
do { \
} while (false)

◆ FARM_ERROR_DETAIL

#define FARM_ERROR_DETAIL (   ...)
Value:
.file = __FILE__, .line = __LINE__, .msg = FARM_FORMAT(__VA_ARGS__) \
}

◆ FARM_ERROR_REPORT

#define FARM_ERROR_REPORT (   cstr,
  ... 
)
Value:
.details = { FARM_ERROR_DETAIL(cstr, ##__VA_ARGS__) } \
}

◆ FARM_TRY

#define FARM_TRY (   Type,
  var,
  expression 
)
Value:
auto maybe##var = (expression); \
if (!maybe##var) { \
auto error = maybe##var.error(); \
error.details.emplace_back( \
FARM_ERROR_DETAIL("FARM_TRY propagated error.\n")); \
return ::tl::make_unexpected(error); \
} \
Type var = ::std::move(*maybe##var);

Assigns *expression to var of Type, but returns error if there is one.

◆ FARM_UNEXPECTED

#define FARM_UNEXPECTED (   cstr,
  ... 
)    ::tl::make_unexpected(FARM_ERROR_REPORT(cstr, ##__VA_ARGS__))
FARM_FORMAT
#define FARM_FORMAT(...)
Formats the cstr using the libfmt library.
Definition: format.h:125
farm_ng::ErrorDetail
Definition: logger.h:401
farm_ng::Error
Definition: expected.h:28
farm_ng::ErrorDetail::file
std::string file
Definition: logger.h:402
farm_ng::Error::details
std::vector< ErrorDetail > details
Definition: expected.h:29
FARM_UNEXPECTED
#define FARM_UNEXPECTED(cstr,...)
Definition: expected.h:86
FARM_ERROR_DETAIL
#define FARM_ERROR_DETAIL(...)
Definition: expected.h:75