FotoSHOCK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
FixedArray.hpp
1 /*
2  * Copyright 2011, 2012 Lukas Jirkovsky
3  *
4  * This file is part of FotoSHOCKcore.
5  *
6  * FotoSHOCKcore is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * FotoSHOCKcore is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with FotoSHOCKcore. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef FIXEDARRAY_H
20 #define FIXEDARRAY_H
21 
22 #include "CoreException.hpp"
23 
24 #include <cassert>
25 #include <cstddef>
26 #include <cstring>
27 
28 namespace FotoSHOCKcore {
29 
31 
37 template <typename T>
38 class FixedArray {
39  public:
40  typedef T value_type;
41  typedef T* iterator;
42  typedef const T* const_iterator;
43  typedef T& reference;
44  typedef const T& const_reference;
45  typedef std::size_t size_type;
46  typedef std::ptrdiff_t difference_type;
47 
49 
53  FixedArray(const size_type size) : m_size(size) {
54  if (size > 0) {
55  m_content = new value_type[size]();
56  } else {
57  m_content = 0;
58  }
59  }
60 
62  FixedArray(const FixedArray<value_type>& other) : m_size(other.m_size) {
63  if (other.m_size > 0) {
64  m_content = new value_type[m_size];
65  std::memcpy(m_content, other.m_content, m_size * sizeof(value_type));
66  } else {
67  m_content = 0;
68  }
69  }
70 
73  if (m_content) {
74  delete[] m_content;
75  }
76  }
77 
80  if (m_size == other.m_size) {
81  std::memcpy(m_content, other.m_content, m_size * sizeof(value_type));
82  return *this;
83  } else {
84  throw ArrayException("Array sizes differ", __FILE__, __LINE__);
85  }
86  }
87 
89 
92  reference operator[](size_type i) {
93  assert (m_content != 0);
94  return m_content[i];
95  }
96 
98 
101  const_reference operator[](size_type i) const {
102  assert (m_content != 0);
103  return m_content[i];
104  }
105 
107  size_type size() const {
108  return m_size;
109  }
110 
111  private:
112  const size_type m_size;
113  value_type* m_content;
114 };
115 
116 }
117 
118 #endif