oculus1

diff libovr/Src/Kernel/OVR_ContainerAllocator.h @ 3:b069a5c27388

added a couple more stuff, fixed all the LibOVR line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:10:05 +0300
parents e2f9e4603129
children
line diff
     1.1 --- a/libovr/Src/Kernel/OVR_ContainerAllocator.h	Sat Sep 14 17:51:03 2013 +0300
     1.2 +++ b/libovr/Src/Kernel/OVR_ContainerAllocator.h	Sun Sep 15 04:10:05 2013 +0300
     1.3 @@ -1,1 +1,256 @@
     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
   1.261 +/************************************************************************************
   1.262 +
   1.263 +PublicHeader:   OVR.h
   1.264 +Filename    :   OVR_ContainerAllocator.h
   1.265 +Content     :   Template allocators and constructors for containers.
   1.266 +Created     :   September 19, 2012
   1.267 +Notes       : 
   1.268 +
   1.269 +Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
   1.270 +
   1.271 +Use of this software is subject to the terms of the Oculus license
   1.272 +agreement provided at the time of installation or download, or which
   1.273 +otherwise accompanies this software in either electronic or hard copy form.
   1.274 +
   1.275 +************************************************************************************/
   1.276 +
   1.277 +#ifndef OVR_ContainerAllocator_h
   1.278 +#define OVR_ContainerAllocator_h
   1.279 +
   1.280 +#include "OVR_Allocator.h"
   1.281 +#include <string.h>
   1.282 +
   1.283 +
   1.284 +namespace OVR {
   1.285 +
   1.286 +
   1.287 +//-----------------------------------------------------------------------------------
   1.288 +// ***** Container Allocator
   1.289 +
   1.290 +// ContainerAllocator serves as a template argument for allocations done by
   1.291 +// containers, such as Array and Hash; replacing it could allow allocator
   1.292 +// substitution in containers.
   1.293 +
   1.294 +class ContainerAllocatorBase
   1.295 +{
   1.296 +public:
   1.297 +    static void* Alloc(UPInt size)                { return OVR_ALLOC(size); }
   1.298 +    static void* Realloc(void* p, UPInt newSize)  { return OVR_REALLOC(p, newSize); }
   1.299 +    static void  Free(void *p)                    { OVR_FREE(p); }
   1.300 +};
   1.301 +
   1.302 +
   1.303 +
   1.304 +//-----------------------------------------------------------------------------------
   1.305 +// ***** Constructors, Destructors, Copiers
   1.306 +
   1.307 +// Plain Old Data - movable, no special constructors/destructor.
   1.308 +template<class T> 
   1.309 +class ConstructorPOD
   1.310 +{
   1.311 +public:
   1.312 +    static void Construct(void *) {}
   1.313 +    static void Construct(void *p, const T& source) 
   1.314 +    { 
   1.315 +        *(T*)p = source;
   1.316 +    }
   1.317 +
   1.318 +    // Same as above, but allows for a different type of constructor.
   1.319 +    template <class S> 
   1.320 +    static void ConstructAlt(void *p, const S& source)
   1.321 +    {
   1.322 +        *(T*)p = source;
   1.323 +    }
   1.324 +
   1.325 +    static void ConstructArray(void*, UPInt) {}
   1.326 +
   1.327 +    static void ConstructArray(void* p, UPInt count, const T& source)
   1.328 +    {
   1.329 +        UByte *pdata = (UByte*)p;
   1.330 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.331 +            *(T*)pdata = source;
   1.332 +    }
   1.333 +
   1.334 +    static void ConstructArray(void* p, UPInt count, const T* psource)
   1.335 +    {
   1.336 +        memcpy(p, psource, sizeof(T) * count);
   1.337 +    }
   1.338 +
   1.339 +    static void Destruct(T*) {}
   1.340 +    static void DestructArray(T*, UPInt) {}
   1.341 +
   1.342 +    static void CopyArrayForward(T* dst, const T* src, UPInt count)
   1.343 +    {
   1.344 +        memmove(dst, src, count * sizeof(T));
   1.345 +    }
   1.346 +
   1.347 +    static void CopyArrayBackward(T* dst, const T* src, UPInt count)
   1.348 +    {
   1.349 +        memmove(dst, src, count * sizeof(T));
   1.350 +    }
   1.351 +
   1.352 +    static bool IsMovable() { return true; }
   1.353 +};
   1.354 +
   1.355 +
   1.356 +//-----------------------------------------------------------------------------------
   1.357 +// ***** ConstructorMov
   1.358 +//
   1.359 +// Correct C++ construction and destruction for movable objects
   1.360 +template<class T> 
   1.361 +class ConstructorMov
   1.362 +{
   1.363 +public:
   1.364 +    static void Construct(void* p) 
   1.365 +    { 
   1.366 +        OVR::Construct<T>(p);
   1.367 +    }
   1.368 +
   1.369 +    static void Construct(void* p, const T& source) 
   1.370 +    { 
   1.371 +        OVR::Construct<T>(p, source);
   1.372 +    }
   1.373 +
   1.374 +    // Same as above, but allows for a different type of constructor.
   1.375 +    template <class S> 
   1.376 +    static void ConstructAlt(void* p, const S& source)
   1.377 +    {
   1.378 +        OVR::ConstructAlt<T,S>(p, source);
   1.379 +    }
   1.380 +
   1.381 +    static void ConstructArray(void* p, UPInt count)
   1.382 +    {
   1.383 +        UByte* pdata = (UByte*)p;
   1.384 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.385 +            Construct(pdata);
   1.386 +    }
   1.387 +
   1.388 +    static void ConstructArray(void* p, UPInt count, const T& source)
   1.389 +    {
   1.390 +        UByte* pdata = (UByte*)p;
   1.391 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.392 +            Construct(pdata, source);
   1.393 +    }
   1.394 +
   1.395 +    static void ConstructArray(void* p, UPInt count, const T* psource)
   1.396 +    {
   1.397 +        UByte* pdata = (UByte*)p;
   1.398 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.399 +            Construct(pdata, *psource++);
   1.400 +    }
   1.401 +
   1.402 +    static void Destruct(T* p)
   1.403 +    {
   1.404 +        p->~T();
   1.405 +        OVR_UNUSED(p); // Suppress silly MSVC warning
   1.406 +    }
   1.407 +
   1.408 +    static void DestructArray(T* p, UPInt count)
   1.409 +    {   
   1.410 +        p += count - 1;
   1.411 +        for (UPInt i=0; i<count; ++i, --p)
   1.412 +            p->~T();
   1.413 +    }
   1.414 +
   1.415 +    static void CopyArrayForward(T* dst, const T* src, UPInt count)
   1.416 +    {
   1.417 +        memmove(dst, src, count * sizeof(T));
   1.418 +    }
   1.419 +
   1.420 +    static void CopyArrayBackward(T* dst, const T* src, UPInt count)
   1.421 +    {
   1.422 +        memmove(dst, src, count * sizeof(T));
   1.423 +    }
   1.424 +
   1.425 +    static bool IsMovable() { return true; }
   1.426 +};
   1.427 +
   1.428 +
   1.429 +//-----------------------------------------------------------------------------------
   1.430 +// ***** ConstructorCPP
   1.431 +//
   1.432 +// Correct C++ construction and destruction for movable objects
   1.433 +template<class T> 
   1.434 +class ConstructorCPP
   1.435 +{
   1.436 +public:
   1.437 +    static void Construct(void* p) 
   1.438 +    { 
   1.439 +        OVR::Construct<T>(p);        
   1.440 +    }
   1.441 +
   1.442 +    static void Construct(void* p, const T& source) 
   1.443 +    { 
   1.444 +        OVR::Construct<T>(p, source);        
   1.445 +    }
   1.446 +
   1.447 +    // Same as above, but allows for a different type of constructor.
   1.448 +    template <class S> 
   1.449 +    static void ConstructAlt(void* p, const S& source)
   1.450 +    {
   1.451 +        OVR::ConstructAlt<T,S>(p, source);        
   1.452 +    }
   1.453 +
   1.454 +    static void ConstructArray(void* p, UPInt count)
   1.455 +    {
   1.456 +        UByte* pdata = (UByte*)p;
   1.457 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.458 +            Construct(pdata);
   1.459 +    }
   1.460 +
   1.461 +    static void ConstructArray(void* p, UPInt count, const T& source)
   1.462 +    {
   1.463 +        UByte* pdata = (UByte*)p;
   1.464 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.465 +            Construct(pdata, source);
   1.466 +    }
   1.467 +
   1.468 +    static void ConstructArray(void* p, UPInt count, const T* psource)
   1.469 +    {
   1.470 +        UByte* pdata = (UByte*)p;
   1.471 +        for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
   1.472 +            Construct(pdata, *psource++);
   1.473 +    }
   1.474 +
   1.475 +    static void Destruct(T* p)
   1.476 +    {
   1.477 +        p->~T();
   1.478 +        OVR_UNUSED(p); // Suppress silly MSVC warning
   1.479 +    }
   1.480 +
   1.481 +    static void DestructArray(T* p, UPInt count)
   1.482 +    {   
   1.483 +        p += count - 1;
   1.484 +        for (UPInt i=0; i<count; ++i, --p)
   1.485 +            p->~T();
   1.486 +    }
   1.487 +
   1.488 +    static void CopyArrayForward(T* dst, const T* src, UPInt count)
   1.489 +    {
   1.490 +        for(UPInt i = 0; i < count; ++i)
   1.491 +            dst[i] = src[i];
   1.492 +    }
   1.493 +
   1.494 +    static void CopyArrayBackward(T* dst, const T* src, UPInt count)
   1.495 +    {
   1.496 +        for(UPInt i = count; i; --i)
   1.497 +            dst[i-1] = src[i-1];
   1.498 +    }
   1.499 +
   1.500 +    static bool IsMovable() { return false; }
   1.501 +};
   1.502 +
   1.503 +
   1.504 +//-----------------------------------------------------------------------------------
   1.505 +// ***** Container Allocator with movement policy
   1.506 +//
   1.507 +// Simple wraps as specialized allocators
   1.508 +template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
   1.509 +template<class T> struct ContainerAllocator     : ContainerAllocatorBase, ConstructorMov<T> {};
   1.510 +template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
   1.511 +
   1.512 +
   1.513 +} // OVR
   1.514 +
   1.515 +
   1.516 +#endif