libresidfp 0.9.1
array.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright (C) 2011-2014 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
24
25#include <atomic>
26
31{
32private:
33 std::atomic<unsigned int> c;
34
35public:
36 counter() : c(1) {}
37 void increase() { ++c; }
38 unsigned int decrease() { return --c; }
39};
40
44template<typename T>
45class matrix
46{
47private:
48 T* data;
49 counter* count;
50 const unsigned int x, y;
51
52public:
53 matrix(unsigned int x, unsigned int y) :
54 data(new T[x * y]),
55 count(new counter()),
56 x(x),
57 y(y) {}
58
59 matrix(const matrix& p) :
60 data(p.data),
61 count(p.count),
62 x(p.x),
63 y(p.y) { count->increase(); }
64
65 ~matrix() { if (count->decrease() == 0) { delete count; delete [] data; } }
66
67 unsigned int length() const { return x * y; }
68
69 T* operator[](unsigned int a) { return &data[a * y]; }
70
71 T const* operator[](unsigned int a) const { return &data[a * y]; }
72};
73
75
76#endif
Definition array.h:31
Definition array.h:46