1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2004  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
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, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#ifndef LOCKEDARRAY_H
23
#define LOCKEDARRAY_H
24
25
#include <algorithm>
26
27
/**
28
 * A _very_ basic array class that allows simple iteration and jumps, keeping
29
 * its currently selected entry and providing a mechanism to lock this
30
 * position. Anyone can unlock it though, so its your job to use it the right
31
 * way ;)
32
 */
33
34
template<class T>
35
class LockedArray
36
{
37
    public:
38
        LockedArray(unsigned int size);
39
        ~LockedArray();
40
41
        void lock() { mLocked = true; };
42
        void unlock() { mLocked = false; };
43
44
        bool isLocked() const { return mLocked; };
45
46
        T getEntry() const { return mData[mCurEntry]; };
47
        void setEntry(T entry) { mData[mCurEntry] = entry; mFilled = true; };
48
49
        void next();
50
        void prev();
51
        void select(unsigned int pos);
52
        unsigned int getPos() const { return mCurEntry; }
53
54
        unsigned int getSize() const { return mSize; };
55
56
        /**
57
         * Clears the array without changing size or data type
58
         */
59
        void clear();
60
61
    protected:
62
        unsigned int mSize;
63
64
        T* mData;
65
66
        unsigned int mCurEntry;
67
        bool mLocked;
68
69
        bool mFilled;
70
};
71
72
template<class T>
73
LockedArray<T>::LockedArray(unsigned int size):
74
    mSize(size), mData(new T[size]), mCurEntry(0), mLocked(false),
75
    mFilled(false)
76
{
77
    std::fill_n(mData, mSize, (T)0);
78
}
79
80
template<class T>
81
LockedArray<T>::~LockedArray()
82
{
83
    delete [] mData;
84
}
85
86
template<class T>
87
void LockedArray<T>::next()
88
{
89
    if (mLocked)
90
        return;
91
92
    if (++mCurEntry == mSize)
93
        mCurEntry = 0;
94
}
95
96
template<class T>
97
void LockedArray<T>::prev()
98
{
99
    if (mLocked)
100
        return;
101
102
    mCurEntry = mCurEntry ? (--mCurEntry) : (mSize - 1);
103
}
104
105
template<class T>
106
void LockedArray<T>::select(unsigned int pos)
107
{
108
    if (mLocked)
109
        return;
110
111
    mCurEntry = pos;
112
    if (mCurEntry >= mSize)
113
        mCurEntry = 0;
114
}
115
116
template<class T>
117
void LockedArray<T>::clear()
118
{
119
    if (!mFilled) return;
120
121
    delete [] mData;
122
123
    mData = new T[mSize];
124
125
    std::fill_n(mData, mSize, (T)0);
126
127
    mCurEntry = 0;
128
129
    mLocked = false;
130
}
131
#endif