C API

Header file result_c

struct cxx_error_code;

#define CXX_DECLARE_RESULT(R,RD,S,SD)

#define CXX_DECLARE_RESULT_EC(R,RD)

#define CXX_RESULT(R,S)

#define CXX_RESULT_EC(R)

#define CXX_RESULT_HAS_VALUE(r)

#define CXX_RESULT_HAS_ERROR(r)

#define CXX_RESULT_ERROR_IS_ERRNO(r)

#define CXX_RESULT_ERROR(r)

#define CXX_RESULT_SET_ERRNO(r)

Struct cxx_error_code

struct cxx_error_code
{
    int code;

    void* category;
};

A C struct representation of std::error_code.


Macro CXX_DECLARE_RESULT

#define CXX_DECLARE_RESULT(R,RD,S,SD) struct result_##R##_##S { RD value; unsigned flags; SD error; }

Declares a C struct representation of result<R, S>.


Macro CXX_DECLARE_RESULT_EC

#define CXX_DECLARE_RESULT_EC(R,RD) CXX_DECLARE_RESULT(R, RD, errorcode, struct cxx_error_code)

Declares a C struct representation of result<R, std::error_code>.


Macro CXX_RESULT

#define CXX_RESULT(R,S) struct result_##R##_##S

A reference to a previously declared struct by CXX_DECLARE_RESULT(R, RD, S, SD)


Macro CXX_RESULT_EC

#define CXX_RESULT_EC(R) struct result_##R##_errorcode

A reference to a previously declared struct by CXX_DECLARE_RESULT_EC(R, RD)


Macro CXX_RESULT_HAS_VALUE

#define CXX_RESULT_HAS_VALUE(r) (((r).flags & 1U) == 1U)

True if a result struct has a valid value


Macro CXX_RESULT_HAS_ERROR

#define CXX_RESULT_HAS_ERROR(r) (((r).flags & 2U) == 2U)

True if a result struct has a valid error


Macro CXX_RESULT_ERROR_IS_ERRNO

#define CXX_RESULT_ERROR_IS_ERRNO(r) (((r).flags & (1U << 4U)) == (1U << 4U))

True if a result struct’s error or code is an errno domain code suitable for setting errno with.


Macro CXX_RESULT_ERROR

#define CXX_RESULT_ERROR(r) _Generic((r).error, struct cxx_error_code : ((struct cxx_error_code *) &(r).error)->code, default : (r).error)

C11 generic selecting a result struct’s error or code integer member.


Macro CXX_RESULT_SET_ERRNO

#define CXX_RESULT_SET_ERRNO(r) (errno = CXX_RESULT_HAS_ERROR(r) ? (CXX_RESULT_ERROR_IS_ERRNO(r) ? CXX_RESULT_ERROR(r) : EAGAIN) : 0)

Convenience macro setting errno to a result struct’s errno compatible error if present, or EAGAIN if errored but incompatible.