libamxc  1.10.3
C Generic Data Containers
Variant types

Data Structures

struct  _amxc_var_type
 A variant type structure. More...
 

Typedefs

typedef int(* amxc_var_new_fn_t) (amxc_var_t *const var)
 Variant type callback function prototype for initializing a variant of a certain type. More...
 
typedef void(* amxc_var_free_fn_t) (amxc_var_t *const var)
 Variant type callback function prototype for freeing previous allocated memory. More...
 
typedef int(* amxc_var_copy_fn_t) (amxc_var_t *const dst, const amxc_var_t *const src)
 Variant type callback function prototype for creating a copy. More...
 
typedef int(* amxc_var_move_fn_t) (amxc_var_t *const dst, amxc_var_t *const src)
 Variant type callback function prototype for moving the content. More...
 
typedef int(* amxc_var_convert_fn_t) (amxc_var_t *const dest, const amxc_var_t *const src)
 Variant type callback function prototype for dynamically converting one type to another. More...
 
typedef int(* amxc_var_compare_fn_t) (const amxc_var_t *const var1, const amxc_var_t *const var2, int *const result)
 Variant type callback function prototype comparing two variants of the same type. More...
 
typedef amxc_var_t *(* amxc_var_get_key_fn_t) (const amxc_var_t *const src, const char *const key, int flags)
 Variant type callback function prototype fetch part of variant by key. More...
 
typedef int(* amxc_var_set_key_fn_t) (amxc_var_t *const dest, amxc_var_t *const src, const char *const key, int flags)
 Variant type callback function prototype set part of variant by key. More...
 
typedef amxc_var_t *(* amxc_var_get_index_fn_t) (const amxc_var_t *const src, const int64_t index, int flags)
 Variant type callback function prototype fetch part of variant by index. More...
 
typedef int(* amxc_var_set_index_fn_t) (amxc_var_t *const dest, amxc_var_t *const src, const int64_t index, int flags)
 Variant type callback function prototype set part of variant by index. More...
 
typedef struct _amxc_var_type amxc_var_type_t
 A variant type structure. More...
 

Functions

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...
 
amxc_var_type_tamxc_var_get_type (unsigned int type_id)
 Get the type definition structure. More...
 

Detailed Description

Typedef Documentation

◆ amxc_var_compare_fn_t

typedef int(* amxc_var_compare_fn_t) (const amxc_var_t *const var1, const amxc_var_t *const var2, int *const result)

Variant type callback function prototype comparing two variants of the same type.

Definition at line 127 of file amxc_variant_type.h.

◆ amxc_var_convert_fn_t

typedef int(* amxc_var_convert_fn_t) (amxc_var_t *const dest, const amxc_var_t *const src)

Variant type callback function prototype for dynamically converting one type to another.

Definition at line 118 of file amxc_variant_type.h.

◆ amxc_var_copy_fn_t

typedef int(* amxc_var_copy_fn_t) (amxc_var_t *const dst, const amxc_var_t *const src)

Variant type callback function prototype for creating a copy.

Definition at line 101 of file amxc_variant_type.h.

◆ amxc_var_free_fn_t

typedef void(* amxc_var_free_fn_t) (amxc_var_t *const var)

Variant type callback function prototype for freeing previous allocated memory.

Definition at line 94 of file amxc_variant_type.h.

◆ amxc_var_get_index_fn_t

typedef amxc_var_t*(* amxc_var_get_index_fn_t) (const amxc_var_t *const src, const int64_t index, int flags)

Variant type callback function prototype fetch part of variant by index.

Definition at line 155 of file amxc_variant_type.h.

◆ amxc_var_get_key_fn_t

typedef amxc_var_t*(* amxc_var_get_key_fn_t) (const amxc_var_t *const src, const char *const key, int flags)

Variant type callback function prototype fetch part of variant by key.

Definition at line 136 of file amxc_variant_type.h.

◆ amxc_var_move_fn_t

typedef int(* amxc_var_move_fn_t) (amxc_var_t *const dst, amxc_var_t *const src)

Variant type callback function prototype for moving the content.

Definition at line 109 of file amxc_variant_type.h.

◆ amxc_var_new_fn_t

typedef int(* amxc_var_new_fn_t) (amxc_var_t *const var)

Variant type callback function prototype for initializing a variant of a certain type.

Allocating memory to store the data is allowed, but a callback function for freeing the data must be provided, see amxc_var_free_fn_t

Definition at line 87 of file amxc_variant_type.h.

◆ amxc_var_set_index_fn_t

typedef int(* amxc_var_set_index_fn_t) (amxc_var_t *const dest, amxc_var_t *const src, const int64_t index, int flags)

Variant type callback function prototype set part of variant by index.

