CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUAligned.h
1 //
2 // CUAligned.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an aligned array that is C++11. Aligned arrays are
6 // added to C++17, but there is no guaranteed Android support for C++17.
7 //
8 // This tempalted class does not uses our standard shared-pointer architecture.
9 // We often want to copy or move these arrays, so we have non-trivial constructors
10 // for this class. With that said, we do have a static method which allocates
11 // a shared pointer.
12 //
13 // CUGL MIT License:
14 // This software is provided 'as-is', without any express or implied
15 // warranty. In no event will the authors be held liable for any damages
16 // arising from the use of this software.
17 //
18 // Permission is granted to anyone to use this software for any purpose,
19 // including commercial applications, and to alter it and redistribute it
20 // freely, subject to the following restrictions:
21 //
22 // 1. The origin of this software must not be misrepresented; you must not
23 // claim that you wrote the original software. If you use this software
24 // in a product, an acknowledgment in the product documentation would be
25 // appreciated but is not required.
26 //
27 // 2. Altered source versions must be plainly marked as such, and must not
28 // be misrepresented as being the original software.
29 //
30 // 3. This notice may not be removed or altered from any source distribution.
31 //
32 // Author: Walker White
33 // Version: 6/30/18
34 #ifndef __CU_ALIGNED_H__
35 #define __CU_ALIGNED_H__
36 #include "CUDebug.h"
37 #include <memory.h>
38 
39 namespace cugl {
40 
41 #pragma mark -
42 #pragma mark Aligned Arrays Template
43 
52 template <class T>
53 class Aligned {
54 private:
56  T* _pntr;
58  void* _orig;
60  size_t _capacity;
62  size_t _length;
64  size_t _alignm;
65 
66 #pragma mark -
67 #pragma mark Constructors
68 public:
72  Aligned() :
73  _pntr(nullptr),
74  _orig(nullptr),
75  _capacity(0),
76  _length(0),
77  _alignm(0) { }
78 
79 
80 
87  Aligned(size_t size, size_t alignment) {
88  _capacity = size*sizeof(T)+alignment;
89  _length = size;
90  _alignm = alignment;
91  _orig = malloc(_capacity);
92  _pntr = (T*)std::align(alignment,_length*sizeof(T),_orig,_capacity);
93  }
94 
100  Aligned(const Aligned<T>& copy) {
101  _capacity = copy._capacity;
102  _length = copy._length;
103  _alignm = copy._alignm;
104  _orig = malloc(_capacity);
105  _pntr = (T*)std::align(_alignm,_length*sizeof(T),_orig,_capacity);
106  std::memcpy(_pntr,copy._pntr,_length*sizeof(T)); // Only copy visible space
107  }
108 
114  Aligned(Aligned<T>&& other) {
115  _capacity = other._capacity;
116  _length = other._length;
117  _alignm = other._alignm;
118  _orig = other._orig;
119  _pntr = other._pntr;
120  other._orig = nullptr;
121  other._pntr = nullptr;
122  }
123 
127  ~Aligned() { dispose(); }
128 
132  void dispose() {
133  if (_orig) free(_orig);
134  _pntr = nullptr;
135  _orig = nullptr;
136  _capacity = 0;
137  _length = 0;
138  }
139 
148  static std::shared_ptr<Aligned<T>> alloc(size_t size, size_t alignment) {
149  std::shared_ptr<Aligned<T>> result = std::make_shared<Aligned<T>>();
150  return (result->reset(size,alignment) ? result : nullptr);
151  }
152 
153 #pragma mark -
154 #pragma mark Array Methods
155 
161  bool reset(size_t size, size_t alignment) {
162  if (_orig) dispose();
163 
164  _capacity = size*sizeof(T)+alignment;
165  _length = size;
166  _alignm = alignment;
167 
168  _orig = malloc(_capacity);
169  _pntr = (T*)std::align(_alignm,size*sizeof(T),_orig,_capacity);
170  return _pntr;
171  }
172 
176  void clear() {
177  std::memset(_pntr,0,_length*sizeof(T));
178  }
179 
185  size_t size() const {
186  return _length;
187  }
188 
189 #pragma mark -
190 #pragma mark Copy Behavior
191 
199  _capacity = copy._capacity;
200  _length = copy._length;
201  _alignm = copy._alignm;
202  _orig = malloc(_capacity);
203  _pntr = (T*)std::align(_alignm,_length*sizeof(T),_orig,_capacity);
204  std::memcpy(_pntr,copy._pntr,_length*sizeof(T)); // Only copy visible space
205  return *this;
206  }
207 
216  _capacity = other._capacity;
217  _length = other._length;
218  _alignm = other._alignm;
219  _orig = other._orig;
220  _pntr = other._pntr;
221  other._orig = nullptr;
222  other._pntr = nullptr;
223  return *this;
224  }
225 
226 #pragma mark -
227 #pragma mark Pointer Behavior
228 
233  operator T*() const {
234  return _pntr;
235  }
236 
237 
243  T& operator*() {
244  return _pntr[0];
245  }
246 
254  T* operator+(size_t offset) {
255  return _pntr+offset;
256  }
257 
265  T& operator[](size_t idx) {
266  return _pntr[idx];
267  }
268 
276  const T& operator[](size_t idx) const {
277  return _pntr[idx];
278  }
279 
280 
281 };
282 
283 }
284 
285 #endif /* __CU_ALIGNED_H__ */
cugl::Aligned::dispose
void dispose()
Definition: CUAligned.h:132
cugl::Aligned::operator*
T & operator*()
Definition: CUAligned.h:243
cugl::Aligned::Aligned
Aligned(Aligned< T > &&other)
Definition: CUAligned.h:114
cugl::Aligned::alloc
static std::shared_ptr< Aligned< T > > alloc(size_t size, size_t alignment)
Definition: CUAligned.h:148
cugl::Aligned::operator[]
T & operator[](size_t idx)
Definition: CUAligned.h:265
cugl::Aligned::reset
bool reset(size_t size, size_t alignment)
Definition: CUAligned.h:161
cugl::Aligned
Definition: CUAligned.h:53
cugl::Aligned::size
size_t size() const
Definition: CUAligned.h:185
cugl::Aligned::Aligned
Aligned(size_t size, size_t alignment)
Definition: CUAligned.h:87
cugl::Aligned::operator=
Aligned< T > & operator=(Aligned< T > &&other)
Definition: CUAligned.h:215
cugl::Aligned::operator=
Aligned< T > & operator=(const Aligned< T > &copy)
Definition: CUAligned.h:198
cugl::Aligned::Aligned
Aligned(const Aligned< T > &copy)
Definition: CUAligned.h:100
cugl::Aligned::operator+
T * operator+(size_t offset)
Definition: CUAligned.h:254
cugl::Aligned::~Aligned
~Aligned()
Definition: CUAligned.h:127
cugl::Aligned::Aligned
Aligned()
Definition: CUAligned.h:72
cugl::Aligned::clear
void clear()
Definition: CUAligned.h:176