libsidplayfp  2.0.2
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 
27 class counter
28 {
29 private:
30  unsigned int c;
31 
32 public:
33  counter() : c(1) {}
34  void increase() { ++c; }
35  unsigned int decrease() { return --c; }
36 };
37 
41 template<typename T>
42 class matrix
43 {
44 private:
45  T* data;
46  counter* count;
47  const unsigned int x, y;
48 
49 public:
50  matrix(unsigned int x, unsigned int y) :
51  data(new T[x * y]),
52  count(new counter()),
53  x(x),
54  y(y) {}
55 
56  matrix(const matrix& p) :
57  data(p.data),
58  count(p.count),
59  x(p.x),
60  y(p.y) { count->increase(); }
61 
62  ~matrix() { if (count->decrease() == 0) { delete count; delete [] data; } }
63 
64  unsigned int length() const { return x * y; }
65 
66  T* operator[](unsigned int a) { return &data[a * y]; }
67 
68  T const* operator[](unsigned int a) const { return &data[a * y]; }
69 };
70 
71 typedef matrix<short> matrix_t;
72 
73 #endif
counter
Definition: array.h:28
matrix
Definition: array.h:43