libUTL++
Stack.h
1 #pragma once
2 
4 
5 #include <libutl/Array.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
20 
22 template <class T = Object>
23 class Stack : public TArray<T>
24 {
27 
28 public:
33  Stack(bool owner)
34  : TArray<T>(owner)
35  {
36  }
37 
41  T* top() const;
42 
47  T* pop();
48 
54  bool
55  push(const T& object)
56  {
57  return Array::add(object);
58  }
59 
65  void
66  push(const T* object)
67  {
68  Array::add(object);
69  }
70 };
71 
73 
74 template <class T>
75 T*
77 {
78  if (this->_items == 0) return nullptr;
79  auto res = utl::cast<T>(this->last());
80  return res;
81 }
82 
84 
85 template <class T>
86 T*
88 {
89  if (this->_items == 0) return nullptr;
90  auto res = utl::cast<T>(this->last());
91  FlagGuard fgo(this, Collection::flg_owner, false);
92  super::remove(this->_items - 1);
93  return res;
94 }
95 
97 
98 UTL_NS_END;
99 
101 
#define UTL_CLASS_IMPL_TPL(className, T)
Implementation of standard UTL++ functionality for a template class.
Definition: macros.h:906
void push(const T *object)
Push an object onto the stack.
Definition: Stack.h:66
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
void remove(FwdIt &begin, const FwdIt &end, bool cmp=false, const Predicate *pred=nullptr, bool predVal=false)
Remove objects from a sequence.
Flag guard.
Definition: FlagsMI.h:171
Template version of Array.
Definition: TArray.h:25
LIFO (last-in, first-out) data structure.
Definition: Stack.h:23
#define UTL_CLASS_DECL_TPL(DC, T, BC)
Declaration of standard UTL++ functionality for a template class with one parameter.
Definition: macros.h:713
Stack(bool owner)
Constructor.
Definition: Stack.h:33
bool push(const T &object)
Push an object onto the stack.
Definition: Stack.h:55