Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_ContainerAllocator.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 PublicHeader: OVR.h
4 Filename : OVR_ContainerAllocator.h
5 Content : Template allocators and constructors for containers.
6 Created : September 19, 2012
7 Notes :
8 
9 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
10 
11 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
13 which is provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
15 
16 You may obtain a copy of the License at
17 
18 http://www.oculusvr.com/licenses/LICENSE-3.1
19 
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
25 
26 ************************************************************************************/
27 
28 #ifndef OVR_ContainerAllocator_h
29 #define OVR_ContainerAllocator_h
30 
31 #include "OVR_Allocator.h"
32 #include <string.h>
33 
34 
35 namespace OVR {
36 
37 
38 //-----------------------------------------------------------------------------------
39 // ***** Container Allocator
40 
41 // ContainerAllocator serves as a template argument for allocations done by
42 // containers, such as Array and Hash; replacing it could allow allocator
43 // substitution in containers.
44 
46 {
47 public:
48  static void* Alloc(UPInt size) { return OVR_ALLOC(size); }
49  static void* Realloc(void* p, UPInt newSize) { return OVR_REALLOC(p, newSize); }
50  static void Free(void *p) { OVR_FREE(p); }
51 };
52 
53 
54 
55 //-----------------------------------------------------------------------------------
56 // ***** Constructors, Destructors, Copiers
57 
58 // Plain Old Data - movable, no special constructors/destructor.
59 template<class T>
61 {
62 public:
63  static void Construct(void *) {}
64  static void Construct(void *p, const T& source)
65  {
66  *(T*)p = source;
67  }
68 
69  // Same as above, but allows for a different type of constructor.
70  template <class S>
71  static void ConstructAlt(void *p, const S& source)
72  {
73  *(T*)p = source;
74  }
75 
76  static void ConstructArray(void*, UPInt) {}
77 
78  static void ConstructArray(void* p, UPInt count, const T& source)
79  {
80  UByte *pdata = (UByte*)p;
81  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
82  *(T*)pdata = source;
83  }
84 
85  static void ConstructArray(void* p, UPInt count, const T* psource)
86  {
87  memcpy(p, psource, sizeof(T) * count);
88  }
89 
90  static void Destruct(T*) {}
91  static void DestructArray(T*, UPInt) {}
92 
93  static void CopyArrayForward(T* dst, const T* src, UPInt count)
94  {
95  memmove(dst, src, count * sizeof(T));
96  }
97 
98  static void CopyArrayBackward(T* dst, const T* src, UPInt count)
99  {
100  memmove(dst, src, count * sizeof(T));
101  }
102 
103  static bool IsMovable() { return true; }
104 };
105 
106 
107 //-----------------------------------------------------------------------------------
108 // ***** ConstructorMov
109 //
110 // Correct C++ construction and destruction for movable objects
111 template<class T>
113 {
114 public:
115  static void Construct(void* p)
116  {
117  OVR::Construct<T>(p);
118  }
119 
120  static void Construct(void* p, const T& source)
121  {
122  OVR::Construct<T>(p, source);
123  }
124 
125  // Same as above, but allows for a different type of constructor.
126  template <class S>
127  static void ConstructAlt(void* p, const S& source)
128  {
129  OVR::ConstructAlt<T,S>(p, source);
130  }
131 
132  static void ConstructArray(void* p, UPInt count)
133  {
134  UByte* pdata = (UByte*)p;
135  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
136  Construct(pdata);
137  }
138 
139  static void ConstructArray(void* p, UPInt count, const T& source)
140  {
141  UByte* pdata = (UByte*)p;
142  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
143  Construct(pdata, source);
144  }
145 
146  static void ConstructArray(void* p, UPInt count, const T* psource)
147  {
148  UByte* pdata = (UByte*)p;
149  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
150  Construct(pdata, *psource++);
151  }
152 
153  static void Destruct(T* p)
154  {
155  p->~T();
156  OVR_UNUSED(p); // Suppress silly MSVC warning
157  }
158 
159  static void DestructArray(T* p, UPInt count)
160  {
161  p += count - 1;
162  for (UPInt i=0; i<count; ++i, --p)
163  p->~T();
164  }
165 
166  static void CopyArrayForward(T* dst, const T* src, UPInt count)
167  {
168  memmove(dst, src, count * sizeof(T));
169  }
170 
171  static void CopyArrayBackward(T* dst, const T* src, UPInt count)
172  {
173  memmove(dst, src, count * sizeof(T));
174  }
175 
176  static bool IsMovable() { return true; }
177 };
178 
179 
180 //-----------------------------------------------------------------------------------
181 // ***** ConstructorCPP
182 //
183 // Correct C++ construction and destruction for movable objects
184 template<class T>
186 {
187 public:
188  static void Construct(void* p)
189  {
190  OVR::Construct<T>(p);
191  }
192 
193  static void Construct(void* p, const T& source)
194  {
195  OVR::Construct<T>(p, source);
196  }
197 
198  // Same as above, but allows for a different type of constructor.
199  template <class S>
200  static void ConstructAlt(void* p, const S& source)
201  {
202  OVR::ConstructAlt<T,S>(p, source);
203  }
204 
205  static void ConstructArray(void* p, UPInt count)
206  {
207  UByte* pdata = (UByte*)p;
208  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
209  Construct(pdata);
210  }
211 
212  static void ConstructArray(void* p, UPInt count, const T& source)
213  {
214  UByte* pdata = (UByte*)p;
215  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
216  Construct(pdata, source);
217  }
218 
219  static void ConstructArray(void* p, UPInt count, const T* psource)
220  {
221  UByte* pdata = (UByte*)p;
222  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
223  Construct(pdata, *psource++);
224  }
225 
226  static void Destruct(T* p)
227  {
228  p->~T();
229  OVR_UNUSED(p); // Suppress silly MSVC warning
230  }
231 
232  static void DestructArray(T* p, UPInt count)
233  {
234  p += count - 1;
235  for (UPInt i=0; i<count; ++i, --p)
236  p->~T();
237  }
238 
239  static void CopyArrayForward(T* dst, const T* src, UPInt count)
240  {
241  for(UPInt i = 0; i < count; ++i)
242  dst[i] = src[i];
243  }
244 
245  static void CopyArrayBackward(T* dst, const T* src, UPInt count)
246  {
247  for(UPInt i = count; i; --i)
248  dst[i-1] = src[i-1];
249  }
250 
251  static bool IsMovable() { return false; }
252 };
253 
254 
255 //-----------------------------------------------------------------------------------
256 // ***** Container Allocator with movement policy
257 //
258 // Simple wraps as specialized allocators
260 template<class T> struct ContainerAllocator : ContainerAllocatorBase, ConstructorMov<T> {};
262 
263 
264 } // OVR
265 
266 
267 #endif
static void CopyArrayBackward(T *dst, const T *src, UPInt count)
static void Construct(void *p)
static void ConstructArray(void *p, UPInt count, const T &source)
static void CopyArrayForward(T *dst, const T *src, UPInt count)
static void ConstructArray(void *p, UPInt count, const T &source)
__BEGIN_NAMESPACE_STD void * memcpy(void *__restrict __dest, const void *__restrict __src, size_t __n) __THROW __nonnull((1
static void ConstructArray(void *p, UPInt count)
static void ConstructArray(void *p, UPInt count, const T *psource)
static void DestructArray(T *, UPInt)
static void * Realloc(void *p, UPInt newSize)
static void CopyArrayForward(T *dst, const T *src, UPInt count)
#define OVR_UNUSED(a)
static void Destruct(T *p)
static void ConstructArray(void *, UPInt)
size_t UPInt
Definition: OVR_Types.h:218
static void Construct(void *)
uint8_t UByte
Definition: OVR_Types.h:249
static void DestructArray(T *p, UPInt count)
__BEGIN_NAMESPACE_STD void void * memmove(void *__dest, const void *__src, size_t __n) __THROW __nonnull((1
static void * Alloc(UPInt size)
static void ConstructArray(void *p, UPInt count, const T *psource)
static void CopyArrayForward(T *dst, const T *src, UPInt count)
static void Destruct(T *p)
static void Construct(void *p, const T &source)
static void ConstructAlt(void *p, const S &source)
static void CopyArrayBackward(T *dst, const T *src, UPInt count)
static void Construct(void *p, const T &source)
#define OVR_REALLOC(p, s)
static void ConstructArray(void *p, UPInt count, const T &source)
#define OVR_ALLOC(s)
static void CopyArrayBackward(T *dst, const T *src, UPInt count)
static void ConstructArray(void *p, UPInt count)
static void ConstructAlt(void *p, const S &source)
static void Construct(void *p)
#define OVR_FREE(p)
static void ConstructAlt(void *p, const S &source)
static void DestructArray(T *p, UPInt count)
static void ConstructArray(void *p, UPInt count, const T *psource)
static void Construct(void *p, const T &source)