oculus1
diff libovr/Src/Kernel/OVR_ContainerAllocator.h @ 1:e2f9e4603129
added LibOVR and started a simple vr wrapper.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Sep 2013 16:14:59 +0300 |
parents | |
children | b069a5c27388 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libovr/Src/Kernel/OVR_ContainerAllocator.h Sat Sep 14 16:14:59 2013 +0300 1.3 @@ -0,0 +1,1 @@ 1.4 +/************************************************************************************ 1.5 1.6 PublicHeader: OVR.h 1.7 Filename : OVR_ContainerAllocator.h 1.8 Content : Template allocators and constructors for containers. 1.9 Created : September 19, 2012 1.10 Notes : 1.11 1.12 Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. 1.13 1.14 Use of this software is subject to the terms of the Oculus license 1.15 agreement provided at the time of installation or download, or which 1.16 otherwise accompanies this software in either electronic or hard copy form. 1.17 1.18 ************************************************************************************/ 1.19 1.20 #ifndef OVR_ContainerAllocator_h 1.21 #define OVR_ContainerAllocator_h 1.22 1.23 #include "OVR_Allocator.h" 1.24 #include <string.h> 1.25 1.26 1.27 namespace OVR { 1.28 1.29 1.30 //----------------------------------------------------------------------------------- 1.31 // ***** Container Allocator 1.32 1.33 // ContainerAllocator serves as a template argument for allocations done by 1.34 // containers, such as Array and Hash; replacing it could allow allocator 1.35 // substitution in containers. 1.36 1.37 class ContainerAllocatorBase 1.38 { 1.39 public: 1.40 static void* Alloc(UPInt size) { return OVR_ALLOC(size); } 1.41 static void* Realloc(void* p, UPInt newSize) { return OVR_REALLOC(p, newSize); } 1.42 static void Free(void *p) { OVR_FREE(p); } 1.43 }; 1.44 1.45 1.46 1.47 //----------------------------------------------------------------------------------- 1.48 // ***** Constructors, Destructors, Copiers 1.49 1.50 // Plain Old Data - movable, no special constructors/destructor. 1.51 template<class T> 1.52 class ConstructorPOD 1.53 { 1.54 public: 1.55 static void Construct(void *) {} 1.56 static void Construct(void *p, const T& source) 1.57 { 1.58 *(T*)p = source; 1.59 } 1.60 1.61 // Same as above, but allows for a different type of constructor. 1.62 template <class S> 1.63 static void ConstructAlt(void *p, const S& source) 1.64 { 1.65 *(T*)p = source; 1.66 } 1.67 1.68 static void ConstructArray(void*, UPInt) {} 1.69 1.70 static void ConstructArray(void* p, UPInt count, const T& source) 1.71 { 1.72 UByte *pdata = (UByte*)p; 1.73 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.74 *(T*)pdata = source; 1.75 } 1.76 1.77 static void ConstructArray(void* p, UPInt count, const T* psource) 1.78 { 1.79 memcpy(p, psource, sizeof(T) * count); 1.80 } 1.81 1.82 static void Destruct(T*) {} 1.83 static void DestructArray(T*, UPInt) {} 1.84 1.85 static void CopyArrayForward(T* dst, const T* src, UPInt count) 1.86 { 1.87 memmove(dst, src, count * sizeof(T)); 1.88 } 1.89 1.90 static void CopyArrayBackward(T* dst, const T* src, UPInt count) 1.91 { 1.92 memmove(dst, src, count * sizeof(T)); 1.93 } 1.94 1.95 static bool IsMovable() { return true; } 1.96 }; 1.97 1.98 1.99 //----------------------------------------------------------------------------------- 1.100 // ***** ConstructorMov 1.101 // 1.102 // Correct C++ construction and destruction for movable objects 1.103 template<class T> 1.104 class ConstructorMov 1.105 { 1.106 public: 1.107 static void Construct(void* p) 1.108 { 1.109 OVR::Construct<T>(p); 1.110 } 1.111 1.112 static void Construct(void* p, const T& source) 1.113 { 1.114 OVR::Construct<T>(p, source); 1.115 } 1.116 1.117 // Same as above, but allows for a different type of constructor. 1.118 template <class S> 1.119 static void ConstructAlt(void* p, const S& source) 1.120 { 1.121 OVR::ConstructAlt<T,S>(p, source); 1.122 } 1.123 1.124 static void ConstructArray(void* p, UPInt count) 1.125 { 1.126 UByte* pdata = (UByte*)p; 1.127 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.128 Construct(pdata); 1.129 } 1.130 1.131 static void ConstructArray(void* p, UPInt count, const T& source) 1.132 { 1.133 UByte* pdata = (UByte*)p; 1.134 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.135 Construct(pdata, source); 1.136 } 1.137 1.138 static void ConstructArray(void* p, UPInt count, const T* psource) 1.139 { 1.140 UByte* pdata = (UByte*)p; 1.141 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.142 Construct(pdata, *psource++); 1.143 } 1.144 1.145 static void Destruct(T* p) 1.146 { 1.147 p->~T(); 1.148 OVR_UNUSED(p); // Suppress silly MSVC warning 1.149 } 1.150 1.151 static void DestructArray(T* p, UPInt count) 1.152 { 1.153 p += count - 1; 1.154 for (UPInt i=0; i<count; ++i, --p) 1.155 p->~T(); 1.156 } 1.157 1.158 static void CopyArrayForward(T* dst, const T* src, UPInt count) 1.159 { 1.160 memmove(dst, src, count * sizeof(T)); 1.161 } 1.162 1.163 static void CopyArrayBackward(T* dst, const T* src, UPInt count) 1.164 { 1.165 memmove(dst, src, count * sizeof(T)); 1.166 } 1.167 1.168 static bool IsMovable() { return true; } 1.169 }; 1.170 1.171 1.172 //----------------------------------------------------------------------------------- 1.173 // ***** ConstructorCPP 1.174 // 1.175 // Correct C++ construction and destruction for movable objects 1.176 template<class T> 1.177 class ConstructorCPP 1.178 { 1.179 public: 1.180 static void Construct(void* p) 1.181 { 1.182 OVR::Construct<T>(p); 1.183 } 1.184 1.185 static void Construct(void* p, const T& source) 1.186 { 1.187 OVR::Construct<T>(p, source); 1.188 } 1.189 1.190 // Same as above, but allows for a different type of constructor. 1.191 template <class S> 1.192 static void ConstructAlt(void* p, const S& source) 1.193 { 1.194 OVR::ConstructAlt<T,S>(p, source); 1.195 } 1.196 1.197 static void ConstructArray(void* p, UPInt count) 1.198 { 1.199 UByte* pdata = (UByte*)p; 1.200 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.201 Construct(pdata); 1.202 } 1.203 1.204 static void ConstructArray(void* p, UPInt count, const T& source) 1.205 { 1.206 UByte* pdata = (UByte*)p; 1.207 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.208 Construct(pdata, source); 1.209 } 1.210 1.211 static void ConstructArray(void* p, UPInt count, const T* psource) 1.212 { 1.213 UByte* pdata = (UByte*)p; 1.214 for (UPInt i=0; i< count; ++i, pdata += sizeof(T)) 1.215 Construct(pdata, *psource++); 1.216 } 1.217 1.218 static void Destruct(T* p) 1.219 { 1.220 p->~T(); 1.221 OVR_UNUSED(p); // Suppress silly MSVC warning 1.222 } 1.223 1.224 static void DestructArray(T* p, UPInt count) 1.225 { 1.226 p += count - 1; 1.227 for (UPInt i=0; i<count; ++i, --p) 1.228 p->~T(); 1.229 } 1.230 1.231 static void CopyArrayForward(T* dst, const T* src, UPInt count) 1.232 { 1.233 for(UPInt i = 0; i < count; ++i) 1.234 dst[i] = src[i]; 1.235 } 1.236 1.237 static void CopyArrayBackward(T* dst, const T* src, UPInt count) 1.238 { 1.239 for(UPInt i = count; i; --i) 1.240 dst[i-1] = src[i-1]; 1.241 } 1.242 1.243 static bool IsMovable() { return false; } 1.244 }; 1.245 1.246 1.247 //----------------------------------------------------------------------------------- 1.248 // ***** Container Allocator with movement policy 1.249 // 1.250 // Simple wraps as specialized allocators 1.251 template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {}; 1.252 template<class T> struct ContainerAllocator : ContainerAllocatorBase, ConstructorMov<T> {}; 1.253 template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {}; 1.254 1.255 1.256 } // OVR 1.257 1.258 1.259 #endif 1.260 \ No newline at end of file