Advertisement
PromitRoy

owner_ptr (replaces unique_ptr)

Jan 19th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. /*This is free and unencumbered software released into the public domain.
  2.  
  3. Anyone is free to copy, modify, publish, use, compile, sell, or
  4. distribute this software, either in source code form or as a compiled
  5. binary, for any purpose, commercial or non-commercial, and by any
  6. means.
  7.  
  8. In jurisdictions that recognize copyright laws, the author or authors
  9. of this software dedicate any and all copyright interest in the
  10. software to the public domain. We make this dedication for the benefit
  11. of the public at large and to the detriment of our heirs and
  12. successors. We intend this dedication to be an overt act of
  13. relinquishment in perpetuity of all present and future rights to this
  14. software under copyright law.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. OTHER DEALINGS IN THE SOFTWARE.*/
  23.  
  24. #ifndef KUT_OWNERPTR_H
  25. #define KUT_OWNERPTR_H
  26. #pragma once
  27.  
  28. #include <cassert>
  29. #include <utility>
  30. #include <type_traits>
  31.  
  32. namespace kut
  33. {
  34.     //Based on Corrade's Pointer but with some differences
  35.     //https://github.com/mosra/corrade/blob/master/src/Corrade/Containers/Pointer.h
  36.  
  37.     template<typename T>
  38.     class owner_ptr
  39.     {
  40.         //Don't use/allow this class for arrays
  41.         static_assert(!std::is_array<T>::value, "owner_ptr cannot refer to arrays");
  42.         //Used in the move constructors for convertible types
  43.         template<typename U> friend class owner_ptr;
  44.  
  45.     public:
  46.         //Some typedefs to match unique_ptr, I don't bother to use them internally
  47.         using pointer = T*;
  48.         using element_type = T;
  49.        
  50.         //implicit construction of null pointer
  51.         owner_ptr(std::nullptr_t = nullptr) noexcept : _p() {}
  52.         //explicit construction with a pointer value
  53.         explicit owner_ptr(T* p) noexcept : _p(p) {}
  54.  
  55.         //copy constructor is deleted
  56.         owner_ptr(const owner_ptr<T>&) = delete;
  57.         //move constructor
  58.         template<typename U>
  59.         owner_ptr(owner_ptr<U>&& other) noexcept : _p(static_cast<T*>(other._p))
  60.         {
  61.             other._p = nullptr;
  62.         }
  63.  
  64.         //assignment copy is deleted
  65.         owner_ptr<T>& operator=(const owner_ptr<T>&) = delete;
  66.         //move assignment
  67.         template<typename U>
  68.         owner_ptr<T>& operator=(owner_ptr<U>&& other) noexcept
  69.         {
  70.             //swap our pointers
  71.             auto temp = other._p;
  72.             other._p = static_cast<U*>(_p);
  73.             _p = static_cast<T*>(temp);
  74.         }
  75.  
  76.         //comparisons to null pointer
  77.         bool operator==(std::nullptr_t) const noexcept { return _p == nullptr; }
  78.         bool operator!=(std::nullptr_t) const noexcept { return _p != nullptr; }
  79.  
  80.         //destructor
  81.         ~owner_ptr() { delete _p; }
  82.  
  83.         //bool conversion to check validity
  84.         explicit operator bool() const noexcept { return _p; }
  85.  
  86.         //retrieve underlying pointer
  87.         T* get() noexcept { return _p; }
  88.         const T* get() const noexcept { return _p; }
  89.  
  90.         //call through to pointer object
  91.         T* operator->() noexcept
  92.         {
  93.             assert(_p);
  94.             return _p;
  95.         }
  96.  
  97.         const T* operator->() const noexcept
  98.         {
  99.             assert(p);
  100.             return _p;
  101.         }
  102.  
  103.         //dereference to underlying object
  104.         T& operator*() noexcept
  105.         {
  106.             assert(p);
  107.             return _p;
  108.         }
  109.  
  110.         const T& operator*() const noexcept
  111.         {
  112.             assert(p);
  113.             return _p;
  114.         }
  115.  
  116.         //clear out the pointer
  117.         void reset(T* pointer = nullptr) noexcept
  118.         {
  119.             delete _p;
  120.             _p = pointer;
  121.         }
  122.  
  123.         T* release() noexcept
  124.         {
  125.             auto ptr = _p;
  126.             _p = nullptr;
  127.             return ptr;
  128.         }
  129.  
  130.     private:
  131.         T* _p;
  132.     };
  133.  
  134.     //reverse equality comparisons
  135.     template<typename T> bool operator==(std::nullptr_t, const owner_ptr<T>& p) { return p == nullptr; }
  136.     template<typename T> bool operator!=(std::nullptr_t, const owner_ptr<T>& p) { return p != nullptr; }
  137.  
  138.     //make_unique equivalent, construct pointer and underlying object
  139.     template<typename T, typename... Args>
  140.     inline owner_ptr<T> make_owner(Args&&... args)
  141.     {
  142.         return owner_ptr<T>(new T(std::forward<Args>(args)...));
  143.     }
  144. }
  145.  
  146. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement