RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
carray.h
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #ifndef RIOENGINE_CENGINE_CARRAY_H_
31 #define RIOENGINE_CENGINE_CARRAY_H_
32 
33 #include <vector>
34 #include "bullet/btBulletDynamicsCommon.h"
35 #include "cengine/cobject.h"
36 
42 template <class T>
43 class CArray : public CObject {
44 public:
50  CArray(T* _array, unsigned int size) {
51  RE_ASSERT(!IS_POINTER(T));
52  RE_ASSERT(_array);
53 
54  m_array = _array;
55  m_size = size;
56  }
57 
58  //-----------------------------------------------------------------------------
59 
64  CArray(const CArray& _array) {
65  RE_ASSERT(_array.data());
66  RE_ASSERT(_array.size() > 0); // TODO is this really necessary?. It should be valid to create a copy of an empty array.
67 
68  m_size = _array.size();
69  m_array = new T[m_size];
70 
71  T* data = (T*)_array.data();
72 
73  for (unsigned int i = 0; i < m_size; i++) {
74  m_array[i] = data[i];
75  }
76  }
77 
78  //-----------------------------------------------------------------------------
79 
83  T objectAtIndex(unsigned int index) const {
85  RE_ASSERT(index < m_size);
86 
87  return m_array[index];
88  }
89 
90  //-----------------------------------------------------------------------------
91 
95  T* data() const {
96  return m_array;
97  }
98 
99  //-----------------------------------------------------------------------------
100 
104  unsigned int size() const {
105  return m_size;
106  }
107 
108  //-----------------------------------------------------------------------------
109 
113  std::vector<T>* toStdVector() const {
115 
116  std::vector<T>* _vector = new std::vector<T>();
117  for (unsigned int i = 0; i < m_size; i++) {
118  _vector->push_back(m_array[i]);
119  }
120 
121  return _vector;
122  }
123 
124  //-----------------------------------------------------------------------------
125 
129  static CArray* fromStdVector(std::vector<T>* vec) {
130  RE_ASSERT(vec);
131 
132  unsigned int vector_size = vec->size();
133  T* data = new T[vector_size];
134 
135  for (unsigned int i = 0; i < vector_size; i++) {
136  data[i] = vec->at(i);
137  }
138 
139  CArray<T>* array_dest = new CArray<T>(data, vector_size);
140  array_dest->autorelease();
141 
142  return array_dest;
143  }
144 
145  //-----------------------------------------------------------------------------
146 
150  static CArray* fromBtAlignedObjectArray(btAlignedObjectArray<T>* vec) {
151  RE_ASSERT(vec);
152 
153  unsigned int vector_size = vec->size();
154  T* data = new T[vector_size];
155 
156  for (unsigned int i = 0; i < vector_size; i++) {
157  data[i] = vec->at(i);
158  }
159 
160  CArray<T>* array_dest = new CArray<T>(data, vector_size);
161  array_dest->autorelease();
162 
163  return array_dest;
164  }
165 
166  //-----------------------------------------------------------------------------
167 
170  }
171 
172 protected:
173  unsigned int m_size;
175 };
176 
177 #endif // RIOENGINE_CENGINE_CARRAY_H_
std::vector< T > * toStdVector() const
Definition: carray.h:113
static CArray * fromBtAlignedObjectArray(btAlignedObjectArray< T > *vec)
Definition: carray.h:150
~CArray()
Definition: carray.h:168
T * data() const
Definition: carray.h:95
#define IS_POINTER(T)
Definition: macro.h:55
unsigned int m_size
Definition: carray.h:173
T * m_array
Definition: carray.h:174
Definition: carray.h:43
T objectAtIndex(unsigned int index) const
Definition: carray.h:83
CObject * autorelease()
Definition: cobject.cpp:87
#define RE_ASSERT
Definition: macro.h:57
#define SAFE_RELEASE_ARRAY(x)
Definition: cobject.h:37
unsigned int size() const
Definition: carray.h:104
CArray(const CArray &_array)
CArray.
Definition: carray.h:64
CArray(T *_array, unsigned int size)
CArray.
Definition: carray.h:50
static CArray * fromStdVector(std::vector< T > *vec)
Definition: carray.h:129