| 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 |
#include <mutex> |
| 25 |
|
| 26 |
extern std::mutex counterOps; |
| 27 |
|
| 28 |
/** |
| 29 |
* Counter. |
| 30 |
*/ |
| 31 |
class counter |
| 32 |
{ |
| 33 |
private: |
| 34 |
unsigned int c; |
| 35 |
|
| 36 |
public: |
| 37 |
counter() : c(1) {} |
| 38 |
void increase() { ++c; } |
| 39 |
unsigned int decrease() { return --c; } |
| 40 |
}; |
| 41 |
|
| 42 |
/** |
| 43 |
* Reference counted pointer to matrix wrapper, for use with standard containers. |
| 44 |
*/ |
| 45 |
template<typename T> |
| 46 |
class matrix |
| 47 |
{ |
| 48 |
private: |
| 49 |
T* data; |
| 50 |
counter* count; |
| 51 |
const unsigned int x, y; |
| 52 |
|
| 53 |
public: |
| 54 |
matrix(unsigned int x, unsigned int y) : |
| 55 |
data(new T[x * y]), |
| 56 |
count(new counter()), |
| 57 |
x(x), |
| 58 |
y(y) {} |
| 59 |
|
| 60 |
matrix(const matrix& p) : |
| 61 |
data(p.data), |
| 62 |
count(p.count), |
| 63 |
x(p.x), |
| 64 |
y(p.y) { count->increase(); } |
| 65 |
|
| 66 |
~matrix() { counterOps.lock(); if (count->decrease() == 0) { delete count; delete [] data; } counterOps.unlock(); } |
| 67 |
|
| 68 |
unsigned int length() const { return x * y; } |
| 69 |
|
| 70 |
T* operator[](unsigned int a) { return &data[a * y]; } |
| 71 |
|
| 72 |
T const* operator[](unsigned int a) const { return &data[a * y]; } |
| 73 |
}; |
| 74 |
|
| 75 |
typedef matrix<short> matrix_t; |
| 76 |
|
| 77 |
#endif |