OpenBarnyard
 
Loading...
Searching...
No Matches
TStack.h
Go to the documentation of this file.
1#pragma once
2
4
5template <typename T, TINT C>
6class TStack
7{
8public:
9 TStack() = default;
10 ~TStack() = default;
11
12 void Push( const T& a_item = T() )
13 {
14 m_iTop++;
15 if ( IsFull() ) return;
16 AtUnsafe( m_iTop ) = a_item;
17 }
18
20 {
21 m_iTop++;
22 return Top();
23 }
24
25 T& Pop()
26 {
27 T& item = AtUnsafe( m_iTop );
28 m_iTop--;
29 if ( IsEmpty() ) m_iTop = 0;
30 return item;
31 }
32
33 TBOOL IsFull() const
34 {
35 return m_iTop == ( C - 1 );
36 }
37
38 T& Top()
39 {
40 return AtUnsafe( m_iTop );
41 }
42
43 TBOOL IsEmpty() const
44 {
45 return m_iTop == -1;
46 }
47
48 void Reset()
49 {
50 m_iTop = -1;
51 }
52
53 TINT Count() const
54 {
55 return m_iTop + 1;
56 }
57
58 T& Begin()
59 {
60 return AtUnsafe( 0 );
61 }
62
63 const T& Begin() const
64 {
65 return AtUnsafe( 0 );
66 }
67
68 T& End()
69 {
70 return AtUnsafe( m_iTop );
71 }
72
73 const T& End() const
74 {
75 return AtUnsafe( m_iTop );
76 }
77
78private:
79 TFORCEINLINE constexpr T& AtUnsafe( TUINT a_uiIndex )
80 {
81 return *( (T*)( m_aBuffer ) + a_uiIndex );
82 }
83
84 TFORCEINLINE constexpr const T& AtUnsafe( TUINT a_uiIndex ) const
85 {
86 return *( (const T*)( m_aBuffer ) + a_uiIndex );
87 }
88
89private:
90 TINT m_iTop = -1;
91 TBYTE m_aBuffer[ sizeof( T ) * C ];
92};
93
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TFORCEINLINE
Definition Defines.h:74
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
unsigned int TUINT
Definition Typedefs.h:8
int TINT
Definition Typedefs.h:7
bool TBOOL
Definition Typedefs.h:6
uint8_t TBYTE
Definition Typedefs.h:19
T & Begin()
Definition TStack.h:58
void Push(const T &a_item=T())
Definition TStack.h:12
T & End()
Definition TStack.h:68
void Reset()
Definition TStack.h:48
const T & Begin() const
Definition TStack.h:63
TINT Count() const
Definition TStack.h:53
~TStack()=default
T & Pop()
Definition TStack.h:25
TBOOL IsFull() const
Definition TStack.h:33
TBOOL IsEmpty() const
Definition TStack.h:43
T & Top()
Definition TStack.h:38
T & PushNull()
Definition TStack.h:19
const T & End() const
Definition TStack.h:73
TStack()=default