staticstack: Move to src/utility

This commit is contained in:
Finlay Davidson 2023-03-16 21:49:44 +01:00 committed by Riku Isokoski
parent 2ba8b17967
commit 47931f41d5
3 changed files with 50 additions and 46 deletions

View File

@ -1,43 +0,0 @@
#include <array>
#include <cstddef>
template <typename T, size_t N>
class StaticStack {
public:
T Pop();
void Push(T element);
void Reset();
T Top();
private:
std::array<T, N> elementArray;
// Number of elements in stack, points to the next empty slot
size_t stackPointer = 0;
};
// Returns random data when popping from empty array.
template <typename T, size_t N>
T StaticStack<T, N>::Pop() {
if (stackPointer > 0) {
stackPointer--;
}
return elementArray[stackPointer];
}
template <typename T, size_t N>
void StaticStack<T, N>::Push(T element) {
if (stackPointer < elementArray.size()) {
elementArray[stackPointer] = element;
stackPointer++;
}
}
template <typename T, size_t N>
void StaticStack<T, N>::Reset() {
stackPointer = 0;
}
template <typename T, size_t N>
T StaticStack<T, N>::Top() {
return elementArray[stackPointer - 1];
}

View File

@ -19,7 +19,7 @@
#include "displayapp/Messages.h"
#include "BootErrors.h"
#include "StaticStack.h"
#include "utility/StaticStack.h"
namespace Pinetime {
@ -126,8 +126,8 @@ namespace Pinetime {
void ApplyBrightness();
static constexpr size_t returnAppStackSize = 10;
StaticStack<Apps, returnAppStackSize> returnAppStack;
StaticStack<FullRefreshDirections, returnAppStackSize> appStackDirections;
Utility::StaticStack<Apps, returnAppStackSize> returnAppStack;
Utility::StaticStack<FullRefreshDirections, returnAppStackSize> appStackDirections;
bool isDimmed = false;
};

47
src/utility/StaticStack.h Normal file
View File

@ -0,0 +1,47 @@
#include <array>
#include <cstddef>
namespace Pinetime {
namespace Utility {
template <typename T, size_t N>
class StaticStack {
public:
T Pop();
void Push(T element);
void Reset();
T Top();
private:
std::array<T, N> elementArray;
// Number of elements in stack, points to the next empty slot
size_t stackPointer = 0;
};
// Returns random data when popping from empty array.
template <typename T, size_t N>
T StaticStack<T, N>::Pop() {
if (stackPointer > 0) {
stackPointer--;
}
return elementArray[stackPointer];
}
template <typename T, size_t N>
void StaticStack<T, N>::Push(T element) {
if (stackPointer < elementArray.size()) {
elementArray[stackPointer] = element;
stackPointer++;
}
}
template <typename T, size_t N>
void StaticStack<T, N>::Reset() {
stackPointer = 0;
}
template <typename T, size_t N>
T StaticStack<T, N>::Top() {
return elementArray[stackPointer - 1];
}
}
}