Skip to content
Merged
207 changes: 207 additions & 0 deletions include/juno/buff/buff_api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
MIT License

Copyright (c) 2025 Robin A. Onsay

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
*/

/**
This API has been generated by LibJuno:
https://www.robinonsay.com/libjuno/
*/

/**
This header contains the juno_buff_queue library API
@author
*/
#ifndef JUNO_BUFF_QUEUE_API_HPP
#define JUNO_BUFF_QUEUE_API_HPP
#include "juno/macros.h"
#include "juno/status.h"
#include "juno/module.h"
#include "juno/module.hpp"
#include "juno/types.h"
#include "buff_queue_api.h"
#include "buff_stack_api.h"
#include <cstddef>


template<typename T, const size_t N>
struct JUNO_BUFF_QUEUE_IMPL_T JUNO_MODULE_DERIVE(JUNO_BUFF_QUEUE_ROOT_T,
private:
T tArrBuff[N];
public:
static JUNO_RESULT_T<JUNO_BUFF_QUEUE_IMPL_T<T, N>> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData)
{
JUNO_BUFF_QUEUE_IMPL_T<T, N> tNew{};
tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler;
tNew.tRoot._pvFailurUserData = pvFailureUserData;
tNew.tRoot.iStartIndex = 0;
tNew.tRoot.zCapacity = N;
tNew.tRoot.zLength = 0;
return JUNO_RESULT_T<JUNO_BUFF_QUEUE_IMPL_T<T, N>>{JUNO_STATUS_SUCCESS, tNew};
}

JUNO_RESULT_T<T> Dequeue()
{
JUNO_RESULT_T<T> tResult{};
auto iIndexResult = JunoBuff_QueueDequeue(&tRoot);
tResult.tStatus = iIndexResult.tStatus;
ASSERT_SUCCESS(iIndexResult.tStatus, return tResult);
tResult.tSuccess = tArrBuff[iIndexResult.tSuccess];
return tResult;
}
JUNO_STATUS_T EnqueueByRef(T& tData)
{
auto iIndexResult = JunoBuff_QueueEnqueue(&tRoot);
ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus);
tArrBuff[iIndexResult.tSuccess] = tData;
return JUNO_STATUS_SUCCESS;
}
JUNO_STATUS_T EnqueueByCopy(T tData)
{
auto iIndexResult = JunoBuff_QueueEnqueue(&tRoot);
ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus);
tArrBuff[iIndexResult.tSuccess] = tData;
return JUNO_STATUS_SUCCESS;
}
JUNO_RESULT_T<T&> Peek()
{
JUNO_RESULT_T<T> tResult{};
auto iIndexResult = JunoBuff_QueueGetIndex(&tRoot);
tResult.tStatus = iIndexResult.tStatus;
ASSERT_SUCCESS(iIndexResult.tStatus, return tResult);
tResult.tSuccess = tArrBuff[iIndexResult.tSuccess];
return tResult;
}
);

template<typename T, const size_t N, typename Q = JUNO_BUFF_QUEUE_IMPL_T<T, N>>
struct JUNO_BUFF_QUEUE_T JUNO_MODULE_DERIVE(JUNO_BUFF_QUEUE_ROOT_T,
private:
Q tQueueImpl;
public:
static JUNO_RESULT_T<JUNO_BUFF_QUEUE_T<T,N,Q>> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData)
{
JUNO_BUFF_QUEUE_T<T,N,Q> tNew{};
auto tResult = JUNO_RESULT_T<JUNO_BUFF_QUEUE_T<T,N,Q>>{JUNO_STATUS_SUCCESS, tNew};
JUNO_RESULT_T<Q> tQResult = Q::New(pfcnFailureHandler, pvFailureUserData);
ASSERT_SUCCESS(tQResult.tStatus, tResult.tStatus = tQResult.tStatus; return tResult);
tNew.tQueueImpl = tQResult.tSuccess;
return tResult;
}

JUNO_RESULT_T<T> Dequeue()
{
return tQueueImpl.Dequeue();
}
JUNO_STATUS_T Enqueue(T& tData)
{
return tQueueImpl.EnqueueByRef(tData);
}
JUNO_STATUS_T Enqueue(T tData)
{
return tQueueImpl.EnqueueByCopy(tData);
}
JUNO_RESULT_T<T&> Peek()
{
return tQueueImpl.Peek();
}

);

