Build fix: missing initializer for member 'payload'
[lttng-tools.git] / src / common / optional.hpp
1 /*
2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef LTTNG_OPTIONAL_H
9 #define LTTNG_OPTIONAL_H
10
11 #include <stdint.h>
12
13 /*
14 * Define wrapper structure representing an optional value.
15 *
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.
20 *
21 * Prefer using this macro where "special" values would be used, e.g.
22 * -1ULL for uint64_t types.
23 *
24 * Declaration example:
25 * struct my_struct {
26 * int a;
27 * LTTNG_OPTIONAL(int) b;
28 * };
29 *
30 * Usage example:
31 * struct my_struct foo = LTTNG_OPTIONAL_INIT;
32 *
33 * LTTNG_OPTIONAL_SET(&foo.b, 42);
34 * if (foo.b.is_set) {
35 * printf("%d", foo.b.value);
36 * }
37 *
38 * LTTNG_OPTIONAL_UNSET(&foo.b);
39 */
40 #define LTTNG_OPTIONAL(type) \
41 struct { \
42 uint8_t is_set; \
43 type value; \
44 }
45
46 /*
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
49 * purposes.
50 *
51 * LTTNG_OPTIONAL_COMM should be combined with the LTTNG_PACKED macro when
52 * used for IPC / network communication.
53 */
54 #define LTTNG_OPTIONAL_COMM LTTNG_OPTIONAL
55
56 /*
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.
59 *
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.
62 */
63 #define LTTNG_OPTIONAL_GET(optional) \
64 ({ \
65 LTTNG_ASSERT((optional).is_set); \
66 (optional).value; \
67 })
68
69 /*
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.
73 */
74 #define LTTNG_OPTIONAL_GET_PTR(optional) \
75 ({ \
76 LTTNG_ASSERT((optional).is_set); \
77 &(optional).value; \
78 })
79
80 /*
81 * Initialize an optional field as unset.
82 *
83 * The wrapped field is set to the value it would gave if it had static storage
84 * duration.
85 */
86 #define LTTNG_OPTIONAL_INIT_UNSET {}
87
88 /*
89 * Initialize an optional field as 'set' with a given value.
90 */
91 #define LTTNG_OPTIONAL_INIT_VALUE(val) { .is_set = 1, .value = val }
92
93 /* Set the value of an optional field. */
94 #define LTTNG_OPTIONAL_SET(field_ptr, val) \
95 do { \
96 (field_ptr)->is_set = 1; \
97 (field_ptr)->value = (val); \
98 } while (0)
99
100 /* Put an optional field in the "unset" (NULL-ed) state. */
101 #define LTTNG_OPTIONAL_UNSET(field_ptr) \
102 do { \
103 (field_ptr)->is_set = 0; \
104 } while (0)
105
106 #endif /* LTTNG_OPTIONAL_H */
This page took 0.033267 seconds and 4 git commands to generate.