LibJuno 0.42.0
LibJuno is a lightweight C99 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_BUFF_HPP
28#define JUNO_BUFF_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_T<T, N>& tQueue, T tData);
40template<typename T, const size_t N>
41RESULT_T<T> Dequeue(QUEUE_T<T, N>& tQueue);
42template<typename T, const size_t N>
43RESULT_T<T*> QueuePeek(QUEUE_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
63#ifndef JUNO_QUEUE_CUSTOM
64
65template<typename T, const size_t N>
68);
69
70#endif
71
72template<typename T, const size_t N>
74{
75 auto& tJunoQueue = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
77 if(tJunoQueue.tRoot.zLength > 0)
78 {
79 tResult.tOk = tJunoQueue.tRoot.tArrBuff.tArr[tJunoQueue.tRoot.iStartIndex];
80 tJunoQueue.tRoot.iStartIndex = (tJunoQueue.tRoot.iStartIndex + 1) % N;
81 tJunoQueue.tRoot.zLength -= 1;
82 return tResult;
83 }
85 JUNO_FAIL(tResult.tStatus, tJunoQueue.tRoot._pfcnFailureHandler, tJunoQueue.tRoot._pvFailureUserData, "Queue is empty");
86 return tResult;
87}
88
89template<typename T, const size_t N>
91{
92 auto& tJunoQueue = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
93 if(tJunoQueue.tRoot.zLength < N)
94 {
95 tJunoQueue.tRoot.tArrBuff.tArr[(tJunoQueue.tRoot.iStartIndex + tJunoQueue.tRoot.zLength) % N] = tData;
96 tJunoQueue.tRoot.zLength += 1;
98 }
99 JUNO_FAIL(JUNO_STATUS_INVALID_SIZE_ERROR, tJunoQueue.tRoot._pfcnFailureHandler, tJunoQueue.tRoot._pvFailureUserData, "Queue is full");
101}
102
103template<typename T, const size_t N>
105{
106 auto& tJunoQueue = reinterpret_cast<JUNO_QUEUE_T<T, N>&>(tQueue);
108 if(tJunoQueue.tRoot.zLength > 0)
109 {
110 tResult.tOk = &tJunoQueue.tRoot.tArrBuff.tArr[tJunoQueue.tRoot.iStartIndex];
111 return tResult;
112 }
114 JUNO_FAIL(tResult.tStatus, tJunoQueue.tRoot._pfcnFailureHandler, tJunoQueue.tRoot._pvFailureUserData, "Queue is empty");
115 return tResult;
116}
117
118template<typename T, const size_t N>
120template<typename T, const size_t N>
122template<typename T, const size_t N>
124
125template<typename T, const size_t N>
128 {
129 auto& tNew = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
130 tNew.tRoot.ptApi = &tApi;
131 tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler;
132 tNew.tRoot._pvFailureUserData = pvFailureUserData;
133 tNew.tRoot.zLength = 0;
134 return JUNO_STATUS_SUCCESS;
135 }
136
137 static constexpr STACK_API_T<T, N> NewApi()
138 {
140 }
141
142);
143
144#ifndef JUNO_STACK_CUSTOM
145
146template<typename T, const size_t N>
149);
150
151#endif
152
153template<typename T, const size_t N>
155{
156 auto& tJunoStack = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
158 if(tJunoStack.tRoot.zLength > 0)
159 {
160 tJunoStack.tRoot.zLength -= 1;
161 tResult.tOk = tJunoStack.tRoot.tArrBuff.tArr[tJunoStack.tRoot.zLength];
162 return tResult;
163 }
165 JUNO_FAIL(tResult.tStatus, tJunoStack.tRoot._pfcnFailureHandler, tJunoStack.tRoot._pvFailureUserData, "Stack is empty");
166 return tResult;
167}
168
169template<typename T, const size_t N>
171{
172 auto& tJunoStack = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
173 if(tJunoStack.tRoot.zLength < N)
174 {
175 tJunoStack.tRoot.tArrBuff.tArr[tJunoStack.tRoot.zLength] = tData;
176 tJunoStack.tRoot.zLength += 1;
177 return JUNO_STATUS_SUCCESS;
178 }
179 JUNO_FAIL(JUNO_STATUS_INVALID_SIZE_ERROR, tJunoStack.tRoot._pfcnFailureHandler, tJunoStack.tRoot._pvFailureUserData, "Stack is full");
181}
182
183template<typename T, const size_t N>
185{
186 auto& tJunoStack = reinterpret_cast<JUNO_STACK_T<T, N>&>(tStack);
188 if(tJunoStack.tRoot.zLength > 0)
189 {
190 tResult.tOk = &tJunoStack.tRoot.tArrBuff.tArr[tJunoStack.tRoot.zLength];
191 return tResult;
192 }
194 JUNO_FAIL(tResult.tStatus, tJunoStack.tRoot._pfcnFailureHandler, tJunoStack.tRoot._pvFailureUserData, "Stack is empty");
195 return tResult;
196}
197
198
199
200}
201}
202
203#endif // JUNO_BUFF_QUEUE_API_H
204
#define JUNO_MODULE(API_T, ROOT_T,...)
Definition module.h:161
#define JUNO_MODULE_ARG(...)
Definition module.h:132
#define JUNO_MODULE_DERIVE(ROOT_T,...)
Definition module.h:203
JUNO_STATUS_T Enqueue(QUEUE_T< T, N > &tQueue, T tData)
Definition juno_buff.hpp:90
JUNO_STATUS_T Push(STACK_T< T, N > &tStack, T tData)
Definition juno_buff.hpp:170
RESULT_T< T > Dequeue(QUEUE_T< T, N > &tQueue)
Definition juno_buff.hpp:73
RESULT_T< T * > QueuePeek(QUEUE_T< T, N > &tQueue)
Definition juno_buff.hpp:104
RESULT_T< T > Pop(STACK_T< T, N > &tStack)
Definition juno_buff.hpp:154
RESULT_T< T * > StackPeek(STACK_T< T, N > &tStack)
Definition juno_buff.hpp:184
Definition buff_api.hpp:36
#define JUNO_STATUS_ERR
Definition status.h:25
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Definition status.h:43
int32_t JUNO_STATUS_T
Definition status.h:23
#define JUNO_FAIL(tStatus, pfcnFailureHandler, pvFailureUserData, pcMessage)
Definition status.h:48
#define JUNO_STATUS_INVALID_SIZE_ERROR
Definition status.h:30
#define JUNO_STATUS_SUCCESS
Definition status.h:24
void JUNO_USER_DATA_T
Definition status.h:42
Definition module.hpp:29
JUNO_STATUS_T tStatus
Definition module.hpp:30
T tOk
Definition module.hpp:31
Definition buff_api.hpp:67
The stack buffer api.
Definition buff_api.hpp:95
Definition buff_api.hpp:50
The stack module.
Definition buff_api.hpp:83