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_emplace(private_buffer_pointer, index, \
118 private_type_compound_literal...) \
119 (__extension__({ \
120 typeof(private_type_compound_literal) *private_buffer_res = NULL; \
121 __auto_type private_i = (index); \
122 __auto_type private_emplace_buff_pointer = (private_buffer_pointer); \
123 private_buffer_res \
124 = CCC_buffer_at(private_emplace_buff_pointer, private_i); \
125 if (private_buffer_res) \
126 { \
127 *private_buffer_res = private_type_compound_literal; \
128 } \
129 private_buffer_res; \
130 }))
131
133#define CCC_private_buffer_emplace_back(private_buffer_pointer, \
134 private_type_compound_literal...) \
135 (__extension__({ \
136 typeof(private_type_compound_literal) *private_buffer_res = NULL; \
137 __auto_type private_emplace_back_private_buffer_pointer \
138 = (private_buffer_pointer); \
139 private_buffer_res = CCC_buffer_allocate_back( \
140 (private_emplace_back_private_buffer_pointer)); \
141 if (private_buffer_res) \
142 { \
143 *private_buffer_res = private_type_compound_literal; \
144 } \
145 private_buffer_res; \
146 }))
147
148/* NOLINTEND(readability-identifier-naming) */
149
150#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