2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef LTTNG_OPTIONAL_H
9 #define LTTNG_OPTIONAL_H
14 * Define wrapper structure representing an optional value.
16 * This macro defines an "is_set" boolean field that must be checked
17 * when accessing the optional field. This "is_set" field provides
18 * the semantics that would be expected of a typical "raw pointer" field
19 * which would be checked for NULL.
21 * Prefer using this macro where "special" values would be used, e.g.
22 * -1ULL for uint64_t types.
24 * Declaration example:
27 * LTTNG_OPTIONAL(int, b);
31 * struct my_struct foo = LTTNG_OPTIONAL_INIT;
33 * LTTNG_OPTIONAL_SET(&foo.b, 42);
35 * printf("%d", foo.b.value);
38 * LTTNG_OPTIONAL_UNSET(&foo.b);
40 #define LTTNG_OPTIONAL(type) \
47 * Alias used for communication structures. If the layout of an LTTNG_OPTIONAL
48 * is changed, the original layout should still be used for communication
51 * LTTNG_OPTIONAL_COMM should be combined with the LTTNG_PACKED macro when
52 * used for IPC / network communication.
54 #define LTTNG_OPTIONAL_COMM LTTNG_OPTIONAL
57 * This macro is available as a 'convenience' to allow sites that assume
58 * an optional value is set to LTTNG_ASSERT() that it is set when accessing it.
60 * Since this returns the 'optional' by value, it is not suitable for all
61 * wrapped optional types. It is meant to be used with PODs.
63 #define LTTNG_OPTIONAL_GET(optional) \
65 LTTNG_ASSERT((optional).is_set); \
70 * This macro is available as a 'convenience' to allow sites that assume
71 * an optional value is set to LTTNG_ASSERT() that it is set when fecthing the
72 * underlying value's address.
74 #define LTTNG_OPTIONAL_GET_PTR(optional) \
76 LTTNG_ASSERT((optional).is_set); \
81 * Initialize an optional field as unset.
83 * The wrapped field is set to the value it would gave if it had static storage
86 #define LTTNG_OPTIONAL_INIT_UNSET { .is_set = 0 }
89 * Initialize an optional field as 'set' with a given value.
91 #define LTTNG_OPTIONAL_INIT_VALUE(val) { .is_set = 1, .value = val }
93 /* Set the value of an optional field. */
94 #define LTTNG_OPTIONAL_SET(field_ptr, val) \
96 (field_ptr)->is_set = 1; \
97 (field_ptr)->value = (val); \
100 /* Put an optional field in the "unset" (NULL-ed) state. */
101 #define LTTNG_OPTIONAL_UNSET(field_ptr) \
103 (field_ptr)->is_set = 0; \
106 #endif /* LTTNG_OPTIONAL_H */
This page took 0.033482 seconds and 4 git commands to generate.