|
Optional Library 0.1
A general-purpose Maybe type for C
|
A general-purpose Maybe type for C. More...
#include <stddef.h>Macros | |
| #define | OPTIONAL_VERSION 0 |
| Returns the major version number of this library. | |
| #define | OPTIONAL(type_name) |
| Returns the type specifier for Optionals with the supplied type name. | |
| #define | OPTIONAL_STRUCT(type) |
| Declares an Optional struct with a default tag and the supplied type. | |
| #define | OPTIONAL_PRESENT(value) |
| Initializes a new Optional containing the supplied value. | |
| #define | OPTIONAL_EMPTY |
| Initializes a new empty Optional. | |
| #define | OPTIONAL_OF_NULLABLE(possibly_null_pointer) |
| Initializes a new Optional based on a possibly null pointer. | |
| #define | OPTIONAL_OF_POSSIBLY_FALSY(possibly_falsy_value) |
| Initializes a new Optional based on a possibly falsy value. | |
| #define | OPTIONAL_IS_PRESENT(optional) |
| Checks if an Optional contains a value. | |
| #define | OPTIONAL_IS_EMPTY(optional) |
| Checks if an Optional is empty. | |
| #define | OPTIONAL_USE_VALUE(optional) |
| Returns an Optional's value. | |
| #define | OPTIONAL_GET_VALUE(optional) |
| Returns an Optional's value as a possibly-null pointer. | |
| #define | OPTIONAL_OR_ELSE(optional, other) |
| Returns an Optional's value, or the supplied one. | |
| #define | OPTIONAL_IF_PRESENT(optional, action) |
| Performs the supplied action with an Optional's value. | |
| #define | OPTIONAL_IF_PRESENT_OR_ELSE(optional, present_action, empty_action) |
| Performs either of the supplied actions with an Optional's value. | |
| #define | OPTIONAL_FILTER(optional, is_acceptable) |
| Conditionally transforms an Optional into an empty one. | |
| #define | OPTIONAL_FILTER_FALSY(optional) |
| Transforms an Optional containing a falsy value into an empty one. | |
| #define | OPTIONAL_FILTER_NULL(optional) |
| Transforms an Optional containing a null pointer into an empty one. | |
| #define | OPTIONAL_MAP(optional, mapper, optional_type) |
| Transforms the value of an Optional. | |
| #define | OPTIONAL_FLAT_MAP(optional, mapper) |
| Transforms an empty Optional into a different one. | |
| #define | OPTIONAL_OR(optional, supplier) |
| Transforms an empty Optional into a different one. | |
| #define | OPTIONAL_TAG(type_name) |
| Returns the struct tag for Optionals with the supplied type name. | |
| #define | OPTIONAL_STRUCT_TAG(type, struct_tag) |
| Declares an Optional struct with the supplied type. | |
A general-purpose Maybe type for C.
This library consists of one header file only. All you need to do is copy optional.h into your project, and include it.
Since it's a header-only library, there is no library code to link against.
| #define OPTIONAL_VERSION 0 |
Returns the major version number of this library.
| #define OPTIONAL | ( | type_name | ) |
Returns the type specifier for Optionals with the supplied type name.
For example, an Optional that can hold an int value, has a type specifier: struct optional_int.
Example:
| type_name | The value type name. |
| #define OPTIONAL_STRUCT | ( | type | ) |
Declares an Optional struct with a default tag and the supplied type.
Example:
| type | The value type. |
| #define OPTIONAL_PRESENT | ( | value | ) |
Initializes a new Optional containing the supplied value.
Example:
| value | The value. |
| #define OPTIONAL_EMPTY |
Initializes a new empty Optional.
Example:
| #define OPTIONAL_OF_NULLABLE | ( | possibly_null_pointer | ) |
Initializes a new Optional based on a possibly null pointer.
This macro will initialize a new empty Optional if the supplied possibly_null_pointer is NULL; otherwise, it will initialize a new Optional containing the supplied pointer.
Example:
| possibly_null_pointer | The possibly null pointer. |
NULL.| #define OPTIONAL_OF_POSSIBLY_FALSY | ( | possibly_falsy_value | ) |
Initializes a new Optional based on a possibly falsy value.
This macro will initialize a new empty Optional if the supplied possibly_falsy_value evaluates to false when converted to bool; otherwise, it will initialize a new Optional containing the supplied value.
Example:
| possibly_falsy_value | The possibly falsy value. |
false.| #define OPTIONAL_IS_PRESENT | ( | optional | ) |
Checks if an Optional contains a value.
Example:
| optional | The Optional to check for presence. |
true if optional is present; otherwise false.| #define OPTIONAL_IS_EMPTY | ( | optional | ) |
Checks if an Optional is empty.
Example:
| optional | The Optional to check for absence. |
true if optional is empty; otherwise false.| #define OPTIONAL_USE_VALUE | ( | optional | ) |
Returns an Optional's value.
Example:
| optional | The Optional to retrieve the value from. |
| #define OPTIONAL_GET_VALUE | ( | optional | ) |
Returns an Optional's value as a possibly-null pointer.
Example:
| optional | The Optional to retrieve the value from. |
NULL. | #define OPTIONAL_OR_ELSE | ( | optional, | |
| other ) |
Returns an Optional's value, or the supplied one.
Example:
| optional | The Optional to retrieve the value from. |
| other | The alternative value. |
| #define OPTIONAL_IF_PRESENT | ( | optional, | |
| action ) |
Performs the supplied action with an Optional's value.
If optional is present, performs the given action with the value; otherwise does nothing.
Example:
| optional | The Optional whose value will be used. |
| action | The function or macro to be applied to optional's value. |
| #define OPTIONAL_IF_PRESENT_OR_ELSE | ( | optional, | |
| present_action, | |||
| empty_action ) |
Performs either of the supplied actions with an Optional's value.
If optional is present, performs the given present-based action with the value; otherwise performs the given empty-based action.
Example:
| optional | The Optional whose value may be used. |
| present_action | The function or macro to be applied to optional's value if present. |
| empty_action | The expression to evaluate if optional is empty. |
| #define OPTIONAL_FILTER | ( | optional, | |
| is_acceptable ) |
Conditionally transforms an Optional into an empty one.
Example:
| optional | The Optional to filter. |
| is_acceptable | The predicate function or macro to apply to the value. |
| #define OPTIONAL_FILTER_FALSY | ( | optional | ) |
Transforms an Optional containing a falsy value into an empty one.
Example:
| optional | The Optional to filter. |
| #define OPTIONAL_FILTER_NULL | ( | optional | ) |
Transforms an Optional containing a null pointer into an empty one.
Example:
| optional | The Optional to filter. |
NULL, a new empty Optional; otherwise, the supplied optional.| #define OPTIONAL_MAP | ( | optional, | |
| mapper, | |||
| optional_type ) |
Transforms the value of an Optional.
Example:
| optional | The Optional whose value will be transformed. |
| mapper | The mapping function or macro that produces the new value. |
| optional_type | The type of the transformed Optional type. |
| #define OPTIONAL_FLAT_MAP | ( | optional, | |
| mapper ) |
Transforms an empty Optional into a different one.
Example:
| optional | The Optional that will be transformed. |
| mapper | The mapping function or macro that produces the new Optional if the given optional is present. |
| #define OPTIONAL_OR | ( | optional, | |
| supplier ) |
Transforms an empty Optional into a different one.
Example:
| optional | The Optional that will be transformed. |
| supplier | The expression that produces the new Optional if the given optional is empty. |
| #define OPTIONAL_TAG | ( | type_name | ) |
Returns the struct tag for Optionals with the supplied type name.
For example, an Optional that can hold an int value, has a struct tag: optional_int.
Example:
| type_name | The value type name. |
| #define OPTIONAL_STRUCT_TAG | ( | type, | |
| struct_tag ) |
Declares an Optional struct with the supplied type.
Example:
| type | The value type. |
| struct_tag | The struct tag. |