template<typename T, const size_t N>
struct JUNO_BUFF_STACK_IMPL_T JUNO_MODULE_DERIVE(JUNO_BUFF_STACK_ROOT_T,
private:
T tArrBuff[N];
public:
static JUNO_RESULT_T<JUNO_BUFF_STACK_IMPL_T<T, N>> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData)
{
JUNO_BUFF_STACK_IMPL_T<T, N> tNew{};
tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler;
tNew.tRoot._pvFailurUserData = pvFailureUserData;
tNew.tRoot.zCapacity = N;
tNew.tRoot.zLength = 0;
return JUNO_RESULT_T<JUNO_BUFF_STACK_IMPL_T<T, N>>{JUNO_STATUS_SUCCESS, tNew};
}

JUNO_RESULT_T<T> Pop()
{
JUNO_RESULT_T<T> tResult{};
auto iIndexResult = JunoBuff_StackPop(&tRoot);
tResult.tStatus = iIndexResult.tStatus;
ASSERT_SUCCESS(iIndexResult.tStatus, return tResult);
tResult.tSuccess = tArrBuff[iIndexResult.tSuccess];
return tResult;
}
JUNO_STATUS_T PushByRef(T& tData)
{
auto iIndexResult = JunoBuff_StackPush(&tRoot);
ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus);
tArrBuff[iIndexResult.tSuccess] = tData;
return JUNO_STATUS_SUCCESS;
}
JUNO_STATUS_T PushByCopy(T tData)
{
auto iIndexResult = JunoBuff_StackPush(&tRoot);
ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus);
tArrBuff[iIndexResult.tSuccess] = tData;
return JUNO_STATUS_SUCCESS;
}
JUNO_RESULT_T<T&> Peek()
{
JUNO_RESULT_T<T> tResult{};
auto iIndexResult = JunoBuff_StackPeek(&tRoot);
tResult.tStatus = iIndexResult.tStatus;
ASSERT_SUCCESS(iIndexResult.tStatus, return tResult);
tResult.tSuccess = tArrBuff[iIndexResult.tSuccess];
return tResult;
}
);

template<typename T, const size_t N, typename S = JUNO_BUFF_STACK_IMPL_T<T, N>>
struct JUNO_BUFF_STACK_T JUNO_MODULE_DERIVE(JUNO_BUFF_STACK_ROOT_T,
private:
S tStackImpl;
public:
static JUNO_RESULT_T<JUNO_BUFF_STACK_T<T, N>> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData)
{
JUNO_BUFF_STACK_T<T,N,S> tNew{};
auto tResult = JUNO_RESULT_T<JUNO_BUFF_STACK_T<T,N,S>>{JUNO_STATUS_SUCCESS, tNew};
JUNO_RESULT_T<S> tQResult = S::New(pfcnFailureHandler, pvFailureUserData);
ASSERT_SUCCESS(tQResult.tStatus, tResult.tStatus = tQResult.tStatus; return tResult);
tNew.tStackImpl = tQResult.tSuccess;
return tResult;
}

JUNO_RESULT_T<T> Pop()
{
return tStackImpl.Pop();
}
JUNO_STATUS_T Push(T& tData)
{
return tStackImpl.PushByRef(tData);
}
JUNO_STATUS_T Push(T tData)
{
return tStackImpl.PushByCopy(tData);

}
JUNO_RESULT_T<T&> Peek()
{
return tStackImpl.Peek;
}
);

#endif // JUNO_BUFF_QUEUE_API_H

7 changes: 2 additions & 5 deletions include/juno/buff/buff_queue_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#ifndef JUNO_BUFF_QUEUE_API_H
#define JUNO_BUFF_QUEUE_API_H
#include "juno/macros.h"
#include "juno/memory/memory_api.h"
#include "juno/status.h"
#include "juno/module.h"
#include "juno/types.h"
Expand All @@ -37,11 +36,9 @@ extern "C"
#endif

typedef struct JUNO_BUFF_QUEUE_API_TAG JUNO_BUFF_QUEUE_API_T;
typedef struct JUNO_BUFF_QUEUE_TAG JUNO_BUFF_QUEUE_ROOT_T;
typedef struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_BUFF_QUEUE_ROOT_T;

JUNO_MODULE_DECLARE(JUNO_BUFF_QUEUE_T);

