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

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2015-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef RESOURCES_MSTACK_H
22
#define RESOURCES_MSTACK_H
23
24
#include "logger.h"
25
26
#include "localconsts.h"
27
28
template<typename T>
29
struct MStack final
30
{
31
372
    constexpr2 explicit MStack(const size_t maxSize) :
32

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





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