GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/mstack.h Lines: 15 17 88.2 %
Date: 2021-03-17 Branches: 15 50 30.0 %

Line Branch Exec Source
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
198
    constexpr2 explicit MStack(const size_t maxSize) :
33

190476
        mStack(new T[maxSize]),
34
        mMaxSize(maxSize),
35
198
        mPointer(mStack - 1),
36
        mStartPointer(mStack - 1),
37
594
        mEndPointer(mStack + maxSize - 1)
38
    {
39
198
    }
40
41
198
    ~MStack()
42
    {
43
198
        delete [] mStack;
44
198
    }
45
46
    A_DELETE_COPY(MStack)
47
48
    T &push()
49
    {
50





582
        if (mPointer == mEndPointer)
51
        {
52
            logger->log("error: max stack size reached");
53
            return *mPointer;
54
        }
55
582
        return *(++mPointer);
56
    }
57
58
    T &getPop()
59
    {
60
1
        return *(mPointer--);
61
    }
62
63
    const T &getPopConst()
64
    {
65
        return *(mPointer--);
66
    }
67
68
    void pop()
69
    {
70
573
        mPointer --;
71
    }
72
73
245
    T &top() const
74
    {
75
245
        return *mPointer;
76
    }
77
78
    const T &topConst() const
79
    {
80
        return *mPointer;
81
    }
82
83
    void clear()
84
    {
85
1
        mPointer = mStack - 1;
86
    }
87
88
    bool empty() const
89
    {
90
        return mPointer == mStartPointer;
91
    }
92
93
    T *mStack;
94
95
    size_t mMaxSize;
96
    T *mPointer;
97
    const T *const mStartPointer;
98
    const T *const mEndPointer;
99
};
100
101
#endif  // RESOURCES_MSTACK_H