libamxc  1.10.3
C Generic Data Containers
amxc_variant_type.c File Reference

Ambiorix variant type API implementation. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <amxc/amxc_variant_type.h>
#include <amxc_variant_priv.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Functions

static int amxc_var_allocate_types (void)
 
static void amxc_var_free_types (void)
 
static amxc_array_it_tamxc_var_next_available_id (void)
 
static amxc_array_it_tamxc_var_get_type_position (const uint32_t index)
 
uint32_t PRIVATE amxc_var_add_type (amxc_var_type_t *const type, const uint32_t index)
 
int PRIVATE amxc_var_remove_type (amxc_var_type_t *const type)
 
amxc_array_tamxc_variant_get_types_array (void)
 
amxc_var_type_tamxc_var_get_type (uint32_t type_id)
 
uint32_t amxc_var_register_type (amxc_var_type_t *const type)
 Register a new variant type. More...
 
int amxc_var_unregister_type (amxc_var_type_t *const type)
 Unregisters an already registered variant type. More...
 
const char * amxc_var_get_type_name_from_id (const uint32_t type_id)
 Get the type name. More...
 
uint32_t amxc_var_get_type_id_from_name (const char *const name)
 Get the type id. More...
 

Variables

static amxc_htable_t amxc_variant_types
 
static amxc_array_t amxc_array_types
 

Detailed Description

Ambiorix variant type API implementation.

Definition in file amxc_variant_type.c.

Function Documentation

◆ amxc_var_add_type()

uint32_t PRIVATE amxc_var_add_type ( amxc_var_type_t *const  type,
const uint32_t  index 
)

Definition at line 119 of file amxc_variant_type.c.

120  {
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 }
#define when_not_null(x, l)
Definition: amxc_macros.h:130
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_null(x, l)
Definition: amxc_macros.h:126
static int amxc_var_allocate_types(void)
static amxc_htable_t amxc_variant_types
static amxc_array_it_t * amxc_var_get_type_position(const uint32_t index)
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_htable_it_init(amxc_htable_it_t *const it)
Initializes a hash table.iterator.
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
The array iterator structure.
Definition: amxc_array.h:174
The hash table iterator structure.
Definition: amxc_htable.h:138
const char * name
amxc_htable_it_t hit

◆ amxc_var_allocate_types()

static int amxc_var_allocate_types ( void  )
static

Definition at line 74 of file amxc_variant_type.c.

74  {
75  int retval = -1;
76 
79 
80  retval = 0;
81 
82 exit:
83  return retval;
84 }
static amxc_array_t amxc_array_types
int amxc_array_init(amxc_array_t *const array, const size_t items)
Initializes an array.
Definition: amxc_array.c:231
int amxc_htable_init(amxc_htable_t *const htable, const size_t reserve)
Initializes a hash table.
Definition: amxc_htable.c:185
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257

◆ amxc_var_free_types()

static void amxc_var_free_types ( void  )
static

Definition at line 86 of file amxc_variant_type.c.

86  {
89 }
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
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

◆ amxc_var_get_type()

amxc_var_type_t* amxc_var_get_type ( uint32_t  type_id)

Definition at line 188 of file amxc_variant_type.c.

188  {
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 }
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
A variant type structure.

◆ amxc_var_get_type_position()

static amxc_array_it_t* amxc_var_get_type_position ( const uint32_t  index)
static

Definition at line 97 of file amxc_variant_type.c.

97  {
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 }
static amxc_array_it_t * amxc_var_next_available_id(void)
int amxc_array_grow(amxc_array_t *const array, const size_t items)
Expands the array.
Definition: amxc_array.c:280

◆ amxc_var_next_available_id()

static amxc_array_it_t* amxc_var_next_available_id ( void  )
static

Definition at line 91 of file amxc_variant_type.c.

91  {
93  ait = amxc_array_it_get_next_free(ait);
94  return ait;
95 }
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

◆ amxc_var_remove_type()

int PRIVATE amxc_var_remove_type ( amxc_var_type_t *const  type)

Definition at line 157 of file amxc_variant_type.c.

157  {
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 }
#define when_true(x, l)
Definition: amxc_macros.h:134
static void amxc_var_free_types(void)
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_VAR_ID_MAX
Same as AMXC_VAR_ID_INVALID.
Definition: amxc_variant.h:263
static amxc_htable_it_t it[2000]

◆ amxc_variant_get_types_array()

amxc_array_t* amxc_variant_get_types_array ( void  )

Definition at line 184 of file amxc_variant_type.c.

184  {
185  return &amxc_array_types;
186 }

Variable Documentation

◆ amxc_array_types

amxc_array_t amxc_array_types
static

Definition at line 65 of file amxc_variant_type.c.

◆ amxc_variant_types

amxc_htable_t amxc_variant_types
static

Definition at line 64 of file amxc_variant_type.c.