Graphviz 14.1.4~dev.20260320.0055
Loading...
Searching...
No Matches
optional.h File Reference

C analog of C++’s std::optional More...

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for optional.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define OPTIONAL(type)
 a container that may or may not contain a value
 
#define OPTIONAL_SET(me, value)
 
#define OPTIONAL_VALUE(me)
 
#define OPTIONAL_VALUE_OR(me, fallback)    ((me).has_value ? (me).value_ : (fallback))
 

Macro Definition Documentation

◆ OPTIONAL

#define OPTIONAL (   type)
Value:
struct { \
bool has_value; \
type value_; \
}
expr procedure type
Definition exparse.y:208

Definition at line 13 of file optional.h.

◆ OPTIONAL_SET

#define OPTIONAL_SET (   me,
  value 
)
Value:
do { \
assert((me) != NULL); \
(me)->has_value = true; \
(me)->value_ = (value); \
} while (0)
node NULL
Definition grammar.y:181

set the value of an optional

This utility macro is intended to avoid the easy typo of setting the value while forgetting to set the has_value member.

Parameters
meThe optional whose value to set
valueThe value to assign

Definition at line 26 of file optional.h.

◆ OPTIONAL_VALUE

#define OPTIONAL_VALUE (   me)
Value:
(((me).has_value ? (void)0 \
: (fprintf(stderr, \
"%s:%d: internal error: attempted to read the " \
"value of an empty optional type\n", \
__FILE__, __LINE__), \
abort())), \
(me).value_)

get the value of an optional

The caller must know the optional has a value before calling this.

Parameters
meThe optional whose value to retrieve
Returns
Value of the optional

Definition at line 39 of file optional.h.

◆ OPTIONAL_VALUE_OR

#define OPTIONAL_VALUE_OR (   me,
  fallback 
)     ((me).has_value ? (me).value_ : (fallback))

get the value of an optional or a given value if the optional is empty

Parameters
meThe optional whose value to retrieve
fallbackThe value to return if the optional is empty
Returns
Value of the optional or fallback if it was empty

Definition at line 53 of file optional.h.