Loading...
Searching...
No Matches
simple_array.h File Reference

This file provides a simple (non-type safe) array for elements with (optional) in-place construction support. More...

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  SimpleArrayConfig
 Simple Array configuration structure. More...
 

Typedefs

typedef struct SimpleArray SimpleArray
 
typedef void SimpleArrayData
 
typedef void SimpleArrayElement
 
typedef void(* SimpleArrayInit) (SimpleArrayElement *elem)
 
typedef void(* SimpleArrayReset) (SimpleArrayElement *elem)
 
typedef void(* SimpleArrayCopy) (SimpleArrayElement *elem, const SimpleArrayElement *other)
 

Functions

SimpleArraysimple_array_alloc (const SimpleArrayConfig *config)
 Allocate a SimpleArray instance with the given configuration.
 
void simple_array_free (SimpleArray *instance)
 Free a SimpleArray instance and release its contents.
 
void simple_array_init (SimpleArray *instance, uint32_t count)
 Initialise a SimpleArray instance by allocating additional space to contain the requested number of elements.
 
void simple_array_reset (SimpleArray *instance)
 Reset a SimpleArray instance and delete all of its elements.
 
void simple_array_copy (SimpleArray *instance, const SimpleArray *other)
 Copy (duplicate) another SimpleArray instance to this one.
 
bool simple_array_is_equal (const SimpleArray *instance, const SimpleArray *other)
 Check if another SimpleArray instance is equal (the same object or holds the same data) to this one.
 
uint32_t simple_array_get_count (const SimpleArray *instance)
 Get the count of elements currently contained in a SimpleArray instance.
 
SimpleArrayElement * simple_array_get (SimpleArray *instance, uint32_t index)
 Get a pointer to an element contained in a SimpleArray instance.
 
const SimpleArrayElement * simple_array_cget (const SimpleArray *instance, uint32_t index)
 Get a const pointer to an element contained in a SimpleArray instance.
 
SimpleArrayData * simple_array_get_data (SimpleArray *instance)
 Get a pointer to the internal data of a SimpleArray instance.
 
const SimpleArrayData * simple_array_cget_data (const SimpleArray *instance)
 Get a constant pointer to the internal data of a SimpleArray instance.
 

Variables

const SimpleArrayConfig simple_array_config_uint8_t
 

Detailed Description

This file provides a simple (non-type safe) array for elements with (optional) in-place construction support.

No append, remove or take operations are supported. Instead, the user must call simple_array_init() in order to reserve the memory for n elements. In case if init() is specified in the configuration, then the elements are constructed in-place.

To clear all elements from the array, call simple_array_reset(), which will also call reset() for each element in case if it was specified in the configuration. This is useful if a custom destructor is required for a particular type. Calling simple_array_free() will also result in a simple_array_reset() call automatically.

Function Documentation

◆ simple_array_alloc()

SimpleArray * simple_array_alloc ( const SimpleArrayConfig * config)

Allocate a SimpleArray instance with the given configuration.

Parameters
[in]configPointer to the type-specific configuration
Returns
Pointer to the allocated SimpleArray instance

◆ simple_array_cget()

const SimpleArrayElement * simple_array_cget ( const SimpleArray * instance,
uint32_t index )

Get a const pointer to an element contained in a SimpleArray instance.

Parameters
[in]instancePointer to the SimpleArray instance to get an element from
[in]indexIndex of the element in question. MUST be less than total element count
Returns
Const pointer to the element specified by index

◆ simple_array_cget_data()

const SimpleArrayData * simple_array_cget_data ( const SimpleArray * instance)

Get a constant pointer to the internal data of a SimpleArray instance.

Parameters
[in]instancePointer to the SimpleArray instance to get the data of
Returns
Constant pointer to the instance's internal data

◆ simple_array_copy()

void simple_array_copy ( SimpleArray * instance,
const SimpleArray * other )

Copy (duplicate) another SimpleArray instance to this one.

If copy() is specified in the config, then it is called for each element, otherwise the data is simply memcpy()'d.

Parameters
[in]instancePointer to the SimpleArray instance to copy to
[in]otherPointer to the SimpleArray instance to copy from

◆ simple_array_free()

void simple_array_free ( SimpleArray * instance)

Free a SimpleArray instance and release its contents.

Parameters
[in]instancePointer to the SimpleArray instance to be freed

◆ simple_array_get()

SimpleArrayElement * simple_array_get ( SimpleArray * instance,
uint32_t index )

Get a pointer to an element contained in a SimpleArray instance.

Parameters
[in]instancePointer to the SimpleArray instance to get an element from
[in]indexIndex of the element in question. MUST be less than total element count
Returns
Pointer to the element specified by index

◆ simple_array_get_count()

uint32_t simple_array_get_count ( const SimpleArray * instance)

Get the count of elements currently contained in a SimpleArray instance.

Parameters
[in]instancePointer to the SimpleArray instance to query the count from
Returns
Count of elements contained in the instance

◆ simple_array_get_data()

SimpleArrayData * simple_array_get_data ( SimpleArray * instance)

Get a pointer to the internal data of a SimpleArray instance.

Parameters
[in]instancePointer to the SimpleArray instance to get the data of
Returns
Pointer to the instance's internal data

◆ simple_array_init()

void simple_array_init ( SimpleArray * instance,
uint32_t count )

Initialise a SimpleArray instance by allocating additional space to contain the requested number of elements.

If init() is specified in the config, then it is called for each element, otherwise the data is filled with zeroes.

Parameters
[in]instancePointer to the SimpleArray instance to be init'd
[in]countNumber of elements to be allocated and init'd

◆ simple_array_is_equal()

bool simple_array_is_equal ( const SimpleArray * instance,
const SimpleArray * other )

Check if another SimpleArray instance is equal (the same object or holds the same data) to this one.

Parameters
[in]instancePointer to the SimpleArray instance to be compared
[in]otherPointer to the SimpleArray instance to be compared
Returns
True if instances are considered equal, false otherwise

◆ simple_array_reset()

void simple_array_reset ( SimpleArray * instance)

Reset a SimpleArray instance and delete all of its elements.

If reset() is specified in the config, then it is called for each element, otherwise the data is simply free()'d.

Parameters
[in]instancePointer to the SimpleArray instance to be reset