libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstddef>
14#include <stack>
15#include <stdexcept>
16#include <promeki/namespace.h>
17
18PROMEKI_NAMESPACE_BEGIN
19
36template <typename T> class Stack {
37 public:
39 Stack() = default;
40
42 Stack(const Stack &other) : d(other.d) {}
43
45 Stack(Stack &&other) noexcept : d(std::move(other.d)) {}
46
48 ~Stack() = default;
49
51 Stack &operator=(const Stack &other) {
52 d = other.d;
53 return *this;
54 }
55
57 Stack &operator=(Stack &&other) noexcept {
58 d = std::move(other.d);
59 return *this;
60 }
61
62 // -- Capacity --
63
65 bool isEmpty() const { return d.empty(); }
66
68 size_t size() const { return d.size(); }
69
70 // -- Access --
71
73 T &top() { return d.top(); }
74
76 const T &top() const { return d.top(); }
77
79 const T &constTop() const { return d.top(); }
80
81 // -- Modifiers --
82
87 void push(const T &value) {
88 d.push(value);
89 return;
90 }
91
96 void push(T &&value) {
97 d.push(std::move(value));
98 return;
99 }
100
106 T pop() {
107 if (d.empty()) throw std::logic_error("Stack::pop on empty stack");
108 T val = std::move(d.top());
109 d.pop();
110 return val;
111 }
112
114 void clear() {
115 while (!d.empty()) d.pop();
116 return;
117 }
118
123 void swap(Stack &other) noexcept {
124 d.swap(other.d);
125 return;
126 }
127
128 // -- Comparison --
129
131 friend bool operator==(const Stack &lhs, const Stack &rhs) { return lhs.d == rhs.d; }
132
134 friend bool operator!=(const Stack &lhs, const Stack &rhs) { return lhs.d != rhs.d; }
135
136 private:
137 std::stack<T> d;
138};
139
140PROMEKI_NAMESPACE_END
141
142#endif // PROMEKI_ENABLE_CORE