Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef UNIQ_PTR_H
- #define UNIQ_PTR_H
- #pragma once
- namespace uniq_ptr
- {
- template <typename T>
- class UniqPtr
- {
- public:
- UniqPtr()
- {}
- UniqPtr(T* ptr)
- {
- this->Reset(ptr);
- }
- UniqPtr(UniqPtr &&ptr) : ptr_(ptr.ptr_)
- {
- ptr.ptr_ = nullptr;
- }
- UniqPtr(const UniqPtr &ptr) = delete;
- ~UniqPtr()
- {
- this->Reset();
- }
- T* operator->() const
- {
- return this->ptr_;
- }
- T& operator*() const
- {
- return *(this->ptr_);
- }
- operator T*() const
- {
- return this->ptr_;
- }
- UniqPtr& operator=(UniqPtr& ptr) = delete;
- UniqPtr& operator=(UniqPtr&& ptr)
- {
- //if (!this->Empty()) delete this->ptr_; //почему????
- this->ptr_ = ptr.ptr_;
- ptr.ptr_ = nullptr;
- return *this;
- }
- //UniqPtr& operator=(const T* ptr) = delete;
- /*UniqPtr& operator=(const T* &&ptr)
- {
- this->Reset(ptr);
- return *this;
- }*/
- UniqPtr& operator=(T* ptr)
- {
- this->Reset(ptr);
- return *this;
- }
- bool Empty() const
- {
- return !this->ptr_;
- }
- void Reset(T* ptr = nullptr)
- {
- if (!this->Empty()) delete this->ptr_;
- this->ptr_ = ptr;
- }
- T* Release()
- {
- T* ptr = this->ptr_;
- this->ptr_ = nullptr;
- return ptr;
- }
- private:
- T* ptr_ = nullptr;
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement