LibJuno 1.0.1
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
array_api.h
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright (c) 2025 Robin A. Onsay
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16*/
17
45#ifndef JUNO_DS_ARRAY_API_H
46#define JUNO_DS_ARRAY_API_H
47#include "juno/macros.h"
49#include "juno/status.h"
50#include "juno/module.h"
51#include <stddef.h>
52#ifdef __cplusplus
53extern "C"
54{
55#endif
56
58typedef struct JUNO_DS_ARRAY_ROOT_TAG JUNO_DS_ARRAY_ROOT_T;
60
65struct JUNO_DS_ARRAY_ROOT_TAG JUNO_MODULE_ROOT(JUNO_DS_ARRAY_API_T,
67 size_t zCapacity;
68);
69
75{
82 JUNO_STATUS_T (*SetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, JUNO_POINTER_T tItem, size_t iIndex);
88 JUNO_RESULT_POINTER_T (*GetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex);
94 JUNO_STATUS_T (*RemoveAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex);
95};
96
101{
102 JUNO_ASSERT_EXISTS(ptArrayApi);
104 ptArrayApi &&
105 ptArrayApi->GetAt &&
106 ptArrayApi->SetAt &&
107 ptArrayApi->RemoveAt
108 );
109 return JUNO_STATUS_SUCCESS;
110}
118{
119 JUNO_ASSERT_EXISTS(ptArray);
121 ptArray->zCapacity
122 );
123 JUNO_STATUS_T tStatus = JunoDs_ArrayApiVerify(ptArray->ptApi);
124 JUNO_ASSERT_SUCCESS(tStatus, return tStatus);
125 return tStatus;
126}
127
136static inline JUNO_STATUS_T JunoDs_ArrayVerifyIndex(const JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
137{
138 JUNO_STATUS_T tStatus = JunoDs_ArrayVerify(ptArray);
139 JUNO_ASSERT_SUCCESS(tStatus, return tStatus);
140 if(iIndex >= ptArray->zCapacity)
141 {
142 tStatus = JUNO_STATUS_OOB_ERROR;
143 JUNO_FAIL_ROOT(tStatus, ptArray, "Index is OOB");
144 }
145 return tStatus;
146}
147
158static inline JUNO_STATUS_T JunoDs_ArrayInit(JUNO_DS_ARRAY_ROOT_T *ptArray, const JUNO_DS_ARRAY_API_T *ptArrayApi, size_t iCapacity, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvUserData)
159{
160 JUNO_ASSERT_EXISTS(ptArray && iCapacity && ptArrayApi);
161 ptArray->ptApi = ptArrayApi;
162 ptArray->zCapacity = iCapacity;
163 ptArray->_pfcnFailureHandler = pfcnFailureHdlr;
164 ptArray->_pvFailureUserData = pvUserData;
165 JUNO_STATUS_T tStatus = JunoDs_ArrayVerify(ptArray);
166 return tStatus;
167}
168
169#ifdef __cplusplus
170}
171#endif
172#endif // JUNO_DS_ARRAY_API_H
173
struct JUNO_DS_ARRAY_ROOT_TAG JUNO_DS_ARRAY_ROOT_T
Opaque array root carrying capacity and API pointer.
Definition array_api.h:58
static JUNO_STATUS_T JunoDs_ArrayApiVerify(const JUNO_DS_ARRAY_API_T *ptArrayApi)
Verify that the array API provides all required functions.
Definition array_api.h:100
static JUNO_STATUS_T JunoDs_ArrayInit(JUNO_DS_ARRAY_ROOT_T *ptArray, const JUNO_DS_ARRAY_API_T *ptArrayApi, size_t iCapacity, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvUserData)
Initialize an array root with capacity and API.
Definition array_api.h:158
static JUNO_STATUS_T JunoDs_ArrayVerify(const JUNO_DS_ARRAY_ROOT_T *ptArray)
Verify an array instance's capacity and API.
Definition array_api.h:117
static JUNO_STATUS_T JunoDs_ArrayVerifyIndex(const JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Verify an index is within array capacity.
Definition array_api.h:136
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:50
#define JUNO_ASSERT_SUCCESS(tStatus,...)
Execute the provided failure operation(s) if status is not success.
Definition macros.h:87
#define JUNO_MODULE_ROOT(API_T,...)
Implement a module root struct containing ptApi and failure fields.
Definition module.h:126
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:56
#define JUNO_STATUS_OOB_ERROR
Index or pointer was out of bounds.
Definition status.h:90
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Failure handler callback signature.
Definition status.h:104
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:49
#define JUNO_FAIL_ROOT(tStatus, ptMod, pcMessage)
Invoke a module root's failure handler if available.
Definition status.h:149
void JUNO_USER_DATA_T
Opaque user data type for failure callbacks.
Definition status.h:96
Common assertion and helper macros for LibJuno modules.
Module system and dependency injection primitives for LibJuno.
Pointer trait and helpers for memory operations.
struct JUNO_POINTER_TAG JUNO_POINTER_T
Definition pointer_api.h:49
Status codes and failure-handling helpers for LibJuno.
Array API vtable.
Definition array_api.h:75
JUNO_STATUS_T(* SetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, JUNO_POINTER_T tItem, size_t iIndex)
Set the value at an index.
Definition array_api.h:82
JUNO_RESULT_POINTER_T(* GetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Get the value at an index.
Definition array_api.h:88
JUNO_STATUS_T(* RemoveAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Remove a value at an index.
Definition array_api.h:94