From 5dae8f8d10d506abc3c75a1f66c1dfe620c84fc1 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 15 Feb 2022 20:27:51 +0100 Subject: refactor: improve project design --- src/app/stack.tpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/app/stack.tpp (limited to 'src/app/stack.tpp') diff --git a/src/app/stack.tpp b/src/app/stack.tpp new file mode 100644 index 0000000..bcdafc0 --- /dev/null +++ b/src/app/stack.tpp @@ -0,0 +1,45 @@ +#pragma once + +#include "stack.hpp" + +#include +#include + +template +Stack::Stack(uint64_t capacity) +{ + _items.reserve(capacity); +} + +template +void Stack::push(Item item) +{ + if (_items.size() == _items.capacity()) + { + throw std::overflow_error("Tried to push when stack is full"); + } + + _items.push_back(item); +} + +template +void Stack::pop() +{ + if (_items.empty()) + { + throw std::underflow_error("Tried to pop when stack size is 0"); + } + + _items.pop_back(); +} + +template +Item Stack::peek() +{ + if (_items.empty()) + { + throw std::underflow_error("Tried to peek when stack size is 0"); + } + + return _items.back(); +} -- cgit v1.2.3-18-g5258