libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_byte_array.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblByteArray structure for managing generic byte data
3 * \ingroup utils
4 *
5 * This file includes the public definition of the GblByteArray
6 * structure as well as its related API. It is a reference-counted,
7 * mutable, dynamically resizable array of bytes
8 *
9 * \todo
10 * - default argument overloading
11 * - consider making it dynamically resizable easily w/ capacity?
12 * - search/find subbytes
13 * - GblByteView type?
14 *
15 * \author 2023 Falco Girgis
16 * \copyright MIT License
17 */
18#ifndef GIMBAL_BYTE_ARRAY_H
19#define GIMBAL_BYTE_ARRAY_H
20
21#include "../strings/gimbal_string_view.h"
22
23#define GBL_SELF_TYPE GblByteArray
24
26
27/*! \brief Reference-counted resizable array of bytes
28 * \ingroup utils
29 *
30 * GblByteArray is a reference-counted dynamic array of
31 * bytes with a list-style API as well as various methods
32 * for manipulating and extracting subarrays.
33 *
34 * \sa GblBitView
35 */
36typedef struct GblByteArray {
37 size_t size; //!< Size of the pData structure
38 uint8_t* pData; //!< Actual data payload, contiguously-allocated array of bytes
39} GblByteArray;
40
41/*! \name Lifetime Management
42 * \brief Methods for creation and reference management
43 * \relatesalso GblByteArray
44 * @{
45 */
46//! Creates and returns a new GblByteArray, with the given \p size and optional initial data and context
47GBL_EXPORT GblByteArray* GblByteArray_create (size_t size,
48 const void* pData/*=NULL*/,
49 GblContext* pCtx /*=NULL*/) GBL_NOEXCEPT;
50//! Increments the reference counter of the given GblByteArray by 1, returning back a pointer to it
52//! Decrements the reference counter of the given GblByteArray by 1, destroying it upon hitting 0
54//! @}
55
56/*! \name Type Operators
57 * \brief Assignment and comparison methods
58 * \relatesalso GblByteArray
59 * @{
60 */
61//! Frees the existing allocation and copies over the allocation and size from \p pOther
62GBL_EXPORT GBL_RESULT GblByteArray_copy (GBL_SELF, const GblByteArray* pOther) GBL_NOEXCEPT;
63//! Frees the existing allocation and takes the allocation from \p pOther, clearing it
64GBL_EXPORT GBL_RESULT GblByteArray_move (GBL_SELF, GblByteArray* pOther) GBL_NOEXCEPT;
65//! Frees the existing allocation and takes the allocation given by \p pData with the given size
66GBL_EXPORT GBL_RESULT GblByteArray_acquire (GBL_SELF, size_t bytes, void* pData) GBL_NOEXCEPT;
67//! Releases the internal allocation resource, copying it and its size out, clearing them from \p pSelf
68GBL_EXPORT GBL_RESULT GblByteArray_release (GBL_SELF, size_t* pSize, void** ppData) GBL_NOEXCEPT;
69//! Compares the two byte arrays with semantics similar to memcmp(), returning the result
70GBL_EXPORT int GblByteArray_compare (GBL_CSELF, const GblByteArray* pRhs) GBL_NOEXCEPT;
71//! Returns GBL_TRUE if the values stored within the two byte arrays are all equal
73//! @}
74
75/*! \name Properties
76 * \brief Methods for getting values
77 * \relatesalso GblByteArray
78 * @{
79 */
80//! Returns the current number of active references to the given GblByteArray
82//! Returns the GblContext pointer the GblByteArray was constructed with
84//! Returns the size of the GblByteArray (GblByteArray::size)
86//! Returns the data pointer of the GblByteArray (GblByteArray::pData)
88//! Return GBL_TRUE if the given GblByteArray is empty (0 bytes)
90//! Computes a hash over the GblByteArray, returning its value
92//! Returns the GblByteArray as a C string, or NULL if it's not NULL-terminated
94//! Returns a GblStringView spanning the bytes of the GblByteArray
96//! @}
97
98/*! \name Reading and Writing
99 * \brief Methods for fetching and storing data
100 * \relatesalso GblByteArray
101 * @{
102 */
103//! Returns the value of the byte located at the given \p index, raising an error upon out-of-range access
105//! Attempts to read a range of \p bytes starting at \p offset, copying them to \p pOut, returning a result code
106GBL_EXPORT GBL_RESULT GblByteArray_read (GBL_CSELF, size_t offset, size_t bytes, void* pOut) GBL_NOEXCEPT;
107//! Attempts to write a range of \p bytes starting at \p offset, copying from \p pIn, returning a result code
108GBL_EXPORT GBL_RESULT GblByteArray_write (GBL_SELF, size_t offset, size_t bytes, const void* pIn) GBL_NOEXCEPT;
109//! Resizes the GblByteArray to \p bytes, copying \p pData over into its internal array
110GBL_EXPORT GBL_RESULT GblByteArray_set (GBL_SELF, size_t bytes, const void* pData) GBL_NOEXCEPT;
111//! @}
112
113/*! \name List API
114 * \brief Methods providing a standard list-style interface
115 * \relatesalso GblByteArray
116 * @{
117 */
118//! Appends \p bytes from \p pData to the end of the given GblByteArray, returning a status code
119GBL_EXPORT GBL_RESULT GblByteArray_append (GBL_SELF, size_t bytes, const void* pData) GBL_NOEXCEPT;
120//! Prepends \p bytes from \p pData to the beginning of the given GblByteArray, returning a status code
121GBL_EXPORT GBL_RESULT GblByteArray_prepend (GBL_SELF, size_t bytes, const void* pData) GBL_NOEXCEPT;
122//! Inserts \p bytes into the given GblByteArray at \p offset, copying from \p pData, returning a status code and resizing as needed
123GBL_EXPORT GBL_RESULT GblByteArray_insert (GBL_SELF, size_t offset, size_t bytes, const void* pData) GBL_NOEXCEPT;
124//! Erases \p bytes from the given GblByteArray starting at \p offset, returning a status code
125GBL_EXPORT GBL_RESULT GblByteArray_erase (GBL_SELF, size_t offset, size_t bytes) GBL_NOEXCEPT;
126//! Clears all bytes from the given GblByteArray, freeing its allocation and resetting its size to 0
128//! Resizes the given GblByteArray to a size of \p bytes, appending byte values of 0 when growing, returning a status code
130//! Grows the given GblByteArray by the given number of \p bytes, filling the new ones with 0s, returning a status code
132//! Shrinks the given GblByteArray by the given number of \p bytes, returning a status code
134//! @}
135
137
138//! \cond
139#define GblByteArray_create(...)
140 GblByteArray_createDefault_(__VA_ARGS__)
141#define GblByteArray_createDefault_(...)
142 GblByteArray_createDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL)
143#define GblByteArray_createDefault__(bytes, data, ctx, ...)
144 (GblByteArray_create)(bytes, data, ctx)
145//! \endcond
146
147#undef GBL_SELF_TYPE
148
149#endif // GIMBAL_BYTE_ARRAY_H
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
uint32_t GblHash
Type representing a calculated numeric hash across the codebase.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
Reference-counted resizable array of bytes.
GblRefCount GblByteArray_unref(GblByteArray *pSelf)
Decrements the reference counter of the given GblByteArray by 1, destroying it upon hitting 0.
GBL_RESULT GblByteArray_prepend(GblByteArray *pSelf, size_t bytes, const void *pData)
Prepends bytes from pData to the beginning of the given GblByteArray, returning a status code.
const char * GblByteArray_cString(const GblByteArray *pSelf)
Returns the GblByteArray as a C string, or NULL if it's not NULL-terminated.
GblBool GblByteArray_equals(const GblByteArray *pSelf, const GblByteArray *pRhs)
Returns GBL_TRUE if the values stored within the two byte arrays are all equal.
GBL_RESULT GblByteArray_append(GblByteArray *pSelf, size_t bytes, const void *pData)
Appends bytes from pData to the end of the given GblByteArray, returning a status code.
int GblByteArray_compare(const GblByteArray *pSelf, const GblByteArray *pRhs)
Compares the two byte arrays with semantics similar to memcmp(), returning the result.
GBL_RESULT GblByteArray_insert(GblByteArray *pSelf, size_t offset, size_t bytes, const void *pData)
Inserts bytes into the given GblByteArray at offset, copying from pData, returning a status code and ...
GblHash GblByteArray_hash(const GblByteArray *pSelf)
Computes a hash over the GblByteArray, returning its value.
GBL_RESULT GblByteArray_shrink(GblByteArray *pSelf, size_t bytes)
Shrinks the given GblByteArray by the given number of bytes, returning a status code.
GBL_RESULT GblByteArray_resize(GblByteArray *pSelf, size_t bytes)
Resizes the given GblByteArray to a size of bytes, appending byte values of 0 when growing,...
GBL_RESULT GblByteArray_set(GblByteArray *pSelf, size_t bytes, const void *pData)
Resizes the GblByteArray to bytes, copying pData over into its internal array.
void * GblByteArray_data(const GblByteArray *pSelf)
Returns the data pointer of the GblByteArray (GblByteArray::pData)
GBL_RESULT GblByteArray_move(GblByteArray *pSelf, GblByteArray *pOther)
Frees the existing allocation and takes the allocation from pOther, clearing it.
size_t GblByteArray_size(const GblByteArray *pSelf)
Returns the size of the GblByteArray (GblByteArray::size)
GblStringView GblByteArray_stringView(const GblByteArray *pSelf)
Returns a GblStringView spanning the bytes of the GblByteArray.
GblContext * GblByteArray_context(const GblByteArray *pSelf)
Returns the GblContext pointer the GblByteArray was constructed with.
GBL_RESULT GblByteArray_acquire(GblByteArray *pSelf, size_t bytes, void *pData)
Frees the existing allocation and takes the allocation given by pData with the given size.
GblRefCount GblByteArray_refCount(const GblByteArray *pSelf)
Returns the current number of active references to the given GblByteArray.
GblBool GblByteArray_empty(const GblByteArray *pSelf)
Return GBL_TRUE if the given GblByteArray is empty (0 bytes)
GBL_RESULT GblByteArray_release(GblByteArray *pSelf, size_t *pSize, void **ppData)
Releases the internal allocation resource, copying it and its size out, clearing them from pSelf.
GBL_RESULT GblByteArray_erase(GblByteArray *pSelf, size_t offset, size_t bytes)
Erases bytes from the given GblByteArray starting at offset, returning a status code.
GblByteArray * GblByteArray_ref(GblByteArray *pSelf)
Increments the reference counter of the given GblByteArray by 1, returning back a pointer to it.
GBL_RESULT GblByteArray_grow(GblByteArray *pSelf, size_t bytes)
Grows the given GblByteArray by the given number of bytes, filling the new ones with 0s,...
GBL_RESULT GblByteArray_write(GblByteArray *pSelf, size_t offset, size_t bytes, const void *pIn)
Attempts to write a range of bytes starting at offset, copying from pIn, returning a result code.
GBL_RESULT GblByteArray_read(const GblByteArray *pSelf, size_t offset, size_t bytes, void *pOut)
Attempts to read a range of bytes starting at offset, copying them to pOut, returning a result code.
GblByteArray * GblByteArray_create(size_t size, const void *pData, GblContext *pCtx)
Creates and returns a new GblByteArray, with the given size and optional initial data and context.
GBL_RESULT GblByteArray_clear(GblByteArray *pSelf)
Clears all bytes from the given GblByteArray, freeing its allocation and resetting its size to 0.
uint8_t GblByteArray_at(const GblByteArray *pSelf, size_t index)
Returns the value of the byte located at the given index, raising an error upon out-of-range access.
size_t size
Size of the pData structure.
GBL_RESULT GblByteArray_copy(GblByteArray *pSelf, const GblByteArray *pOther)
Frees the existing allocation and copies over the allocation and size from pOther.
uint8_t * pData
Actual data payload, contiguously-allocated array of bytes.