Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This is free and unencumbered software released into the public domain.
- Anyone is free to copy, modify, publish, use, compile, sell, or
- distribute this software, either in source code form or as a compiled
- binary, for any purpose, commercial or non-commercial, and by any
- means.
- In jurisdictions that recognize copyright laws, the author or authors
- of this software dedicate any and all copyright interest in the
- software to the public domain. We make this dedication for the benefit
- of the public at large and to the detriment of our heirs and
- successors. We intend this dedication to be an overt act of
- relinquishment in perpetuity of all present and future rights to this
- software under copyright law.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.*/
- #ifndef KUT_OWNERPTR_H
- #define KUT_OWNERPTR_H
- #pragma once
- #include <cassert>
- #include <utility>
- #include <type_traits>
- namespace kut
- {
- //Based on Corrade's Pointer but with some differences
- //https://github.com/mosra/corrade/blob/master/src/Corrade/Containers/Pointer.h
- template<typename T>
- class owner_ptr
- {
- //Don't use/allow this class for arrays
- static_assert(!std::is_array<T>::value, "owner_ptr cannot refer to arrays");
- //Used in the move constructors for convertible types
- template<typename U> friend class owner_ptr;
- public:
- //Some typedefs to match unique_ptr, I don't bother to use them internally
- using pointer = T*;
- using element_type = T;
- //implicit construction of null pointer
- owner_ptr(std::nullptr_t = nullptr) noexcept : _p() {}
- //explicit construction with a pointer value
- explicit owner_ptr(T* p) noexcept : _p(p) {}
- //copy constructor is deleted
- owner_ptr(const owner_ptr<T>&) = delete;
- //move constructor
- template<typename U>
- owner_ptr(owner_ptr<U>&& other) noexcept : _p(static_cast<T*>(other._p))
- {
- other._p = nullptr;
- }
- //assignment copy is deleted
- owner_ptr<T>& operator=(const owner_ptr<T>&) = delete;
- //move assignment
- template<typename U>
- owner_ptr<T>& operator=(owner_ptr<U>&& other) noexcept
- {
- //swap our pointers
- auto temp = other._p;
- other._p = static_cast<U*>(_p);
- _p = static_cast<T*>(temp);
- }
- //comparisons to null pointer
- bool operator==(std::nullptr_t) const noexcept { return _p == nullptr; }
- bool operator!=(std::nullptr_t) const noexcept { return _p != nullptr; }
- //destructor
- ~owner_ptr() { delete _p; }
- //bool conversion to check validity
- explicit operator bool() const noexcept { return _p; }
- //retrieve underlying pointer
- T* get() noexcept { return _p; }
- const T* get() const noexcept { return _p; }
- //call through to pointer object
- T* operator->() noexcept
- {
- assert(_p);
- return _p;
- }
- const T* operator->() const noexcept
- {
- assert(p);
- return _p;
- }
- //dereference to underlying object
- T& operator*() noexcept
- {
- assert(p);
- return _p;
- }
- const T& operator*() const noexcept
- {
- assert(p);
- return _p;
- }
- //clear out the pointer
- void reset(T* pointer = nullptr) noexcept
- {
- delete _p;
- _p = pointer;
- }
- T* release() noexcept
- {
- auto ptr = _p;
- _p = nullptr;
- return ptr;
- }
- private:
- T* _p;
- };
- //reverse equality comparisons
- template<typename T> bool operator==(std::nullptr_t, const owner_ptr<T>& p) { return p == nullptr; }
- template<typename T> bool operator!=(std::nullptr_t, const owner_ptr<T>& p) { return p != nullptr; }
- //make_unique equivalent, construct pointer and underlying object
- template<typename T, typename... Args>
- inline owner_ptr<T> make_owner(Args&&... args)
- {
- return owner_ptr<T>(new T(std::forward<Args>(args)...));
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement