Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_object_const_key_value.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Interface afw_object Implementation for <afwdev {srcdir}>
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 
18 
19 #define impl_afw_object_get_meta \
20  afw_object_impl_internal_get_meta
21 
22 #define impl_afw_object_get_property_meta \
23  afw_object_impl_internal_get_property_meta
24 
25 #define impl_afw_object_get_next_property_meta \
26  afw_object_impl_internal_get_next_property_meta
27 
28 /* Declares and rti/inf defines for interface afw_object */
29 #define AFW_IMPLEMENTATION_ID "afw_object_const_key_value"
31 
32 
33 
34 /* Create a const object from key/value utf-8 strings. */
35 AFW_DEFINE(const afw_object_t *)
37  const afw_utf8_t *path,
38  const afw_utf8_z_t * const *pairs,
39  const afw_pool_t *p,
40  afw_xctx_t *xctx)
41 {
43  const afw_utf8_z_t * const *c;
45 
46  self = afw_pool_calloc_type(p,
48  self->pub.inf = &impl_afw_object_inf;
49  self->pub.p = p;
50 
51  /* Count number of pairs and allocate memory for key/values */
52  for (c = pairs; *c; self->entry_count++, c += 2) {
53  if (!*(c + 1)) {
54  AFW_THROW_ERROR_Z(general, "Missing value for pair", xctx);
55  }
56  }
57  self->entry = e = afw_pool_calloc(p,
58  self->entry_count * sizeof(afw_object_const_key_value_self_t), xctx);
59  self->entry_end = e + self->entry_count;
60 
61  /* Set entries */
62  for (c = pairs; *c; c += 2, e++) {
63  e->key.s = *c;
64  e->key.len = strlen(*c);
65  e->value.inf = &afw_value_evaluated_string_inf;
66  e->value.internal.s = *(c + 1);
67  e->value.internal.len = strlen(*(c + 1));
68  }
69 
70  /* Set ids. */
72  (const afw_object_t *)self,
73  path, xctx);
74 
75  /* Return objects. */
76  return (const afw_object_t *)self;
77 }
78 
79 
80 
81 /*
82  * Implementation of method release for interface afw_object.
83  */
84 void
85 impl_afw_object_release(
86  const afw_object_t * instance,
87  afw_xctx_t *xctx)
88 {
89  /* Not reference counted. */
90 }
91 
92 /*
93  * Implementation of method add_reference for interface afw_object.
94  */
95 void
96 impl_afw_object_add_reference(
97  const afw_object_t * instance,
98  afw_xctx_t *xctx)
99 {
100  /* Not reference counted. */
101 }
102 
103 /*
104  * Implementation of method get_count for interface afw_object.
105  */
108  const afw_object_t * instance,
109  afw_xctx_t * xctx)
110 {
111 // <afwdev {prefixed_interface_name}>_self_t *self =
112 // (<afwdev {prefixed_interface_name}>_self_t *)instance;
113 
115  AFW_THROW_ERROR_Z(general, "Method not implemented.", xctx);
116 
117 }
118 
119 /*
120  * Implementation of method get_property for interface afw_object.
121  */
122 const afw_value_t *
123 impl_afw_object_get_property(
124  const afw_object_t * instance,
125  const afw_utf8_t * property_name,
126  afw_xctx_t *xctx)
127 {
131 
132  for (e = self->entry; e < self->entry_end; e++) {
133  if (afw_utf8_equal(&e->key, property_name)) {
134  return (const afw_value_t *)&e->value;
135  }
136  }
137 
138  return NULL;
139 }
140 
141 /*
142  * Implementation of method get_next_property for interface afw_object.
143  */
144 const afw_value_t *
145 impl_afw_object_get_next_property(
146  const afw_object_t * instance,
147  const afw_iterator_t * * iterator,
148  const afw_utf8_t * * property_name,
149  afw_xctx_t *xctx)
150 {
154  const afw_value_t *result;
155 
156  e = (const afw_object_const_key_value_string_entry_t *)*iterator;
157  if (!e) {
158  e = self->entry;
159  }
160 
161  if (e < self->entry_end) {
162  result = (const afw_value_t *)& e->value;
163  if (property_name) {
164  *property_name = &e->key;
165  }
166  *iterator = (afw_iterator_t *)(e + 1);
167  }
168  else {
169  result = NULL;
170  if (property_name) {
171  *property_name = NULL;
172  }
173  *iterator = NULL;
174  }
175 
176  return result;
177 }
178 
179 /*
180  * Implementation of method has_property for interface afw_object.
181  */
183 impl_afw_object_has_property(
184  const afw_object_t * instance,
185  const afw_utf8_t * property_name,
186  afw_xctx_t *xctx)
187 {
188  return impl_afw_object_get_property(instance, property_name, xctx) != NULL;
189 }
190 
191 /*
192  * Implementation of method get_setter for interface afw_object.
193  */
194 const afw_object_setter_t *
195 impl_afw_object_get_setter(
196  const afw_object_t * instance,
197  afw_xctx_t *xctx)
198 {
199  return NULL;
200 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
afw_value_evaluated_string_inf
Unmanaged evaluated value inf for data type string.
struct afw_iterator_s afw_iterator_t
_Bool afw_boolean_t
Definition: afw_common.h:373
afw_utf8_octet_t afw_utf8_z_t
NFC normalized UTF-8 null terminated string.
Definition: afw_common.h:523
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
afw_size_t impl_afw_object_get_count(const afw_object_t *instance, afw_xctx_t *xctx)
afw_object_meta_set_ids_using_path(const afw_object_t *instance, const afw_utf8_t *path, afw_xctx_t *xctx)
Set object's ids using path.
afw_object_create_const_from_key_value_strings_z(const afw_utf8_t *path, const afw_utf8_z_t *const *pairs, const afw_pool_t *p, afw_xctx_t *xctx)
Create a const object from key/value utf-8 null terminated strings.
#define afw_pool_calloc(instance, size, xctx)
Call method calloc 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_boolean_t afw_utf8_equal(const afw_utf8_t *s1, const afw_utf8_t *s2)
Check to see if a string equals another string.
Interface afw_object public struct.
Interface afw_object_setter public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_value public struct.
Interface afw_xctx public struct.