libsidplayfp 1.8.8
array.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright (C) 2011 Leandro Nini
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef ARRAY_H
22#define ARRAY_H
23
27{
28private:
29 unsigned int c;
30
31public:
32 counter() : c(1) {}
33 void increase() { ++c; }
34 unsigned int decrease() { return --c; }
35};
36
40template<typename T>
41class array
42{
43private:
44 counter* count;
45 const unsigned int x, y;
46 T* data;
47
48public:
49 array(unsigned int x, unsigned int y) :
50 count(new counter()),
51 x(x),
52 y(y),
53 data(new T[x * y]) {}
54
55 array(const array& p) :
56 count(p.count),
57 x(p.x),
58 y(p.y),
59 data(p.data) { count->increase(); }
60
61 ~array() { if (count->decrease() == 0) { delete count; delete [] data; } }
62
63 unsigned int length() const { return x * y; }
64
65 T* operator[](unsigned int a) { return a < x ? &data[a * y] : 0; }
66
67 T const* operator[](unsigned int a) const { return a < x ? &data[a * y] : 0; }
68};
69
71
72#endif
Definition: array.h:42
Definition: array.h:27