Advertisement
Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace StdLib
  4. {
  5.     template <uiw Size> class DataHolder
  6.     {
  7.         static constexpr uiw size = (Size & 15) ? ((Size & ~15) + 16) : Size;
  8.         union
  9.         {
  10.             void *_address;
  11.             alignas(16) ui8 _local[size];
  12.         };
  13.         void (*_onMoving)(DataHolder &target, DataHolder &source);
  14.     #ifdef DEBUG
  15.         bool _isDestroyed = false;
  16.         bool _isMovedFrom = false;
  17.     #endif
  18.  
  19.     public:
  20.     #ifdef DEBUG
  21.         ~DataHolder()
  22.         {
  23.             ASSUME(_isDestroyed || _isMovedFrom);
  24.         }
  25.     #endif
  26.  
  27.         DataHolder() = delete;
  28.  
  29.         template <typename T> DataHolder(T &&source)
  30.         {
  31.             static_assert(alignof(T) <= 16, "Objects with pecular alignment requirements are not allowed");
  32.  
  33.             if constexpr (sizeof(T) <= size)
  34.             {
  35.                 new (_local) T(std::forward<T>(source));
  36.                 _onMoving = [](DataHolder &target, DataHolder &source)
  37.                 {
  38.                     new (target._local) T(std::move(source.Get<T>()));
  39.                     target._onMoving = source._onMoving;
  40.                 #ifdef DEBUG
  41.                     source._isMovedFrom = true;
  42.                 #endif
  43.                 };
  44.             }
  45.             else
  46.             {
  47.                 _address = new T(std::forward<T>(source));
  48.                 _onMoving = [](DataHolder &target, DataHolder &source)
  49.                 {
  50.                     target._address = source._address;
  51.                     source._address = nullptr;
  52.                     target._onMoving = source._onMoving;
  53.                 #ifdef DEBUG
  54.                     source._isMovedFrom = true;
  55.                 #endif
  56.                 };
  57.             }
  58.         }
  59.  
  60.         DataHolder(DataHolder &&source)
  61.         {
  62.             source._onMoving(*this, source);
  63.         }
  64.  
  65.         DataHolder &operator = (DataHolder &&source)
  66.         {
  67.             source._onMoving(*this, source);
  68.             return *this;
  69.         }
  70.  
  71.         template <typename T> void Destroy()
  72.         {
  73.         #ifdef DEBUG
  74.             ASSUME(!_isDestroyed);
  75.             _isDestroyed = true;
  76.         #endif
  77.             if constexpr (sizeof(T) <= size)
  78.             {
  79.                 ((T *)_local)->~T();
  80.             }
  81.             else
  82.             {
  83.                 T *casted = (T *)_address;
  84.                 delete casted;
  85.             }
  86.         }
  87.  
  88.         template <typename T> T &Get()
  89.         {
  90.         #ifdef DEBUG
  91.             ASSUME(!_isDestroyed && !_isMovedFrom);
  92.         #endif
  93.             if constexpr (sizeof(T) <= size)
  94.             {
  95.                 return *(T *)_local;
  96.             }
  97.             else
  98.             {
  99.                 return *(T *)_address;
  100.             }
  101.         }
  102.  
  103.         template <typename T> const T &Get() const
  104.         {
  105.         #ifdef DEBUG
  106.             ASSUME(!_isDestroyed && !_isMovedFrom);
  107.         #endif
  108.             if constexpr (sizeof(T) <= size)
  109.             {
  110.                 return *(T *)_local;
  111.             }
  112.             else
  113.             {
  114.                 return *(T *)_address;
  115.             }
  116.         }
  117.  
  118.         template <typename T> constexpr bool IsPlacedLocally() const
  119.         {
  120.             return sizeof(T) <= size;
  121.         }
  122.     };
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement