C Container Collection (CCC)
Loading...
Searching...
No Matches
private_flat_buffer.h
Go to the documentation of this file.
1
24#ifndef CCC_PRIVATE_FLAT_BUFFER_H
25#define CCC_PRIVATE_FLAT_BUFFER_H
26
28#include <stddef.h>
29#include <string.h>
32#include "../types.h" /* IWYU pragma: keep */
33
34/* NOLINTBEGIN(readability-identifier-naming) */
35
42 void *data;
44 size_t count;
46 size_t capacity;
51};
52
54#define CCC_private_flat_buffer_default(private_type_name) \
55 (struct CCC_Flat_buffer) { \
56 .sizeof_type = sizeof(private_type_name), \
57 .alignof_type = alignof(private_type_name), \
58 }
59
64#define CCC_private_flat_buffer_for( \
65 private_type_name, private_capacity, private_count, private_data... \
66) \
67 (struct CCC_Flat_buffer) { \
68 .data = (private_data), .sizeof_type = sizeof(private_type_name), \
69 .alignof_type = alignof(private_type_name), .count = (private_count), \
70 .capacity = (private_capacity), \
71 }
72
75#define CCC_private_flat_buffer_from( \
76 private_allocator, \
77 private_optional_capacity, \
78 private_compound_literal_array... \
79) \
80 (struct { struct CCC_Flat_buffer private; }){(__extension__({ \
81 typeof(*private_compound_literal_array) \
82 *private_flat_buffer_initializer_list \
83 = private_compound_literal_array; \
84 struct CCC_Flat_buffer private_buf = CCC_private_flat_buffer_default( \
85 typeof(*private_flat_buffer_initializer_list) \
86 ); \
87 size_t const private_n \
88 = sizeof(private_compound_literal_array) \
89 / sizeof(*private_flat_buffer_initializer_list); \
90 size_t const private_cap = private_optional_capacity; \
91 if (CCC_flat_buffer_reserve( \
92 &private_buf, \
93 (private_n > private_cap ? private_n : private_cap), \
94 &(private_allocator) \
95 ) \
96 == CCC_RESULT_OK) { \
97 (void)memcpy( \
98 private_buf.data, \
99 private_flat_buffer_initializer_list, \
100 private_n * sizeof(*private_flat_buffer_initializer_list) \
101 ); \
102 private_buf.count = private_n; \
103 } \
104 private_buf; \
105 }))}.private
106
109#define CCC_private_flat_buffer_with_capacity( \
110 private_type_name, private_allocator, private_capacity \
111) \
112 (struct { struct CCC_Flat_buffer private; }){(__extension__({ \
113 struct CCC_Flat_buffer private_buf \
114 = CCC_private_flat_buffer_default(private_type_name); \
115 (void)CCC_flat_buffer_reserve( \
116 &private_buf, (private_capacity), &(private_allocator) \
117 ); \
118 private_buf; \
119 }))}.private
120
125#if defined(__clang__) || defined(__llvm__)
127# define CCC_private_flat_buffer_with_storage( \
128 private_count, private_compound_literal_array... \
129 ) \
130 (struct { \
131 static_assert( \
132 sizeof(private_compound_literal_array), \
133 "provide non-zero capacity compound literal array" \
134 ); \
135 static_assert( \
136 private_count \
137 <= (sizeof(private_compound_literal_array) \
138 / sizeof(*private_compound_literal_array)), \
139 "provide count less than or equal to capacity of " \
140 "compound literal array" \
141 ); \
142 CCC_Flat_buffer private; \
143 }){{ \
144 .data = (private_compound_literal_array), \
145 .sizeof_type = sizeof(*private_compound_literal_array), \
146 .alignof_type = alignof(*private_compound_literal_array), \
147 .count = private_count, \
148 .capacity = sizeof(private_compound_literal_array) \
149 / sizeof(*private_compound_literal_array), \
150 }} \
151 .private
152#else
154# define CCC_private_flat_buffer_with_storage( \
155 private_count, private_compound_literal_array... \
156 ) \
157 (struct CCC_Flat_buffer) { \
158 .data = (private_compound_literal_array), \
159 .sizeof_type = sizeof(*private_compound_literal_array), \
160 .alignof_type = alignof(*private_compound_literal_array), \
161 .count = private_count, \
162 .capacity = sizeof(private_compound_literal_array) \
163 / sizeof(*private_compound_literal_array), \
164 }
165#endif
166
168#define CCC_private_flat_buffer_emplace( \
169 private_flat_buffer_pointer, index, private_type_compound_literal... \
170) \
171 (__extension__({ \
172 __auto_type private_i = (index); \
173 typeof(private_type_compound_literal) *private_flat_buffer_res \
174 = CCC_flat_buffer_at((private_flat_buffer_pointer), private_i); \
175 if (private_flat_buffer_res) { \
176 *private_flat_buffer_res = private_type_compound_literal; \
177 } \
178 private_flat_buffer_res; \
179 }))
180
182#define CCC_private_flat_buffer_emplace_back( \
183 private_flat_buffer_pointer, \
184 private_allocator_pointer, \
185 private_type_compound_literal... \
186) \
187 (__extension__({ \
188 typeof(private_type_compound_literal) *private_flat_buffer_res \
189 = CCC_flat_buffer_allocate_back( \
190 (private_flat_buffer_pointer), (private_allocator_pointer) \
191 ); \
192 if (private_flat_buffer_res) { \
193 *private_flat_buffer_res = private_type_compound_literal; \
194 } \
195 private_flat_buffer_res; \
196 }))
197
198/* NOLINTEND(readability-identifier-naming) */
199
200#endif /* CCC_PRIVATE_FLAT_BUFFER_H */
Definition: private_flat_buffer.h:40
size_t alignof_type
Definition: private_flat_buffer.h:50
void * data
Definition: private_flat_buffer.h:42
size_t capacity
Definition: private_flat_buffer.h:46
size_t count
Definition: private_flat_buffer.h:44
size_t sizeof_type
Definition: private_flat_buffer.h:48