struct JUNO_BUFF_QUEUE_TAG JUNO_MODULE_ROOT(JUNO_BUFF_QUEUE_ROOT_T,
struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_MODULE_ROOT(JUNO_BUFF_QUEUE_API_T,
size_t iStartIndex;
size_t zLength;
size_t zCapacity;
Expand Down
2 changes: 0 additions & 2 deletions include/juno/buff/buff_stack_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ extern "C"
typedef struct JUNO_BUFF_STACK_API_TAG JUNO_BUFF_STACK_API_T;
typedef struct JUNO_BUFF_STACK_ROOT_TAG JUNO_BUFF_STACK_ROOT_T;

JUNO_MODULE_DECLARE(JUNO_BUFF_STACK_T);

struct JUNO_BUFF_STACK_ROOT_TAG JUNO_MODULE_ROOT(JUNO_BUFF_STACK_API_T,
size_t zLength;
size_t zCapacity;
Expand Down
12 changes: 12 additions & 0 deletions include/juno/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ typedef struct NAME_T \
SUCCESS_T tSuccess; \
} NAME_T

#define JUNO_RESULT(SUCCESS_T) \
{ \
JUNO_STATUS_T tStatus; \
SUCCESS_T tSuccess; \
}

/**
* @def JUNO_MODULE_OPTION(NAME_T, SUCCESS_T)
* @brief Defines an option type combining a flag to indicate some and a success payload.
Expand All @@ -233,4 +239,10 @@ typedef struct NAME_T \
SOME_T tSome; \
} NAME_T;

#define JUNO_OPTION(SOME_T) \
{ \
bool bIsSome; \
SOME_T tSome; \
}

#endif // JUNO_MODULE_H
38 changes: 38 additions & 0 deletions include/juno/module.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
MIT License

Copyright (c) 2025 Robin A. Onsay

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
*/
#ifndef JUNO_MODULE_HPP
#define JUNO_MODULE_HPP

#include "status.h"
#include <stdint.h>
#include "module.h"

template<typename T>
struct JUNO_RESULT_T
{
JUNO_STATUS_T tStatus;
T tSuccess;
};

template<typename T>
struct JUNO_OPTION_T
{
bool bIsSome;
T tSome;
};

#endif // JUNO_MODULE_H
2 changes: 2 additions & 0 deletions tests/test_buff.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ static void test_queue(void)
TEST_ASSERT_EQUAL(iTruth, iTestQueue[iIndex]);
iTestQueue[iIndex] = 0;
}
tResult = JunoBuff_QueueDequeue(&tQueue);
TEST_ASSERT_NOT_EQUAL(JUNO_STATUS_SUCCESS, tResult.tStatus);
}

static void test_stack(void)
Expand Down
71 changes: 71 additions & 0 deletions tests/test_buff_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "juno/status.h"
#include "unity.h"
#include "unity_internals.h"
#include "juno/buff/buff_api.hpp"
#include <cstddef>
#include <cstdint>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>


void setUp(void)
{

}
void tearDown(void)
{

}



static void test_queue(void)
{
using BUFF_T = JUNO_BUFF_QUEUE_T<uint32_t, 10>;
auto tTestQueueResult = BUFF_T::New(NULL, NULL);
TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tTestQueueResult.tStatus);
auto tTestQueue = tTestQueueResult.tSuccess;
for(size_t i = 0; i < tTestQueue.tRoot.zCapacity; i++)
{
auto tStatus = tTestQueue.Enqueue(i+1);
TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tStatus);
}
auto tStatus = tTestQueue.Enqueue(11);
TEST_ASSERT_NOT_EQUAL(JUNO_STATUS_SUCCESS, tStatus);
for(size_t i = 0; i < tTestQueue.tRoot.zCapacity; i++)
{
auto tResult = tTestQueue.Dequeue();
TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tResult.tStatus);
TEST_ASSERT_EQUAL(i+1, tResult.tSuccess);
}
}

static void test_stack(void)
{
using STACK_T = JUNO_BUFF_STACK_T<uint32_t, 10>;
auto tTestQueueResult = STACK_T::New(NULL, NULL);
TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tTestQueueResult.tStatus);
auto tTestStack = tTestQueueResult.tSuccess;
for(size_t i = 0; i < tTestStack.tRoot.zCapacity; i++)
{
auto tStatus = tTestStack.Push(i+1);
TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tStatus);
}
auto tStatus = tTestStack.Push(11);
TEST_ASSERT_NOT_EQUAL(JUNO_STATUS_SUCCESS, tStatus);
for(size_t i = 0; i < tTestStack.tRoot.zCapacity; i++)
{
auto tResult = tTestStack.Pop();
TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tResult.tStatus);
TEST_ASSERT_EQUAL(tTestStack.tRoot.zCapacity - i, tResult.tSuccess);
}
}

int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_queue);
RUN_TEST(test_stack);
return UNITY_END();
}
Loading