ManaPlus
mstack.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2015-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef RESOURCES_MSTACK_H
23 #define RESOURCES_MSTACK_H
24 
25 #include "logger.h"
26 
27 #include "localconsts.h"
28 
29 template<typename T>
30 struct MStack final
31 {
32  constexpr2 explicit MStack(const size_t maxSize) :
33  mStack(new T[maxSize]),
34  mMaxSize(maxSize),
35  mPointer(mStack - 1),
36  mStartPointer(mStack - 1),
37  mEndPointer(mStack + maxSize - 1)
38  {
39  }
40 
42  {
43  delete [] mStack;
44  }
45 
47 
48  T &push()
49  {
50  if (mPointer == mEndPointer)
51  {
52  logger->log("error: max stack size reached");
53  return *mPointer;
54  }
55  return *(++mPointer);
56  }
57 
58  T &getPop()
59  {
60  return *(mPointer--);
61  }
62 
63  const T &getPopConst()
64  {
65  return *(mPointer--);
66  }
67 
68  void pop()
69  {
70  mPointer --;
71  }
72 
73  T &top() const
74  {
75  return *mPointer;
76  }
77 
78  const T &topConst() const
79  {
80  return *mPointer;
81  }
82 
83  void clear()
84  {
85  mPointer = mStack - 1;
86  }
87 
88  bool empty() const
89  {
90  return mPointer == mStartPointer;
91  }
92 
94 
95  size_t mMaxSize;
97  const T *const mStartPointer;
98  const T *const mEndPointer;
99 };
100 
101 #endif // RESOURCES_MSTACK_H
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define new
Definition: debug_new.h:147
#define constexpr2
Definition: localconsts.h:49
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
Logger * logger
Definition: logger.cpp:89
Definition: mstack.h:31
void clear()
Definition: mstack.h:83
const T & topConst() const
Definition: mstack.h:78
T & getPop()
Definition: mstack.h:58
~MStack()
Definition: mstack.h:41
bool empty() const
Definition: mstack.h:88
const T *const mStartPointer
Definition: mstack.h:97
T * mPointer
Definition: mstack.h:96
void pop()
Definition: mstack.h:68
const T *const mEndPointer
Definition: mstack.h:98
size_t mMaxSize
Definition: mstack.h:95
MStack(const size_t maxSize)
Definition: mstack.h:32
T & top() const
Definition: mstack.h:73
const T & getPopConst()
Definition: mstack.h:63
T & push()
Definition: mstack.h:48
T * mStack
Definition: mstack.h:93