InfiniTime/src/StaticStack.h
Riku Isokoski 5ef3c0d3be Implement a return app stack
Each opened app (screen) is pushed on a stack, which is then popped from
when returning instead of hard coded "previous apps". Return swipe and
refresh directions are automatically determined from the app transition.
2023-01-05 15:13:05 +01:00

39 lines
882 B
C++

#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];
}