libamxc  1.10.3
C Generic Data Containers
amxc_variant_type.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdint.h>
59 
60 #include <amxc/amxc_variant_type.h>
61 #include <amxc_variant_priv.h>
62 #include <amxc/amxc_macros.h>
63 
66 
74 static int amxc_var_allocate_types(void) {
75  int retval = -1;
76 
79 
80  retval = 0;
81 
82 exit:
83  return retval;
84 }
85 
86 static void amxc_var_free_types(void) {
89 }
90 
93  ait = amxc_array_it_get_next_free(ait);
94  return ait;
95 }
96 
97 static amxc_array_it_t* amxc_var_get_type_position(const uint32_t index) {
98  amxc_array_it_t* ait = NULL;
99 
100  if(index == UINT32_MAX) {
101  // no index specified - take first free item after fixed types
103  if(!ait) {
104  // no more free positions, grow array and add
107  }
108  } else {
109  // index specified
110  // this can be only called from inside (fixed types)
111  // no need to check if there is already a type registered here
112  ait = amxc_array_get_at(&amxc_array_types, index);
113  }
114 
115 exit:
116  return ait;
117 }
118 
120  const uint32_t index) {
121  uint32_t type_id = -1;
122  amxc_array_it_t* ait = NULL;
123  amxc_htable_it_t* hit = NULL;
124  when_null(type, exit);
125 
126  hit = amxc_htable_get(&amxc_variant_types, type->name);
127  if(hit == &type->hit) {
128  type_id = type->type_id;
129  goto exit;
130  }
131 
132  when_not_null(hit, exit);
133 
134  // TODO?: move into constructor, this is lib init code
136  // nothing allocated yet - pre-allocate all fixed items
138  }
139 
140  ait = amxc_var_get_type_position(index);
141  when_null(ait, exit);
142 
143  amxc_array_it_set_data(ait, type);
144  amxc_htable_it_init(&type->hit);
145  if(amxc_htable_insert(&amxc_variant_types, type->name, &type->hit) == -1) {
147  goto exit;
148  }
149 
150  type->type_id = amxc_array_it_index(ait);
151  type_id = type->type_id;
152 
153 exit:
154  return type_id;
155 }
156 
158  int retval = -1;
159  amxc_array_it_t* it = NULL;
161  when_null(hit, exit);
162  when_true(hit != &type->hit, exit);
163 
164  amxc_htable_it_take(hit);
165  amxc_htable_it_clean(hit, NULL);
166 
169 
170  type->type_id = AMXC_VAR_ID_MAX;
171 
172  // TODO?: move into destructor, this is lib exit code
173  // if hash table is empty, no more types are registered, clear buffers
176  }
177 
178  retval = 0;
179 
180 exit:
181  return retval;
182 }
183 
185  return &amxc_array_types;
186 }
187 
188 amxc_var_type_t* amxc_var_get_type(uint32_t type_id) {
189  amxc_var_type_t* type = NULL;
191  when_null(ait, exit);
192 
193  type = (amxc_var_type_t*) ait->data;
194 
195 exit:
196  return type;
197 }
198 
200  int retval = -1;
201  when_null(type, exit);
202 
203  retval = amxc_var_add_type(type, AMXC_VAR_ID_MAX);
204 
205 exit:
206  return retval;
207 }
208 
210  int retval = -1;
211  when_null(type, exit);
213 
214  retval = amxc_var_remove_type(type);
215 
216 exit:
217  return retval;
218 }
219 
220 const char* amxc_var_get_type_name_from_id(const uint32_t type_id) {
221  const char* name = NULL;
222  amxc_var_type_t* type = NULL;
224  when_null(ait, exit);
225  when_null(ait->data, exit);
226 
227  type = (amxc_var_type_t*) ait->data;
228  name = type->name;
229 
230 exit:
231  return name;
232 }
233 
234 uint32_t amxc_var_get_type_id_from_name(const char* const name) {
235  uint32_t type_id = AMXC_VAR_ID_MAX;
236  amxc_htable_it_t* hit = NULL;
237  amxc_var_type_t* type = NULL;
238  when_null(name, exit);
239  when_true(*name == 0, exit);
240 
241  hit = amxc_htable_get(&amxc_variant_types, name);
242  when_null(hit, exit);
243 
244  type = amxc_htable_it_get_data(hit, amxc_var_type_t, hit);
245  type_id = type->type_id;
246 
247 exit:
248  return type_id;
249 }
#define when_not_null(x, l)
Definition: amxc_macros.h:130
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_true(x, l)
Definition: amxc_macros.h:134
#define PRIVATE
Definition: amxc_macros.h:66
#define when_null(x, l)
Definition: amxc_macros.h:126
static int amxc_var_allocate_types(void)
static amxc_array_t amxc_array_types
amxc_var_type_t * amxc_var_get_type(uint32_t type_id)
uint32_t PRIVATE amxc_var_add_type(amxc_var_type_t *const type, const uint32_t index)
amxc_array_t * amxc_variant_get_types_array(void)
static amxc_htable_t amxc_variant_types
int PRIVATE amxc_var_remove_type(amxc_var_type_t *const type)
static amxc_array_it_t * amxc_var_get_type_position(const uint32_t index)
static amxc_array_it_t * amxc_var_next_available_id(void)
static void amxc_var_free_types(void)
Ambiorix ring buffer API header file.
amxc_array_it_t * amxc_array_it_get_next_free(const amxc_array_it_t *const reference)
Gets the next free item in the array, starting from the provided array iterator.
Definition: amxc_array_it.c:87
int amxc_array_it_set_data(amxc_array_it_t *const it, void *data)
Sets the data pointer of an array iterator.
void * amxc_array_it_take_data(amxc_array_it_t *const it)
Gets and removes a data pointer from the iterator.
unsigned int amxc_array_it_index(const amxc_array_it_t *const it)
Gets the index of the iterator in the array.
int amxc_array_init(amxc_array_t *const array, const size_t items)
Initializes an array.
Definition: amxc_array.c:231
amxc_array_it_t * amxc_array_get_at(const amxc_array_t *const array, const unsigned int index)
Gets the item iterator for the given index.
Definition: amxc_array.c:504
void amxc_array_clean(amxc_array_t *const array, amxc_array_it_delete_t func)
Removes all items from the array.
Definition: amxc_array.c:261
int amxc_array_grow(amxc_array_t *const array, const size_t items)
Expands the array.
Definition: amxc_array.c:280
int amxc_htable_it_init(amxc_htable_it_t *const it)
Initializes a hash table.iterator.
void amxc_htable_it_clean(amxc_htable_it_t *const it, amxc_htable_it_delete_t func)
Removes the iterator from the htable and frees allocated memory.
void amxc_htable_it_take(amxc_htable_it_t *const it)
Removes the iterator from the hash table.
#define amxc_htable_it_get_data(it, type, member)
Gets the data pointer from an hash table iterator.
Definition: amxc_htable.h:87
int amxc_htable_init(amxc_htable_t *const htable, const size_t reserve)
Initializes a hash table.
Definition: amxc_htable.c:185
AMXC_INLINE bool amxc_htable_is_empty(const amxc_htable_t *const htable)
Checks that the hash table is empty.
Definition: amxc_htable.h:311
amxc_htable_it_t * amxc_htable_get(const amxc_htable_t *const htable, const char *const key)
Gets a hash table iterator from the hash table.
Definition: amxc_htable.c:278
int amxc_htable_insert(amxc_htable_t *const htable, const char *const key, amxc_htable_it_t *const it)
Inserts an item in the hash table.
Definition: amxc_htable.c:237
void amxc_htable_clean(amxc_htable_t *const htable, amxc_htable_it_delete_t func)
Removes all items from the hash table.
Definition: amxc_htable.c:200
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
#define AMXC_VAR_ID_MAX
Same as AMXC_VAR_ID_INVALID.
Definition: amxc_variant.h:263
uint32_t amxc_var_register_type(amxc_var_type_t *const type)
Register a new variant type.
uint32_t amxc_var_get_type_id_from_name(const char *const name)
Get the type id.
int amxc_var_unregister_type(amxc_var_type_t *const type)
Unregisters an already registered variant type.
const char * amxc_var_get_type_name_from_id(const uint32_t type_id)
Get the type name.
The array iterator structure.
Definition: amxc_array.h:174
The array structure.
Definition: amxc_array.h:162
The hash table iterator structure.
Definition: amxc_htable.h:138
The hash table structure.
Definition: amxc_htable.h:175
A variant type structure.
const char * name
amxc_htable_it_t hit
static amxc_htable_it_t it[2000]