Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_object_type.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Object Type
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
18 impl_create_property_type(
19  const afw_object_t *property_types_object,
20  const afw_object_t *property_type_object,
21  const afw_pool_t *p,
22  afw_xctx_t *xctx)
23 {
25  const afw_utf8_t *dataType;
26  afw_boolean_t found;
27 
29  self->property_type_object = property_type_object;
30 
31  self->context_type_id = afw_object_old_get_property_as_string(
32  property_type_object, &afw_s_contextType, xctx);
33 
34  self->default_value = afw_object_get_property(property_type_object,
35  &afw_s_defaultValue, xctx);
36 
37  self->data_type_parameter = afw_object_old_get_property_as_string(
38  property_type_object, &afw_s_dataTypeParameter, xctx);
39 
40  dataType = afw_object_old_get_property_as_string(property_type_object,
41  &afw_s_dataType, xctx);
42  if (dataType) {
43  self->data_type = afw_environment_get_data_type(dataType, xctx);
44  }
45 
46  self->allow_write = afw_object_old_get_property_as_boolean(
47  property_type_object, &afw_s_allowWrite, &found, xctx) || !found;
48 
49  self->allow_query = afw_object_old_get_property_as_boolean_deprecated(property_type_object,
50  &afw_s_allowQuery, xctx);
51 
52  self->required = afw_object_old_get_property_as_boolean_deprecated(property_type_object,
53  &afw_s_required, xctx);
54 
57  return self;
58 }
59 
60 
61 const afw_object_type_t *
63  const afw_adaptor_t *adaptor,
64  const afw_object_t *object_type_object,
65  const afw_pool_t *p,
66  afw_xctx_t *xctx)
67 {
68  afw_object_type_t *self;
69  const afw_iterator_t *iterator;
70  const afw_object_t *property_type_object;
71  afw_object_type_property_type_t *property_type;
72  const afw_utf8_t *property_name;
73 
74  self = afw_pool_calloc_type(p, afw_object_type_t, xctx);
75  self->adaptor_id = &adaptor->adaptor_id;
76  self->object_type_object = object_type_object;
77  self->object_type_id = object_type_object->meta.id;
78 
79  self->property_types_object = afw_object_old_get_property_as_object(
80  object_type_object, &afw_s_propertyTypes, xctx);
81  if (self->property_types_object) {
82  iterator = NULL;
83  while ((property_type_object =
84  afw_object_old_get_next_property_as_object(self->property_types_object,
85  &iterator, &property_name, xctx)))
86  {
87  property_type = impl_create_property_type(
88  self->property_types_object, property_type_object, p, xctx);
89  property_type->property_name = property_name;
90  property_type->next = self->first_property_type;
91  self->first_property_type = property_type;
92  }
93  }
94 
95  self->other_properties_object = afw_object_old_get_property_as_object(
96  object_type_object, &afw_s_otherProperties, xctx);
97  if (self->other_properties_object) {
98  self->other_properties =
99  impl_create_property_type(
100  self->property_types_object, self->other_properties_object,
101  p, xctx);
102  }
103 
104  return self;
105 }
106 
107 
108 
109 /* Get property type object for property */
112  const afw_object_type_t *object_type,
113  const afw_utf8_t *property_name,
114  afw_xctx_t *xctx)
115 {
116  const afw_object_type_property_type_t *result;
117 
118  for (result = object_type->first_property_type;
119  result && !afw_utf8_equal(result->property_name, property_name);
120  result = result->next);
121 
122  if (!result) {
123  result = object_type->other_properties;
124  }
125 
126  return result;
127 }
128 
129 
130 /* Get next property type for object type. */
133  const afw_object_type_t *object_type,
134  const afw_iterator_t * *iterator,
135  const afw_utf8_t * *property_name,
136  afw_xctx_t *xctx)
137 {
138  const afw_object_type_property_type_t *result;
139 
141  if (!object_type->first_property_type) {
142  *iterator = NULL;
143  if (property_name) {
144  *property_name = NULL;
145  }
146  return NULL;
147  }
148 
149  if (!*iterator) {
150  result = object_type->first_property_type;
151  }
152  else {
153  result = ((afw_object_type_property_type_t *)*iterator)->next;
154  }
155 
156  if (result && property_name) {
157  *property_name = result->property_name;
158  }
159 
160  *iterator = (afw_iterator_t *)result;
161 
162  return result;
163 }
164 
165 
166 /* Normalize a value based on property type. */
167 AFW_DEFINE(const afw_value_t *)
170  const afw_value_t *value,
171  const afw_pool_t *p,
172  afw_xctx_t *xctx)
173 {
174  afw_boolean_t normalization_needed;
175  const afw_utf8_t *embedded_object_type_id;
176  const afw_data_type_t *data_type;
177  const afw_object_t *object;
178  const afw_value_t *result;
179 
180  result = value;
181  normalization_needed = false;
182 
183  value = afw_value_evaluate(value, p, xctx);
184 
185  data_type = afw_value_get_data_type(value, xctx);
186  if (pt->data_type && pt->data_type != data_type) {
187  normalization_needed = true;
188  data_type = pt->data_type;
189  }
190 
193  if (normalization_needed) {
194 
195  if (data_type == afw_data_type_object) {
196  AFW_THROW_ERROR_Z(general,
197  "Normalize not supported for object",
198  xctx);
199  }
200 
201  result = afw_value_convert(value, data_type, true, p, xctx);
202  }
203 
204  /* Make user object id set for objects. */
205  if (afw_value_is_object(result)) {
206  object = ((const afw_value_object_t *)result)->internal;
207  embedded_object_type_id =
209  pt->property_type_object, &afw_s_dataTypeParameter, xctx);
210  if (embedded_object_type_id) {
212  object, embedded_object_type_id, xctx);
213  }
214  }
215 
216  return result;
217 }
218 
219 
220 /* Normalize a value based an object type and property name. */
221 AFW_DEFINE(const afw_value_t *)
223  const afw_object_type_t *object_type,
224  const afw_utf8_t *property_name,
225  const afw_value_t *value,
226  const afw_pool_t *p,
227  afw_xctx_t *xctx)
228 {
230  const afw_value_t *result;
231 
232  result = NULL;
233  pt = afw_object_type_property_type_get(object_type,
234  property_name, xctx);
235  if (pt) {
236  result = afw_object_type_property_type_normalize(pt, value,
237  p, xctx);
238  }
239 
240  return result;
241 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
#define afw_object_old_get_property_as_boolean(object, property_name, found, xctx)
Get property function for data type boolean value.
#define afw_value_is_object(A_VALUE)
Macro to determine if value is evaluated object.
#define afw_object_old_get_property_as_object(object, property_name, xctx)
Get property function for data type object value.
afw_data_type_object
Data type struct for object.
#define afw_object_old_get_next_property_as_object(object, iterator, property_name, xctx)
Get next property function for data type object value.
#define afw_object_old_get_property_as_string(object, property_name, xctx)
Get property function for data type string value.
struct afw_iterator_s afw_iterator_t
_Bool afw_boolean_t
Definition: afw_common.h:373
const afw_data_type_t * afw_environment_get_data_type(const afw_utf8_t *type, afw_xctx_t *xctx)
Get the data_type associated with configuration entry type.
#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
#define afw_object_get_property(instance, property_name, xctx)
Call method get_property of interface afw_object.
afw_object_meta_set_object_type_id(const afw_object_t *instance, const afw_utf8_t *object_type_id, afw_xctx_t *xctx)
Set object's object type id.
afw_object_type_property_type_get(const afw_object_type_t *object_type, const afw_utf8_t *property_name, afw_xctx_t *xctx)
Get property type object for property.
afw_object_type_property_type_get_next(const afw_object_type_t *object_type, const afw_iterator_t **iterator, const afw_utf8_t **property_name, afw_xctx_t *xctx)
Get next property type for object type.
afw_object_type_property_type_normalize(const afw_object_type_property_type_t *pt, const afw_value_t *value, const afw_pool_t *p, afw_xctx_t *xctx)
Normalize a value based on property type.
const afw_object_type_t * afw_object_type_internal_create(const afw_adaptor_t *adaptor, const afw_object_t *object_type_object, const afw_pool_t *p, afw_xctx_t *xctx)
Create an object type.
afw_object_type_property_normalize(const afw_object_type_t *object_type, const afw_utf8_t *property_name, const afw_value_t *value, const afw_pool_t *p, afw_xctx_t *xctx)
Normalize a value based an object type and property name.
afw_object_old_get_property_as_boolean_deprecated(const afw_object_t *instance, const afw_utf8_t *property_name, afw_xctx_t *xctx)
Get an object's property value as a boolean.
Definition: afw_object.c:436
#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.
#define afw_value_get_data_type(instance, xctx)
Call method get_data_type of interface afw_value.
#define afw_value_evaluate(value, p, xctx)
Evaluate value if needed using specific pool.
Definition: afw_value.h:841
afw_value_convert(const afw_value_t *value, const afw_data_type_t *to_data_type, afw_boolean_t required, const afw_pool_t *p, afw_xctx_t *xctx)
Convert a value to a value/data type.
Definition: afw_value.c:584
Interface afw_adaptor public struct.
Interface afw_data_type public struct.
const afw_utf8_t * id
Object id or property name.
Definition: afw_common.h:782
Interface afw_object public struct.
Struct for afw_object_type_property_type_t.
Struct for afw_object_type_t.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
struct for data type object values.
Interface afw_value public struct.
Interface afw_xctx public struct.