generic_iterators
1.0.0
Demonstration of implementing and using type safe generic iterators in pure, standard C
|
Utilities to define and implement an Iterable. More...
Go to the source code of this file.
Macros | |
#define | CONCAT_(A, B) A##B |
#define | CONCAT(A, B) CONCAT_(A, B) |
#define | Iterator(T) T##Iterator |
Convenience macro to get the type of the Iterator (typeclass) with given element type. More... | |
#define | Iterable(T) T##Iterable |
Convenience macro to get the type of the Iterable (typeclass instance) with given element type. More... | |
#define | DefineIteratorOf(T) |
Define an Iterator typeclass and its Iterable instance for given element type. More... | |
#define | impl_iterator(IterType, ElmntType, Name, next_f) |
Define a function to turn given IterType into an Iterable(ElmntType). More... | |
Utilities to define and implement an Iterable.
#define DefineIteratorOf | ( | T | ) |
Define an Iterator typeclass and its Iterable instance for given element type.
T | The type of value the Iterator instance will yield. Must be alphanumeric. |
T
is a pointer, it needs to be typedef-ed into a type that does not contain the *
. Only alphanumerics. T
must also exist. #define impl_iterator | ( | IterType, | |
ElmntType, | |||
Name, | |||
next_f | |||
) |
Define a function to turn given IterType
into an Iterable(ElmntType).
Implement the Iterator typeclass for a type. Essentially defining a wrapper function that returns the Iterable.
The defined function takes in a value of IterType
and wraps it in an Iterable
- which can be passed around to generic functions working on an iterable.
The term "generic" is used here in the context of the input. As in, the function taking a generic iterable, does not care about what type is backing up the iterable; but, does care about what element type the iterator yields.
IterType | The semantic type (C type) this impl is for, must be a pointer type. |
ElmntType | The type of value the Iterator instance will yield. |
Name | Name to define the function as. |
next_f | Function pointer that serves as the next implementation for IterType . This function must have the signature of Maybe(ElmntType) (*)(IterType self) - i.e, should take IterType and return a value of the corresponding element type wrapped in a Maybe - Nothing value indicates end of iteration. |
ElmntType
is a pointer, it needs to be typedef-ed into a type that does not contain the *
. Only alphanumerics. ElmntType
must exist. #define Iterable | ( | T | ) | T##Iterable |
Convenience macro to get the type of the Iterable (typeclass instance) with given element type.
T | The type of value the Iterable will yield. Must be the same type name passed to DefineIteratorOf(T). |
T
is a pointer, it needs to be typedef-ed into a type that does not contain the *
. Only alphanumerics. #define Iterator | ( | T | ) | T##Iterator |
Convenience macro to get the type of the Iterator (typeclass) with given element type.
T | The type of value the Iterator instance will yield. Must be the same type name passed to DefineIteratorOf(T). |
T
is a pointer, it needs to be typedef-ed into a type that does not contain the *
. Only alphanumerics.