LibJuno 1.0.1
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
juno_buff.hpp
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
27#ifndef JUNO_DS_HPP
28#define JUNO_DS_HPP
29#include "buff_api.hpp"
30#include "juno/module.h"
31#include "juno/module.hpp"
32#include "juno/status.h"
33
34namespace juno
35{
36namespace buff
37{
38template<typename T, const size_t N>
39JUNO_STATUS_T Enqueue(QUEUE_ROOT_T<T, N>& tQueue, T tData);
40template<typename T, const size_t N>
41RESULT_T<T> Dequeue(QUEUE_ROOT_T<T, N>& tQueue);
42template<typename T, const size_t N>
43RESULT_T<T*> QueuePeek(QUEUE_ROOT_T<T, N>& tQueue);
44
45template<typename T, const size_t N>
48 {
49 auto& tNew = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
50 tNew.tRoot.ptApi = &tApi;
51 tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler;
52 tNew.tRoot._pvFailureUserData = pvFailureUserData;
53 tNew.tRoot.iStartIndex = 0;
54 tNew.tRoot.zLength = 0;
56 }
57 static constexpr QUEUE_API_T<T, N> NewApi()
58 {
60 }
61);
62
63template<typename T, const size_t N>
65{
66 auto& tJunoQueue = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
68 if(tJunoQueue.tRoot.zLength > 0)
69 {
70 tResult.tOk = tJunoQueue.tRoot.tArrBuff.tArr[tJunoQueue.tRoot.iStartIndex];
71 tJunoQueue.tRoot.iStartIndex = (tJunoQueue.tRoot.iStartIndex + 1) % N;
72 tJunoQueue.tRoot.zLength -= 1;
73 return tResult;
74 }
76 JUNO_FAIL(tResult.tStatus, tJunoQueue.tRoot._pfcnFailureHandler, tJunoQueue.tRoot._pvFailureUserData, "Queue is empty");
77 return tResult;
78}
79
80template<typename T, const size_t N>
82{
83 auto& tJunoQueue = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
84 if(tJunoQueue.tRoot.zLength < N)
85 {
86 tJunoQueue.tRoot.tArrBuff.tArr[(tJunoQueue.tRoot.iStartIndex + tJunoQueue.tRoot.zLength) % N] = tData;
87 tJunoQueue.tRoot.zLength += 1;
89 }
90 JUNO_FAIL(JUNO_STATUS_INVALID_SIZE_ERROR, tJunoQueue.tRoot._pfcnFailureHandler, tJunoQueue.tRoot._pvFailureUserData, "Queue is full");
92}
93
94template<typename T, const size_t N>
96{
97 auto& tJunoQueue = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
99 if(tJunoQueue.tRoot.zLength > 0)
100 {
101 tResult.tOk = &tJunoQueue.tRoot.tArrBuff.tArr[tJunoQueue.tRoot.iStartIndex];
102 return tResult;
103 }
105 JUNO_FAIL(tResult.tStatus, tJunoQueue.tRoot._pfcnFailureHandler, tJunoQueue.tRoot._pvFailureUserData, "Queue is empty");
106 return tResult;
107}
108
109template<typename T, const size_t N>
111template<typename T, const size_t N>
113template<typename T, const size_t N>
115
116template<typename T, const size_t N>
119 {
120 auto& tNew = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
121 tNew.tRoot.ptApi = &tApi;
122 tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler;
123 tNew.tRoot._pvFailureUserData = pvFailureUserData;
124 tNew.tRoot.zLength = 0;
125 return JUNO_STATUS_SUCCESS;
126 }
127
128 static constexpr STACK_API_T<T, N> NewApi()
129 {
131 }
132
133);
134
135template<typename T, const size_t N>
137{
138 auto& tJunoStack = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
140 if(tJunoStack.tRoot.zLength > 0)
141 {
142 tJunoStack.tRoot.zLength -= 1;
143 tResult.tOk = tJunoStack.tRoot.tArrBuff.tArr[tJunoStack.tRoot.zLength];
144 return tResult;
145 }
147 JUNO_FAIL(tResult.tStatus, tJunoStack.tRoot._pfcnFailureHandler, tJunoStack.tRoot._pvFailureUserData, "Stack is empty");
148 return tResult;
149}
150
151template<typename T, const size_t N>
153{
154 auto& tJunoStack = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
155 if(tJunoStack.tRoot.zLength < N)
156 {
157 tJunoStack.tRoot.tArrBuff.tArr[tJunoStack.tRoot.zLength] = tData;
158 tJunoStack.tRoot.zLength += 1;
159 return JUNO_STATUS_SUCCESS;
160 }
161 JUNO_FAIL(JUNO_STATUS_INVALID_SIZE_ERROR, tJunoStack.tRoot._pfcnFailureHandler, tJunoStack.tRoot._pvFailureUserData, "Stack is full");
163}
164
165template<typename T, const size_t N>
167{
168 auto& tJunoStack = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
170 if(tJunoStack.tRoot.zLength > 0)
171 {
172 tResult.tOk = &tJunoStack.tRoot.tArrBuff.tArr[tJunoStack.tRoot.zLength];
173 return tResult;
174 }
176 JUNO_FAIL(tResult.tStatus, tJunoStack.tRoot._pfcnFailureHandler, tJunoStack.tRoot._pvFailureUserData, "Stack is empty");
177 return tResult;
178}
179
180
181
182}
183}
184
185#endif // JUNO_DS_QUEUE_API_H
186
#define JUNO_MODULE_ARG(...)
Pass-through argument pack helper for module macros.
Definition module.h:91
#define JUNO_MODULE_DERIVE(ROOT_T,...)
Implement a derived module embedding the root as the first member.
Definition module.h:157
#define JUNO_STATUS_ERR
Unspecified error.
Definition status.h:58
#define JUNO_STATUS_INVALID_SIZE_ERROR
Provided size or alignment was invalid or unsupported.
Definition status.h:68
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:56
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(tStatus, pfcnFailureHandler, pvFailureUserData, pcMessage)
Invoke a failure handler if provided.
Definition status.h:123
void JUNO_USER_DATA_T
Opaque user data type for failure callbacks.
Definition status.h:96
Module system and dependency injection primitives for LibJuno.
RESULT_T< T > Dequeue(QUEUE_ROOT_T< T, N > &tQueue)
Definition juno_buff.hpp:64
JUNO_STATUS_T Enqueue(QUEUE_ROOT_T< T, N > &tQueue, T tData)
Definition juno_buff.hpp:81
JUNO_STATUS_T Push(STACK_ROOT_T< T, N > &tStack, T tData)
Definition juno_buff.hpp:152
RESULT_T< T * > StackPeek(STACK_ROOT_T< T, N > &tStack)
Definition juno_buff.hpp:166
RESULT_T< T > Pop(STACK_ROOT_T< T, N > &tStack)
Definition juno_buff.hpp:136
RESULT_T< T * > QueuePeek(QUEUE_ROOT_T< T, N > &tQueue)
Definition juno_buff.hpp:95
Definition buff_api.hpp:36
Status codes and failure-handling helpers for LibJuno.
Definition module.hpp:29
JUNO_STATUS_T tStatus
Definition module.hpp:30
T tOk
Definition module.hpp:31