Bash++
Bash++ compiler internal documentation
SensibleStack.h
Go to the documentation of this file.
1 
6 #ifndef SRC_SENSIBLESTACK_H_
7 #define SRC_SENSIBLESTACK_H_
8 
9 #include <stack>
10 #include <string>
11 #include <type_traits>
12 
20 template <typename T, typename = std::enable_if<std::is_arithmetic_v<T>>>
21 class SensibleStack : public std::stack<T> {
22  public:
23  SensibleStack() : std::stack<T>() {}
24  static const T zero = T(0);
25 
26  T top() {
27  if (this->empty()) {
28  return zero;
29  }
30  return std::stack<T>::top();
31  }
32 
33  inline void pop() {
34  if (!this->empty()) {
35  std::stack<T>::pop();
36  }
37  }
38 };
39 
45 class SensibleStringStack : public std::stack<std::string> {
46  public:
47  SensibleStringStack() : std::stack<std::string>() {}
48 
49  std::string top() {
50  if (this->empty()) {
51  return "";
52  }
53  return std::stack<std::string>::top();
54  }
55 
56  inline void pop() {
57  if (!this->empty()) {
58  std::stack<std::string>::pop();
59  }
60  }
61 };
62 
63 #endif // SRC_SENSIBLESTACK_H_
A stack that returns zero if empty.
Definition: SensibleStack.h:21
void pop()
Definition: SensibleStack.h:33
SensibleStack()
Definition: SensibleStack.h:23
static const T zero
Definition: SensibleStack.h:24
T top()
Definition: SensibleStack.h:26
A stack that returns an empty string if empty.
Definition: SensibleStack.h:45
std::string top()
Definition: SensibleStack.h:49
void pop()
Definition: SensibleStack.h:56
SensibleStringStack()
Definition: SensibleStack.h:47