LibJuno 1.0.4
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 // @{"req": ["REQ-ARRAY-002"]}
83 JUNO_STATUS_T (*SetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, JUNO_POINTER_T tItem, size_t iIndex);
89 // @{"req": ["REQ-ARRAY-003"]}
90 JUNO_RESULT_POINTER_T (*GetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex);
96 // @{"req": ["REQ-ARRAY-004"]}
97 JUNO_STATUS_T (*RemoveAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex);
98};
99
104{
105 JUNO_ASSERT_EXISTS(ptArrayApi);
107 ptArrayApi &&
108 ptArrayApi->GetAt &&
109 ptArrayApi->SetAt &&
110 ptArrayApi->RemoveAt
111 );
112 return JUNO_STATUS_SUCCESS;
113}
120// @{"req": ["REQ-ARRAY-006", "REQ-ARRAY-007"]}
122{
123 JUNO_ASSERT_EXISTS(ptArray);
125 ptArray->zCapacity
126 );
127 JUNO_STATUS_T tStatus = JunoDs_ArrayApiVerify(ptArray->ptApi);
128 JUNO_ASSERT_SUCCESS(tStatus, return tStatus);
129 return tStatus;
130}
131
140// @{"req": ["REQ-ARRAY-005"]}
141static inline JUNO_STATUS_T JunoDs_ArrayVerifyIndex(const JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
142{
143 JUNO_STATUS_T tStatus = JunoDs_ArrayVerify(ptArray);
144 JUNO_ASSERT_SUCCESS(tStatus, return tStatus);
145 if(iIndex >= ptArray->zCapacity)
146 {
147 tStatus = JUNO_STATUS_OOB_ERROR;
148 JUNO_FAIL_ROOT(tStatus, ptArray, "Index is OOB");
149 }
150 return tStatus;
151}
152
163// @{"req": ["REQ-ARRAY-001", "REQ-ARRAY-007"]}
164static 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)
165{
166 JUNO_ASSERT_EXISTS(ptArray && iCapacity && ptArrayApi);
167 ptArray->ptApi = ptArrayApi;
168 ptArray->zCapacity = iCapacity;
169 ptArray->_pfcnFailureHandler = pfcnFailureHdlr;
170 ptArray->_pvFailureUserData = pvUserData;
171 JUNO_STATUS_T tStatus = JunoDs_ArrayVerify(ptArray);
172 return tStatus;
173}
174
175#ifdef __cplusplus
176}
177#endif
178#endif // JUNO_DS_ARRAY_API_H
179
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:103
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:164
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:121
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:141
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:51
#define JUNO_ASSERT_SUCCESS(tStatus,...)
Execute the provided failure operation(s) if status is not success.
Definition macros.h:89
#define JUNO_MODULE_ROOT(API_T,...)
Implement a module root struct containing ptApi and failure fields.
Definition module.h:129
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:59
#define JUNO_STATUS_OOB_ERROR
Index or pointer was out of bounds.
Definition status.h:93
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Failure handler callback signature.
Definition status.h:110
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:51
#define JUNO_FAIL_ROOT(tStatus, ptMod, pcMessage)
Invoke a module root's failure handler if available.
Definition status.h:156
void JUNO_USER_DATA_T
Opaque user data type for failure callbacks.
Definition status.h:101
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:83
JUNO_RESULT_POINTER_T(* GetAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Get the value at an index.
Definition array_api.h:90
JUNO_STATUS_T(* RemoveAt)(JUNO_DS_ARRAY_ROOT_T *ptArray, size_t iIndex)
Remove a value at an index.
Definition array_api.h:97