oculus1
view libovr/Src/Kernel/OVR_ContainerAllocator.h @ 17:cfe4979ab3eb
ops, minor error in the last commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 21 Sep 2013 07:09:48 +0300 |
parents | e2f9e4603129 |
children |
line source
1 /************************************************************************************
3 PublicHeader: OVR.h
4 Filename : OVR_ContainerAllocator.h
5 Content : Template allocators and constructors for containers.
6 Created : September 19, 2012
7 Notes :
9 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
11 Use of this software is subject to the terms of the Oculus license
12 agreement provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
15 ************************************************************************************/
17 #ifndef OVR_ContainerAllocator_h
18 #define OVR_ContainerAllocator_h
20 #include "OVR_Allocator.h"
21 #include <string.h>
24 namespace OVR {
27 //-----------------------------------------------------------------------------------
28 // ***** Container Allocator
30 // ContainerAllocator serves as a template argument for allocations done by
31 // containers, such as Array and Hash; replacing it could allow allocator
32 // substitution in containers.
34 class ContainerAllocatorBase
35 {
36 public:
37 static void* Alloc(UPInt size) { return OVR_ALLOC(size); }
38 static void* Realloc(void* p, UPInt newSize) { return OVR_REALLOC(p, newSize); }
39 static void Free(void *p) { OVR_FREE(p); }
40 };
44 //-----------------------------------------------------------------------------------
45 // ***** Constructors, Destructors, Copiers
47 // Plain Old Data - movable, no special constructors/destructor.
48 template<class T>
49 class ConstructorPOD
50 {
51 public:
52 static void Construct(void *) {}
53 static void Construct(void *p, const T& source)
54 {
55 *(T*)p = source;
56 }
58 // Same as above, but allows for a different type of constructor.
59 template <class S>
60 static void ConstructAlt(void *p, const S& source)
61 {
62 *(T*)p = source;
63 }
65 static void ConstructArray(void*, UPInt) {}
67 static void ConstructArray(void* p, UPInt count, const T& source)
68 {
69 UByte *pdata = (UByte*)p;
70 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
71 *(T*)pdata = source;
72 }
74 static void ConstructArray(void* p, UPInt count, const T* psource)
75 {
76 memcpy(p, psource, sizeof(T) * count);
77 }
79 static void Destruct(T*) {}
80 static void DestructArray(T*, UPInt) {}
82 static void CopyArrayForward(T* dst, const T* src, UPInt count)
83 {
84 memmove(dst, src, count * sizeof(T));
85 }
87 static void CopyArrayBackward(T* dst, const T* src, UPInt count)
88 {
89 memmove(dst, src, count * sizeof(T));
90 }
92 static bool IsMovable() { return true; }
93 };
96 //-----------------------------------------------------------------------------------
97 // ***** ConstructorMov
98 //
99 // Correct C++ construction and destruction for movable objects
100 template<class T>
101 class ConstructorMov
102 {
103 public:
104 static void Construct(void* p)
105 {
106 OVR::Construct<T>(p);
107 }
109 static void Construct(void* p, const T& source)
110 {
111 OVR::Construct<T>(p, source);
112 }
114 // Same as above, but allows for a different type of constructor.
115 template <class S>
116 static void ConstructAlt(void* p, const S& source)
117 {
118 OVR::ConstructAlt<T,S>(p, source);
119 }
121 static void ConstructArray(void* p, UPInt count)
122 {
123 UByte* pdata = (UByte*)p;
124 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
125 Construct(pdata);
126 }
128 static void ConstructArray(void* p, UPInt count, const T& source)
129 {
130 UByte* pdata = (UByte*)p;
131 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
132 Construct(pdata, source);
133 }
135 static void ConstructArray(void* p, UPInt count, const T* psource)
136 {
137 UByte* pdata = (UByte*)p;
138 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
139 Construct(pdata, *psource++);
140 }
142 static void Destruct(T* p)
143 {
144 p->~T();
145 OVR_UNUSED(p); // Suppress silly MSVC warning
146 }
148 static void DestructArray(T* p, UPInt count)
149 {
150 p += count - 1;
151 for (UPInt i=0; i<count; ++i, --p)
152 p->~T();
153 }
155 static void CopyArrayForward(T* dst, const T* src, UPInt count)
156 {
157 memmove(dst, src, count * sizeof(T));
158 }
160 static void CopyArrayBackward(T* dst, const T* src, UPInt count)
161 {
162 memmove(dst, src, count * sizeof(T));
163 }
165 static bool IsMovable() { return true; }
166 };
169 //-----------------------------------------------------------------------------------
170 // ***** ConstructorCPP
171 //
172 // Correct C++ construction and destruction for movable objects
173 template<class T>
174 class ConstructorCPP
175 {
176 public:
177 static void Construct(void* p)
178 {
179 OVR::Construct<T>(p);
180 }
182 static void Construct(void* p, const T& source)
183 {
184 OVR::Construct<T>(p, source);
185 }
187 // Same as above, but allows for a different type of constructor.
188 template <class S>
189 static void ConstructAlt(void* p, const S& source)
190 {
191 OVR::ConstructAlt<T,S>(p, source);
192 }
194 static void ConstructArray(void* p, UPInt count)
195 {
196 UByte* pdata = (UByte*)p;
197 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
198 Construct(pdata);
199 }
201 static void ConstructArray(void* p, UPInt count, const T& source)
202 {
203 UByte* pdata = (UByte*)p;
204 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
205 Construct(pdata, source);
206 }
208 static void ConstructArray(void* p, UPInt count, const T* psource)
209 {
210 UByte* pdata = (UByte*)p;
211 for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
212 Construct(pdata, *psource++);
213 }
215 static void Destruct(T* p)
216 {
217 p->~T();
218 OVR_UNUSED(p); // Suppress silly MSVC warning
219 }
221 static void DestructArray(T* p, UPInt count)
222 {
223 p += count - 1;
224 for (UPInt i=0; i<count; ++i, --p)
225 p->~T();
226 }
228 static void CopyArrayForward(T* dst, const T* src, UPInt count)
229 {
230 for(UPInt i = 0; i < count; ++i)
231 dst[i] = src[i];
232 }
234 static void CopyArrayBackward(T* dst, const T* src, UPInt count)
235 {
236 for(UPInt i = count; i; --i)
237 dst[i-1] = src[i-1];
238 }
240 static bool IsMovable() { return false; }
241 };
244 //-----------------------------------------------------------------------------------
245 // ***** Container Allocator with movement policy
246 //
247 // Simple wraps as specialized allocators
248 template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
249 template<class T> struct ContainerAllocator : ContainerAllocatorBase, ConstructorMov<T> {};
250 template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
253 } // OVR
256 #endif