libsidplayfp 2.5.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#ifdef HAVE_CONFIG_H
26# include "config.h"
27#endif
28
29#ifdef HAVE_CXX11
30# include <atomic>
31#endif
32
37{
38private:
39#ifndef HAVE_CXX11
40 volatile unsigned int c;
41#else
42 std::atomic<unsigned int> c;
43#endif
44
45public:
46 counter() : c(1) {}
47 void increase() { ++c; }
48 unsigned int decrease() { return --c; }
49};
50
54template<typename T>
55class matrix
56{
57private:
58 T* data;
59 counter* count;
60 const unsigned int x, y;
61
62public:
63 matrix(unsigned int x, unsigned int y) :
64 data(new T[x * y]),
65 count(new counter()),
66 x(x),
67 y(y) {}
68
69 matrix(const matrix& p) :
70 data(p.data),
71 count(p.count),
72 x(p.x),
73 y(p.y) { count->increase(); }
74
75 ~matrix() { if (count->decrease() == 0) { delete count; delete [] data; } }
76
77 unsigned int length() const { return x * y; }
78
79 T* operator[](unsigned int a) { return &data[a * y]; }
80
81 T const* operator[](unsigned int a) const { return &data[a * y]; }
82};
83
85
86#endif
Definition array.h:37
Definition array.h:56