oculus1

annotate libovr/Src/Kernel/OVR_ContainerAllocator.h @ 12:d797639e0234

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