1//============================================================================
2//
3// SSSS tt lll lll
4// SS SS tt ll ll
5// SS tttttt eeee ll ll aaaa
6// SSSS tt ee ee ll ll aa
7// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8// SS SS tt ee ll ll aa aa
9// SSSS ttt eeeee llll llll aaaaa
10//
11// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
12// and the Stella Team
13//
14// See the file "License.txt" for information on usage and redistribution of
15// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16//============================================================================
17
18#ifndef STACK_HXX
19#define STACK_HXX
20
21#include <functional>
22
23#include "bspf.hxx"
24
25/**
26 * Simple fixed size stack class.
27 */
28namespace Common {
29
30template <class T, uInt32 CAPACITY = 50>
31class FixedStack
32{
33 private:
34 std::array<T, CAPACITY> _stack;
35 uInt32 _size;
36
37 public:
38 using StackFunction = std::function<void(T&)>;
39
40 FixedStack<T, CAPACITY>() : _size(0) { }
41
42 bool empty() const { return _size <= 0; }
43 bool full() const { return _size >= CAPACITY; }
44
45 T top() const { return _stack[_size - 1]; }
46 T get(uInt32 pos) { return _stack[pos]; }
47 void push(const T& x) { _stack[_size++] = x; }
48 T pop() { return std::move(_stack[--_size]); }
49 uInt32 size() const { return _size; }
50
51 // Reverse the contents of the stack
52 // This operation isn't needed very often, but it's handy to have
53 void reverse() {
54 for(uInt32 i = 0, j = _size - 1; i < j; ++i, --j)
55 std::swap(_stack[i], _stack[j]);
56 }
57
58 // Apply the given function to every item in the stack
59 // We do it this way so the stack API can be preserved,
60 // and no access to individual elements is allowed outside
61 // the class.
62 void applyAll(const StackFunction& func) {
63 for(uInt32 i = 0; i < _size; ++i)
64 func(_stack[i]);
65 }
66
67 friend ostream& operator<<(ostream& os, const FixedStack<T>& s) {
68 for(uInt32 pos = 0; pos < s._size; ++pos)
69 os << s._stack[pos] << " ";
70 return os;
71 }
72
73 private:
74 // Following constructors and assignment operators not supported
75 FixedStack(const FixedStack&) = delete;
76 FixedStack(FixedStack&&) = delete;
77 FixedStack& operator=(const FixedStack&) = delete;
78 FixedStack& operator=(FixedStack&&) = delete;
79};
80
81} // Namespace Common
82
83#endif
84