Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_list_const_array.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework afw_list interface for const arrays
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 #define impl_afw_list_get_entry_meta afw_list_impl_get_entry_meta
19 #define impl_afw_list_get_next_entry_meta afw_list_impl_get_next_entry_meta
20 
21 
22 /* Declares and rti/inf defines for interface afw_list */
23 #define AFW_IMPLEMENTATION_ID "afw_list_const_array_of_values"
24 #include "afw_list_impl_declares.h"
25 
35  afw_list_t pub;
36 
37  /* Private implementation variables */
38  const afw_value_t *const *values;
39  const afw_value_t *const *end_of_values;
40 
42 
43 
44 /* Create an immutable list from an array of objects. */
45 AFW_DEFINE(const afw_list_t *)
47  const afw_object_t *const *objects,
48  afw_size_t count,
49  const afw_pool_t *p,
50  afw_xctx_t *xctx)
51 {
52  const afw_value_t **values;
53  const afw_value_t **v;
54  const afw_object_t *const *o;
55  afw_size_t i;
56 
57  values = NULL;
58  if (count > 0) {
59  values = afw_pool_malloc(p, count * sizeof(afw_value_t *), xctx);
60  for (o = objects, v = values, i = 0; i < count; o++, v++, i++) {
61  *v = afw_value_create_object(*o, p, xctx);
62  }
63  }
64 
65  return afw_list_const_create_array_of_values(values, count, p, xctx);
66 }
67 
68 
69 /* Create an list from NULL terminated array of objects. */
70 AFW_DEFINE(const afw_list_t *)
72  const afw_object_t * const *objects,
73  const afw_pool_t *p,
74  afw_xctx_t *xctx)
75 {
76  const afw_object_t *const *o;
77  afw_size_t count;
78 
79  count = 0;
80  if (objects) {
81  for (o = objects; *o; count++, o++);
82  }
83 
84  return afw_list_const_create_array_of_objects(objects, count, p, xctx);
85 }
86 
87 
88 
89 /* Create an immutable list from an array of values. */
90 AFW_DEFINE(const afw_list_t *)
92  const afw_value_t *const *values,
93  afw_size_t count,
94  const afw_pool_t *p,
95  afw_xctx_t *xctx)
96 {
98 
99  self = afw_pool_calloc_type(p,
101  self->pub.inf = &impl_afw_list_inf;
102  if (count > 0) {
103  self->values = values;
104  self->end_of_values = &values[count];
105  }
106 
107  return (const afw_list_t *)self;
108 }
109 
110 
111 
112 /* Create an immutable list from NULL terminated array of values. */
113 AFW_DEFINE(const afw_list_t *)
115  const afw_value_t * const *values,
116  const afw_pool_t *p,
117  afw_xctx_t *xctx)
118 {
119  const afw_value_t *const *v;
120  afw_size_t count;
121 
122  count = 0; {
123  for (count = 0, v = values; *v; count++, v++);
124  }
125 
126  return afw_list_const_create_array_of_values(values, count, p, xctx);
127 }
128 
129 
130 
131 /*
132  * Implementation of method release of interface afw_list.
133  */
134 void
136  const afw_list_t * instance,
137  afw_xctx_t *xctx)
138 {
139  /* Nothing to do. */
140 }
141 
142 
143 
144 /*
145  * Implementation of method get_count for interface afw_list.
146  */
149  const afw_list_t * instance,
150  afw_xctx_t *xctx)
151 {
154 
155  return self->end_of_values - self->values;
156 }
157 
158 
159 
160 /*
161  * Implementation of method get_data_type for interface afw_list.
162  */
163 const afw_data_type_t *
165  const afw_list_t * instance,
166  afw_xctx_t *xctx)
167 {
168  return NULL;
169 }
170 
171 
172 
173 /*
174  * Implementation of method get_entry_internal for interface afw_list.
175  */
178  const afw_list_t * instance,
179  afw_integer_t index,
180  const afw_data_type_t * * data_type,
181  const void * * internal,
182  afw_xctx_t *xctx)
183 {
186  afw_size_t i, count;
187  const afw_value_t *value;
188 
189  /* Safecast index to afw_size_t and get value from array. */
190  count = self->end_of_values - self->values;
191  i = afw_safe_cast_integer_to_size(index, xctx);
192  value = (i >= count) ? NULL : self->values[i];
193 
194  if (value) {
195  *internal = AFW_VALUE_INTERNAL(value);
196  if (data_type) {
197  *data_type = afw_value_get_data_type(value, xctx);
198  }
199  return true;
200  }
201  else {
202  *internal = NULL;
203  if (data_type) {
204  *data_type = NULL;
205  }
206  return false;
207  }
208 }
209 
210 
211 
212 /*
213  * Implementation of method get_entry_value for interface afw_list.
214  */
215 const afw_value_t *
217  const afw_list_t * instance,
218  afw_integer_t index,
219  const afw_pool_t * p,
220  afw_xctx_t *xctx)
221 {
224  afw_size_t i, count;
225  const afw_value_t *value;
226 
227  /* Safecast index to afw_size_t and get value from array. */
228  count = self->end_of_values - self->values;
229  i = afw_safe_cast_integer_to_size(index, xctx);
230  value = (i >= count) ? NULL : self->values[i];
231 
232  /* Return result. */
233  return value;
234 }
235 
236 
237 
238 /*
239  * Implementation of method get_next_internal for interface afw_list.
240  */
243  const afw_list_t * instance,
244  const afw_iterator_t * * iterator,
245  const afw_data_type_t * * data_type,
246  const void * * internal,
247  afw_xctx_t *xctx)
248 {
251  const afw_value_t *const *values;
252 
253  /* If iterator is NULL, set it to values. */
254  if (!*iterator) {
255  if (self->values) {
256  *iterator = (const afw_iterator_t *)self->values;
257  }
258  }
259  else {
260  *iterator = (const afw_iterator_t * )
261  ((*(const afw_value_t *const * const *)iterator) + 1);
262  }
263 
264  values = *(const afw_value_t *const *const *)iterator;
265 
266  if (!values || values >= self->end_of_values) {
267  *internal = NULL;
268  *iterator = NULL;
269  if (data_type) {
270  *data_type = NULL;
271  }
272  return false;
273  }
274  else {
275  *internal = AFW_VALUE_INTERNAL(*values);
276  if (data_type) {
277  *data_type = afw_value_get_data_type(*values, xctx);
278  }
279  return true;
280  }
281 }
282 
283 
284 
285 /*
286  * Implementation of method get_next_value for interface afw_list.
287  */
288 const afw_value_t *
290  const afw_list_t * instance,
291  const afw_iterator_t * * iterator,
292  const afw_pool_t * p,
293  afw_xctx_t *xctx)
294 {
297  const afw_value_t *const *values;
298 
299  /* If iterator is NULL, set it to values. */
300  if (!*iterator) {
301  if (self->values) {
302  *iterator = (const afw_iterator_t *)self->values;
303  }
304  }
305  else {
306  *iterator = (const afw_iterator_t *)
307  ((*(const afw_value_t *const * const *)iterator) + 1);
308  }
309 
310  values = *(const afw_value_t *const * const *)iterator;
311 
312  if (!values || values >= self->end_of_values) {
313  *iterator = NULL;
314  return NULL;
315  }
316  else {
317  return *values;
318  }
319 }
320 
321 
322 
323 /*
324  * Implementation of method get_setter for interface afw_list.
325  */
326 const afw_list_setter_t *
328  const afw_list_t * instance,
329  afw_xctx_t *xctx)
330 {
331  return NULL;
332 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
struct impl_afw_list_const_array_of_values_self_s impl_afw_list_const_array_of_values_self_t
Interface afw_interface implementation declares.
afw_value_create_object(const afw_object_t *internal, const afw_pool_t *p, afw_xctx_t *xctx)
Create function for unmanaged data type object value.
struct afw_iterator_s afw_iterator_t
_Bool afw_boolean_t
Definition: afw_common.h:373
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
impl_afw_list_get_next_value(const afw_list_t *instance, const afw_iterator_t **iterator, const afw_pool_t *p, afw_xctx_t *xctx)
impl_afw_list_get_entry_internal(const afw_list_t *instance, afw_integer_t index, const afw_data_type_t **data_type, const void **internal, afw_xctx_t *xctx)
impl_afw_list_get_data_type(const afw_list_t *instance, afw_xctx_t *xctx)
impl_afw_list_release(const afw_list_t *instance, afw_xctx_t *xctx)
impl_afw_list_get_next_internal(const afw_list_t *instance, const afw_iterator_t **iterator, const afw_data_type_t **data_type, const void **internal, afw_xctx_t *xctx)
impl_afw_list_get_setter(const afw_list_t *instance, afw_xctx_t *xctx)
impl_afw_list_get_count(const afw_list_t *instance, afw_xctx_t *xctx)
impl_afw_list_get_entry_value(const afw_list_t *instance, afw_integer_t index, const afw_pool_t *p, afw_xctx_t *xctx)
afw_list_const_create_null_terminated_array_of_objects(const afw_object_t *const *objects, const afw_pool_t *p, afw_xctx_t *xctx)
Create an immutable list from NULL terminated array of objects.
afw_list_const_create_null_terminated_array_of_values(const afw_value_t *const *values, const afw_pool_t *p, afw_xctx_t *xctx)
Create an immutable list from NULL terminated array of values.
afw_list_const_create_array_of_objects(const afw_object_t *const *objects, afw_size_t count, const afw_pool_t *p, afw_xctx_t *xctx)
Create an immutable list from an array of objects.
afw_list_const_create_array_of_values(const afw_value_t *const *values, afw_size_t count, const afw_pool_t *p, afw_xctx_t *xctx)
Create an immutable list from an array of values.
#define afw_pool_malloc(instance, size, xctx)
Call method malloc of interface afw_pool.
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
afw_size_t afw_safe_cast_integer_to_size(afw_integer_t integer, afw_xctx_t *xctx)
Safely cast afw_integer_t to afw_size_t.
Definition: afw_safe_cast.h:68
#define afw_value_get_data_type(instance, xctx)
Call method get_data_type of interface afw_value.
#define AFW_VALUE_INTERNAL(_VALUE_)
Macro to get const void * of the internal of a value.
Definition: afw_value.h:856
Interface afw_data_type public struct.
Interface afw_list public struct.
Interface afw_list_setter public struct.
Interface afw_object public struct.
Interface afw_pool public struct.
Interface afw_value public struct.
Interface afw_xctx public struct.