OVR_ContainerAllocator.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 PublicHeader:   OVR.h
00004 Filename    :   OVR_ContainerAllocator.h
00005 Content     :   Template allocators and constructors for containers.
00006 Created     :   September 19, 2012
00007 Notes       : 
00008 
00009 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00010 
00011 Use of this software is subject to the terms of the Oculus license
00012 agreement provided at the time of installation or download, or which
00013 otherwise accompanies this software in either electronic or hard copy form.
00014 
00015 ************************************************************************************/
00016 
00017 #ifndef OVR_ContainerAllocator_h
00018 #define OVR_ContainerAllocator_h
00019 
00020 #include "OVR_Allocator.h"
00021 #include <string.h>
00022 
00023 
00024 namespace OVR {
00025 
00026 
00027 //-----------------------------------------------------------------------------------
00028 // ***** Container Allocator
00029 
00030 // ContainerAllocator serves as a template argument for allocations done by
00031 // containers, such as Array and Hash; replacing it could allow allocator
00032 // substitution in containers.
00033 
00034 class ContainerAllocatorBase
00035 {
00036 public:
00037     static void* Alloc(UPInt size)                { return OVR_ALLOC(size); }
00038     static void* Realloc(void* p, UPInt newSize)  { return OVR_REALLOC(p, newSize); }
00039     static void  Free(void *p)                    { OVR_FREE(p); }
00040 };
00041 
00042 
00043 
00044 //-----------------------------------------------------------------------------------
00045 // ***** Constructors, Destructors, Copiers
00046 
00047 // Plain Old Data - movable, no special constructors/destructor.
00048 template<class T> 
00049 class ConstructorPOD
00050 {
00051 public:
00052     static void Construct(void *) {}
00053     static void Construct(void *p, const T& source) 
00054     { 
00055         *(T*)p = source;
00056     }
00057 
00058     // Same as above, but allows for a different type of constructor.
00059     template <class S> 
00060     static void ConstructAlt(void *p, const S& source)
00061     {
00062         *(T*)p = source;
00063     }
00064 
00065     static void ConstructArray(void*, UPInt) {}
00066 
00067     static void ConstructArray(void* p, UPInt count, const T& source)
00068     {
00069         UByte *pdata = (UByte*)p;
00070         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00071             *(T*)pdata = source;
00072     }
00073 
00074     static void ConstructArray(void* p, UPInt count, const T* psource)
00075     {
00076         memcpy(p, psource, sizeof(T) * count);
00077     }
00078 
00079     static void Destruct(T*) {}
00080     static void DestructArray(T*, UPInt) {}
00081 
00082     static void CopyArrayForward(T* dst, const T* src, UPInt count)
00083     {
00084         memmove(dst, src, count * sizeof(T));
00085     }
00086 
00087     static void CopyArrayBackward(T* dst, const T* src, UPInt count)
00088     {
00089         memmove(dst, src, count * sizeof(T));
00090     }
00091 
00092     static bool IsMovable() { return true; }
00093 };
00094 
00095 
00096 //-----------------------------------------------------------------------------------
00097 // ***** ConstructorMov
00098 //
00099 // Correct C++ construction and destruction for movable objects
00100 template<class T> 
00101 class ConstructorMov
00102 {
00103 public:
00104     static void Construct(void* p) 
00105     { 
00106         OVR::Construct<T>(p);
00107     }
00108 
00109     static void Construct(void* p, const T& source) 
00110     { 
00111         OVR::Construct<T>(p, source);
00112     }
00113 
00114     // Same as above, but allows for a different type of constructor.
00115     template <class S> 
00116     static void ConstructAlt(void* p, const S& source)
00117     {
00118         OVR::ConstructAlt<T,S>(p, source);
00119     }
00120 
00121     static void ConstructArray(void* p, UPInt count)
00122     {
00123         UByte* pdata = (UByte*)p;
00124         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00125             Construct(pdata);
00126     }
00127 
00128     static void ConstructArray(void* p, UPInt count, const T& source)
00129     {
00130         UByte* pdata = (UByte*)p;
00131         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00132             Construct(pdata, source);
00133     }
00134 
00135     static void ConstructArray(void* p, UPInt count, const T* psource)
00136     {
00137         UByte* pdata = (UByte*)p;
00138         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00139             Construct(pdata, *psource++);
00140     }
00141 
00142     static void Destruct(T* p)
00143     {
00144         p->~T();
00145         OVR_UNUSED(p); // Suppress silly MSVC warning
00146     }
00147 
00148     static void DestructArray(T* p, UPInt count)
00149     {   
00150         p += count - 1;
00151         for (UPInt i=0; i<count; ++i, --p)
00152             p->~T();
00153     }
00154 
00155     static void CopyArrayForward(T* dst, const T* src, UPInt count)
00156     {
00157         memmove(dst, src, count * sizeof(T));
00158     }
00159 
00160     static void CopyArrayBackward(T* dst, const T* src, UPInt count)
00161     {
00162         memmove(dst, src, count * sizeof(T));
00163     }
00164 
00165     static bool IsMovable() { return true; }
00166 };
00167 
00168 
00169 //-----------------------------------------------------------------------------------
00170 // ***** ConstructorCPP
00171 //
00172 // Correct C++ construction and destruction for movable objects
00173 template<class T> 
00174 class ConstructorCPP
00175 {
00176 public:
00177     static void Construct(void* p) 
00178     { 
00179         OVR::Construct<T>(p);        
00180     }
00181 
00182     static void Construct(void* p, const T& source) 
00183     { 
00184         OVR::Construct<T>(p, source);        
00185     }
00186 
00187     // Same as above, but allows for a different type of constructor.
00188     template <class S> 
00189     static void ConstructAlt(void* p, const S& source)
00190     {
00191         OVR::ConstructAlt<T,S>(p, source);        
00192     }
00193 
00194     static void ConstructArray(void* p, UPInt count)
00195     {
00196         UByte* pdata = (UByte*)p;
00197         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00198             Construct(pdata);
00199     }
00200 
00201     static void ConstructArray(void* p, UPInt count, const T& source)
00202     {
00203         UByte* pdata = (UByte*)p;
00204         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00205             Construct(pdata, source);
00206     }
00207 
00208     static void ConstructArray(void* p, UPInt count, const T* psource)
00209     {
00210         UByte* pdata = (UByte*)p;
00211         for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
00212             Construct(pdata, *psource++);
00213     }
00214 
00215     static void Destruct(T* p)
00216     {
00217         p->~T();
00218         OVR_UNUSED(p); // Suppress silly MSVC warning
00219     }
00220 
00221     static void DestructArray(T* p, UPInt count)
00222     {   
00223         p += count - 1;
00224         for (UPInt i=0; i<count; ++i, --p)
00225             p->~T();
00226     }
00227 
00228     static void CopyArrayForward(T* dst, const T* src, UPInt count)
00229     {
00230         for(UPInt i = 0; i < count; ++i)
00231             dst[i] = src[i];
00232     }
00233 
00234     static void CopyArrayBackward(T* dst, const T* src, UPInt count)
00235     {
00236         for(UPInt i = count; i; --i)
00237             dst[i-1] = src[i-1];
00238     }
00239 
00240     static bool IsMovable() { return false; }
00241 };
00242 
00243 
00244 //-----------------------------------------------------------------------------------
00245 // ***** Container Allocator with movement policy
00246 //
00247 // Simple wraps as specialized allocators
00248 template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
00249 template<class T> struct ContainerAllocator     : ContainerAllocatorBase, ConstructorMov<T> {};
00250 template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
00251 
00252 
00253 } // OVR
00254 
00255 
00256 #endif


oculus_sdk
Author(s):
autogenerated on Fri Aug 28 2015 11:53:10