Advertisement
Guest User

Proper Moving?

a guest
Dec 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. class NonCopyableType
  2. {
  3. private:
  4.     struct NonCopyableTypeAllocator : std::allocator<NonCopyableType>
  5.     {
  6.         template <class U, class... Args>
  7.         void construct(U* p, Args&&... args)
  8.         {
  9.             ::new ((void*)p) U(std::forward<Args>(args)...);
  10.         }
  11.  
  12.         template <class U>
  13.         struct rebind
  14.         {
  15.             typedef NonCopyableTypeAllocator other;
  16.         };
  17.     };
  18.  
  19.     friend class NonCopyableTypeAllocator;
  20.     friend class NonCopyableTypeList;
  21.    
  22.     u8* myData = nullptr;
  23.  
  24.     NonCopyableType(u8* data, size_t& offset) : myData(data + offset) {}
  25.     NonCopyableType(const NonCopyableType&) = delete;
  26.     NonCopyableType(NonCopyableType&&)      = default;
  27.     NonCopyableType& operator=(const NonCopyableType&) = delete;
  28.     NonCopyableType& operator=(NonCopyableType&&) = default;
  29. };
  30.  
  31. class NonCopyableTypeList : private std::vector<NonCopyableType, NonCopyableType::NonCopyableTypeAllocator>
  32. {
  33. public:
  34.     NonCopyableTypeList(std::shared_ptr<u8[]> data, size_t length); // would initialize associatedData, length, and add a bunch of NonCopyableTypes to itself
  35.     NonCopyableTypeList(NonCopyableTypeList&& other)
  36.         : std::vector<NonCopyableType, NonCopyableType::NonCopyableTypeAllocator>(std::forward<std::vector<NonCopyableType, NonCopyableType::NonCopyableTypeAllocator>>(other)),
  37.           associatedData(std::move(other.associatedData)),
  38.           length(std::move(other.length))
  39.     {
  40.     }
  41.     NonCopyableTypeList& operator=(NonCopyableTypeList&& other)
  42.     {
  43.         std::vector<NonCopyableType, NonCopyableType::NonCopyableTypeAllocator>::operator=(std::forward<std::vector<NonCopyableType, NonCopyableType::NonCopyableTypeAllocator>>(other));
  44.         length                                                   = std::move(other.length);
  45.         associatedData                                           = std::move(other.associatedData);
  46.         return *this;
  47.     }
  48.  
  49.     using std::vector<NonCopyableType, NonCopyableType::NonCopyableTypeAllocator>::operator[];
  50.  
  51. private:
  52.     NonCopyableTypeList(const NonCopyableTypeList&) = delete;
  53.     NonCopyableTypeList& operator=(const NonCopyableTypeList&) = delete;
  54.     std::shared_ptr<u8[]> associatedData;
  55.     size_t length;
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement