My Project
Loading...
Searching...
No Matches
result.h
Go to the documentation of this file.
1#ifndef RESULT_H
2#define RESULT_H
3
4#include <stdbool.h>
5
32#define Result(name, value_t, status_t) \
33 typedef struct { \
34 status_t status; \
35 value_t _; \
36 }(name)
37
62#define Maybe(value_t) \
63 Result(Maybe##value_t, value_t, bool); \
64 \
65 Maybe##value_t Some##value_t(value_t value); \
66 \
67 extern const Maybe##value_t None##value_t
68
69#define MaybeImpl(value_t) \
70 Maybe##value_t Some##value_t(value_t value) { \
71 return (Maybe##value_t){true, value}; \
72 } \
73 \
74 const Maybe##value_t None##value_t = {false}
75
76/* Result type for IO operations. */
77#define IOResult(name, value_t) Result(name, value_t, IOStatus)
78
79/* IO operations statuses. */
80typedef enum {
81 /* Keep calm, everything is fine. */
82 IO_OK = 0,
83 /* Stream opening error. */
84 IO_OPEN_ERR,
85 /* Stream closing error. You may lost written data probably. */
86 IO_CLOSE_ERR,
87 /* Cannot read from stream. */
88 IO_READ_ERR,
89 /* Cannot write to stream. */
90 IO_WRITE_ERR,
91} IOStatus;
92
93#endif /* RESULT_H */