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