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
50#define CCC_private_buf_non_CCC_private_buf_default_size(...) __VA_ARGS__
52#define CCC_private_buf_default_size(...) 0
54#define CCC_private_buf_optional_size(...) \
55 __VA_OPT__(CCC_private_buf_non_)##CCC_private_buf_default_size(__VA_ARGS__)
56
61#define CCC_private_buffer_initialize(private_data, private_type_name, \
62 private_allocate, private_context_data, \
63 private_capacity, ...) \
64 { \
65 .data = (private_data), \
66 .sizeof_type = sizeof(private_type_name), \
67 .count = CCC_private_buf_optional_size(__VA_ARGS__), \
68 .capacity = (private_capacity), \
69 .allocate = (private_allocate), \
70 .context = (private_context_data), \
71 }
72
75#define CCC_private_buffer_from(private_allocate, private_context_data, \
76 private_optional_capacity, \
77 private_compound_literal_array...) \
78 (__extension__({ \
79 typeof(*private_compound_literal_array) \
80 *private_buffer_initializer_list \
81 = private_compound_literal_array; \
82 struct CCC_Buffer private_buf = CCC_private_buffer_initialize( \
83 NULL, typeof(*private_buffer_initializer_list), private_allocate, \
84 private_context_data, 0); \
85 size_t const private_n = sizeof(private_compound_literal_array) \
86 / sizeof(*private_buffer_initializer_list); \
87 size_t const private_cap = private_optional_capacity; \
88 if (CCC_buffer_reserve( \
89 &private_buf, \
90 (private_n > private_cap ? private_n : private_cap), \
91 private_allocate) \
92 == CCC_RESULT_OK) \
93 { \
94 (void)memcpy(private_buf.data, private_buffer_initializer_list, \
95 private_n \
96 * sizeof(*private_buffer_initializer_list)); \
97 private_buf.count = private_n; \
98 } \
99 private_buf; \
100 }))
101
104#define CCC_private_buffer_with_capacity(private_type_name, private_allocate, \
105 private_context_data, \
106 private_capacity) \
107 (__extension__({ \
108 struct CCC_Buffer private_buf = CCC_private_buffer_initialize( \
109 NULL, private_type_name, private_allocate, private_context_data, \
110 0); \
111 (void)CCC_buffer_reserve(&private_buf, (private_capacity), \
112 private_allocate); \
113 private_buf; \
114 }))
115
117#define CCC_private_buffer_with_compound_literal( \
118 private_count, private_compound_literal_array...) \
119 \
120 { \
121 .data = (private_compound_literal_array), \
122 .sizeof_type = sizeof(*private_compound_literal_array), \
123 .count = private_count, \
124 .capacity = sizeof(private_compound_literal_array) \
125 / sizeof(*private_compound_literal_array), \
126 .allocate = NULL, \
127 .context = NULL, \
128 }
129
131#define CCC_private_buffer_with_context_compound_literal( \
132 private_context, private_count, private_compound_literal_array...) \
133 \
134 { \
135 .data = (private_compound_literal_array), \
136 .sizeof_type = sizeof(*private_compound_literal_array), \
137 .count = private_count, \
138 .capacity = sizeof(private_compound_literal_array) \
139 / sizeof(*private_compound_literal_array), \
140 .allocate = NULL, \
141 .context = (private_context), \
142 }
143
145#define CCC_private_buffer_emplace(private_buffer_pointer, index, \
146 private_type_compound_literal...) \
147 (__extension__({ \
148 typeof(private_type_compound_literal) *private_buffer_res = NULL; \
149 __auto_type private_i = (index); \
150 __auto_type private_emplace_buff_pointer = (private_buffer_pointer); \
151 private_buffer_res \
152 = CCC_buffer_at(private_emplace_buff_pointer, private_i); \
153 if (private_buffer_res) \
154 { \
155 *private_buffer_res = private_type_compound_literal; \
156 } \
157 private_buffer_res; \
158 }))
159
161#define CCC_private_buffer_emplace_back(private_buffer_pointer, \
162 private_type_compound_literal...) \
163 (__extension__({ \
164 typeof(private_type_compound_literal) *private_buffer_res = NULL; \
165 __auto_type private_emplace_back_private_buffer_pointer \
166 = (private_buffer_pointer); \
167 private_buffer_res = CCC_buffer_allocate_back( \
168 (private_emplace_back_private_buffer_pointer)); \
169 if (private_buffer_res) \
170 { \
171 *private_buffer_res = private_type_compound_literal; \
172 } \
173 private_buffer_res; \
174 }))
175
176/* NOLINTEND(readability-identifier-naming) */
177
178#endif /* CCC_PRIVATE_BUF_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