Result and Maybe types.
More...
#include <stdbool.h>
Go to the source code of this file.
|
#define | Result(name, value_t, status_t) |
| Result with value and status.
|
|
#define | Maybe(value_t) |
| Maybe result.
|
|
#define | MaybeImpl(value_t) |
|
#define | IOResult(name, value_t) Result(name, value_t, IOStatus) |
|
|
enum | IOStatus {
IO_OK = 0
, IO_OPEN_ERR
, IO_CLOSE_ERR
, IO_READ_ERR
,
IO_WRITE_ERR
} |
|
◆ Maybe
Value:
\
Maybe##value_t Some##value_t(value_t value); \
\
extern const Maybe##value_t None##value_t
#define Maybe(value_t)
Maybe result.
Definition result.h:62
#define Result(name, value_t, status_t)
Result with value and status.
Definition result.h:32
Maybe result.
Maybe is a value tagged with a validness flag.
Usage: header.h
RGB image.
Definition image.h:39
source.c
MaybeImage make_image() {
if (some_condition) {
return SomeImage(img);
}
return NoneImage;
}
◆ MaybeImpl
#define MaybeImpl |
( |
|
value_t | ) |
|
Value: Maybe##value_t Some##value_t(value_t value) { \
return (
Maybe##value_t){
true, value}; \
} \
\
const Maybe##value_t None##value_t = {
false}
◆ Result
#define Result |
( |
|
name, |
|
|
|
value_t, |
|
|
|
status_t |
|
) |
| |
Value: typedef struct { \
status_t status; \
value_t _; \
}(name)
Result with value and status.
Usage:
typedef enum {
CMD_ARGS_CORRECT = 0,
CMD_ARGS_INCORRECT,
CMD_ARGS_BAD_ANGLE,
} ArgsParseStatus;
ArgsParseResult parsed_args = do_stuff();
if (parsed_args.status != CMD_ARGS_CORRECT) {
exit(1);
}
print_args(parsed_args._);
Command line args required for the program.
Definition _cmd.h:15