generic_iterators  1.0.0
Demonstration of implementing and using type safe generic iterators in pure, standard C
iterator.h
Go to the documentation of this file.
1 
6 #ifndef IT_ITERATOR_H
7 #define IT_ITERATOR_H
8 
9 #include "maybe.h"
10 #include "typeclass.h"
11 
12 #define CONCAT_(A, B) A##B
13 #define CONCAT(A, B) CONCAT_(A, B)
14 
31 #define Iterator(T) T##Iterator
32 
48 #define Iterable(T) T##Iterable
49 
65 #define DefineIteratorOf(T) \
66  typedef typeclass(Maybe(T) (*const next)(void* self)) Iterator(T); \
67  typedef typeclass_instance(Iterator(T)) Iterable(T)
68 
123 #define impl_iterator(IterType, ElmntType, Name, next_f) \
124  static inline Maybe(ElmntType) CONCAT(next_f, __)(void* self) \
125  { \
126  Maybe(ElmntType) (*const next_)(IterType self) = (next_f); \
127  (void)next_; \
128  return (next_f)(self); \
129  } \
130  Iterable(ElmntType) Name(IterType x) \
131  { \
132  static Iterator(ElmntType) const tc = {.next = (CONCAT(next_f, __))}; \
133  return (Iterable(ElmntType)){.tc = &tc, .self = x}; \
134  }
135 
136 #endif /* !IT_ITERATOR_H */
Utilities to define and use a Maybe type.
Utilities to define a typeclass and its instance.