Definition at line 164 of file amxc_variant_type.h.

◆ amxc_var_set_key_fn_t

typedef int(* amxc_var_set_key_fn_t) (amxc_var_t *const dest, amxc_var_t *const src, const char *const key, int flags)

Variant type callback function prototype set part of variant by key.

Definition at line 145 of file amxc_variant_type.h.

◆ amxc_var_type_t

A variant type structure.

New or application specific variants can be defined and registered. All function pointers are optional.

The member type_id and hit will be filled during registration and must never be changed.

A unique name must be provided.

The init function is called when the type of the variant is set see amxc_var_set_type. When the initialization is ok, return 0, otherwise return an error code.

The del function is called when the content of the variant must be cleared.

The copy function is called when a copy needs to be made.

See also
ref amxc_var_copy. The function must return 0 when the copy was successful, an error code otherwise.

The convert functions convert_from, convert_to are called when conversion from one type to another type is needed. These functions take two arguments, the destination variant and the source variant. These functions are used by amxc_var_convert. If the source variant type has a convert_to function, it will be called. If that conversion fails or the source variant type does not have a convert_to function, the convert_from function of the destination variant type is called. When conversion was successful, these functions must return 0, if conversion was impossible (not supported) or fails for any other reason an error code must be returned.

The compare function is called to compare two variants. Before calling the compare function, one of the variants will be converted to the same type as the other one. If none of the variants can be converted to the type of the other variant, the compare fails. The compare functions must set the result to a number < 0 if the left variant is smaller then the right variant, to a number > 0 if the left variant is bigger then the right variant, or to 0 if they are the same.

Function Documentation

◆ amxc_var_get_type()

amxc_var_type_t* amxc_var_get_type ( unsigned int  type_id)

Get the type definition structure.

Get the type definition structure.

Parameters
type_idthe type id
Returns
returns the type definition structure for the type id or NULL if the type id does not exist

◆ amxc_var_get_type_id_from_name()

uint32_t amxc_var_get_type_id_from_name ( const char *const  name)

Get the type id.

Using the type name look-up the type id and return it.

Parameters
namethe name of the type
Returns
returns the type id, or AMXC_VAR_ID_MAX if no type with the given name was not found

Definition at line 234 of file amxc_variant_type.c.

234  {
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_true(x, l)
Definition: amxc_macros.h:134
#define when_null(x, l)
Definition: amxc_macros.h:126
static amxc_htable_t amxc_variant_types
#define amxc_htable_it_get_data(it, type, member)
Gets the data pointer from an hash table iterator.
Definition: amxc_htable.h:87
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
#define AMXC_VAR_ID_MAX
Same as AMXC_VAR_ID_INVALID.
Definition: amxc_variant.h:263
The hash table iterator structure.
Definition: amxc_htable.h:138
A variant type structure.

◆ amxc_var_get_type_name_from_id()

const char* amxc_var_get_type_name_from_id ( const uint32_t  type_id)

Get the type name.

Using the type id look-up the type name and return it.

Parameters
type_idthe type id
Returns
returns the name of the type id, or NULL pointer when no type exists with that type id.

Definition at line 220 of file amxc_variant_type.c.

220  {
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 }
static amxc_array_t amxc_array_types
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
The array iterator structure.
Definition: amxc_array.h:174
const char * name

◆ amxc_var_register_type()

uint32_t amxc_var_register_type ( amxc_var_type_t *const  type)

Register a new variant type.

After the type has been registered the fields type_id and hit will be filled. These values should not be changed.

The type can be used after registration.

Typically the registration is done in a constructor function.

Note
Make sure that the pointer to the registered structure is valid until the type is unregistered.
Parameters
typepointer to a struct defining this type
Returns
When registration is successful this function returns 0.

Definition at line 199 of file amxc_variant_type.c.

199  {
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 }
uint32_t PRIVATE amxc_var_add_type(amxc_var_type_t *const type, const uint32_t index)

◆ amxc_var_unregister_type()

int amxc_var_unregister_type ( amxc_var_type_t *const  type)

Unregisters an already registered variant type.

Only registered types can be unregistered.

After unregistering a type it can not be used again until it is registered again using amxc_var_register_type.

Typically unregistration is done in a destructor function.

Note
Any variants of this type still existing will become rogue variants. It will probably not possible anymore to convert them to other types or to free any allocated memory.
Parameters
typepointer to a struct defining this type
Returns
When unregistration is successful this function returns 0.

Definition at line 209 of file amxc_variant_type.c.

209  {
210  int retval = -1;
211  when_null(type, exit);
213 
214  retval = amxc_var_remove_type(type);
215 
216 exit:
217  return retval;
218 }
int PRIVATE amxc_var_remove_type(amxc_var_type_t *const type)
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257