C Container Collection (CCC)
Loading...
Searching...
No Matches
private_buffer.h
1
16#ifndef CCC_PRIVATE_BUFFER_H
17#define CCC_PRIVATE_BUFFER_H
18
20#include <assert.h>
21#include <stddef.h>
22#include <string.h>
25#include "../types.h"
26
27/* NOLINTBEGIN(readability-identifier-naming) */
28
34{
36 void *data;
38 size_t count;
40 size_t capacity;
46 void *context;
47};
48
53#define CCC_private_buffer_initialize(private_type_name, private_allocate, \
54 private_context_data, private_capacity, \
55 private_count, private_data...) \
56 { \
57 .data = (private_data), \
58 .sizeof_type = sizeof(private_type_name), \
59 .count = (private_count), \
60 .capacity = (private_capacity), \
61 .allocate = (private_allocate), \
62 .context = (private_context_data), \
63 }
64
67#define CCC_private_buffer_from(private_allocate, private_context_data, \
68 private_optional_capacity, \
69 private_compound_literal_array...) \
70 (__extension__({ \
71 typeof(*private_compound_literal_array) \
72 *private_buffer_initializer_list \
73 = private_compound_literal_array; \
74 struct CCC_Buffer private_buf = CCC_private_buffer_initialize( \
75 typeof(*private_buffer_initializer_list), private_allocate, \
76 private_context_data, 0, 0, NULL); \
77 size_t const private_n = sizeof(private_compound_literal_array) \
78 / sizeof(*private_buffer_initializer_list); \
79 size_t const private_cap = private_optional_capacity; \
80 if (CCC_buffer_reserve( \
81 &private_buf, \
82 (private_n > private_cap ? private_n : private_cap), \
83 private_allocate) \
84 == CCC_RESULT_OK) \
85 { \
86 (void)memcpy(private_buf.data, private_buffer_initializer_list, \
87 private_n \
88 * sizeof(*private_buffer_initializer_list)); \
89 private_buf.count = private_n; \
90 } \
91 private_buf; \
92 }))
93
96#define CCC_private_buffer_with_capacity(private_type_name, private_allocate, \
97 private_context_data, \
98 private_capacity) \
99 (__extension__({ \
100 struct CCC_Buffer private_buf = CCC_private_buffer_initialize( \
101 private_type_name, private_allocate, private_context_data, 0, 0, \
102 NULL); \
103 (void)CCC_buffer_reserve(&private_buf, (private_capacity), \
104 private_allocate); \
105 private_buf; \
106 }))
107
109#define CCC_private_buffer_with_compound_literal( \
110 private_count, private_compound_literal_array...) \
111 \
112 { \
113 .data = (private_compound_literal_array), \
114 .sizeof_type = sizeof(*private_compound_literal_array), \
115 .count = private_count, \
116 .capacity = sizeof(private_compound_literal_array) \
117 / sizeof(*private_compound_literal_array), \
118 .allocate = NULL, \
119 .context = NULL, \
120 }
121
123#define CCC_private_buffer_with_context_compound_literal( \
124 private_context, private_count, private_compound_literal_array...) \
125 \
126 { \
127 .data = (private_compound_literal_array), \
128 .sizeof_type = sizeof(*private_compound_literal_array), \
129 .count = private_count, \
130 .capacity = sizeof(private_compound_literal_array) \
131 / sizeof(*private_compound_literal_array), \
132 .allocate = NULL, \
133 .context = (private_context), \
134 }
135
137#define CCC_private_buffer_with_allocator(private_type_name, private_allocate) \
138 { \
139 .data = NULL, \
140 .sizeof_type = sizeof(private_type_name), \
141 .count = 0, \
142 .capacity = 0, \
143 .allocate = (private_allocate), \
144 .context = NULL, \
145 }
146
148#define CCC_private_buffer_with_context_allocator( \
149 private_type_name, private_allocate, private_context) \
150 { \
151 .data = NULL, \
152 .sizeof_type = sizeof(private_type_name), \
153 .count = 0, \
154 .capacity = 0, \
155 .allocate = (private_allocate), \
156 .context = (private_context), \
157 }
158
160#define CCC_private_buffer_emplace(private_buffer_pointer, index, \
161 private_type_compound_literal...) \
162 (__extension__({ \
163 typeof(private_type_compound_literal) *private_buffer_res = NULL; \
164 __auto_type private_i = (index); \
165 __auto_type private_emplace_buff_pointer = (private_buffer_pointer); \
166 private_buffer_res \
167 = CCC_buffer_at(private_emplace_buff_pointer, private_i); \
168 if (private_buffer_res) \
169 { \
170 *private_buffer_res = private_type_compound_literal; \
171 } \
172 private_buffer_res; \
173 }))
174
176#define CCC_private_buffer_emplace_back(private_buffer_pointer, \
177 private_type_compound_literal...) \
178 (__extension__({ \
179 typeof(private_type_compound_literal) *private_buffer_res = NULL; \
180 __auto_type private_emplace_back_private_buffer_pointer \
181 = (private_buffer_pointer); \
182 private_buffer_res = CCC_buffer_allocate_back( \
183 (private_emplace_back_private_buffer_pointer)); \
184 if (private_buffer_res) \
185 { \
186 *private_buffer_res = private_type_compound_literal; \
187 } \
188 private_buffer_res; \
189 }))
190
191/* NOLINTEND(readability-identifier-naming) */
192
193#endif /* CCC_PRIVATE_BUFFER_H */
Definition: private_buffer.h:34
void * context
Definition: private_buffer.h:46
size_t capacity
Definition: private_buffer.h:40
size_t count
Definition: private_buffer.h:38
size_t sizeof_type
Definition: private_buffer.h:42
CCC_Allocator * allocate
Definition: private_buffer.h:44
void * data
Definition: private_buffer.h:36
void * CCC_Allocator(CCC_Allocator_context)
An allocation function at the core of all containers.
Definition: types.h:340