generic_iterators  1.0.0
Demonstration of implementing and using type safe generic iterators in pure, standard C
maybe.h
Go to the documentation of this file.
1 
9 #ifndef IT_MAYBE_H
10 #define IT_MAYBE_H
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 
19 typedef enum
20 {
24 
40 #define Maybe(T) Maybe##T
41 
57 #define DefineMaybe(T) \
58  typedef struct \
59  { \
60  MaybeTag tag; \
61  /* Don't access this member manually */ \
62  T val; \
63  } Maybe(T); \
64  static inline T T##_from_just(Maybe(T) maybex) \
65  { \
66  if (is_just(maybex)) { \
67  return maybex.val; \
68  } else { \
69  fputs("Attempted to extract Just value from Nothing", stderr); \
70  abort(); \
71  } \
72  }
73 
91 #define Just(v, T) ((Maybe(T)){.tag = MaybeTag_Just, .val = (v)})
92 
108 #define Nothing(T) ((Maybe(T)){0})
109 
116 #define is_nothing(x) ((x).tag == MaybeTag_Nothing)
123 #define is_just(x) ((x).tag == MaybeTag_Just)
124 
137 #define from_just(x, T) T##_from_just(x)
138 
151 #define from_just_(x) (x).val
152 
153 #endif /* !IT_MAYBE_H */
MaybeTag
Definition: maybe.h:20
@ MaybeTag_Just
Just tag - indicates presence of a value.
Definition: maybe.h:22
@ MaybeTag_Nothing
Nothing tag - indicates absence of a value.
Definition: maybe.h:21