diff --git a/TinySTL/Memory.h b/TinySTL/Memory.h index dfa804f6a21824b43f8d2ec19919cbd411ccdde3..2e05e67bd3fdd367dc495d5c5b50903fb5ea4ff3 100644 --- a/TinySTL/Memory.h +++ b/TinySTL/Memory.h @@ -18,6 +18,7 @@ namespace TinySTL{ public: typedef T element_type; typedef D deleter_type; + typedef element_type *pointer; public: explicit unique_ptr(T *data = nullptr) :data_(data){} unique_ptr(T *data, deleter_type del) :data_(data), deleter(del){} @@ -29,7 +30,7 @@ namespace TinySTL{ unique_ptr& operator = (unique_ptr&& up){ if (&up != this){ clean(); - swap(data_, up.data_); + swap(*this, up); } } @@ -37,6 +38,28 @@ namespace TinySTL{ unique_ptr& operator = (const unique_ptr&) = delete; ~unique_ptr(){ clean(); } + + pointer get()const{ return data_; } + deleter_type& get_deleter(){ return deleter; } + const deleter_type& get_deleter()const{ return deleter; } + + operator bool()const{ return get() != nullptr; } + + pointer release(){ + auto p = nullptr; + swap(p, data_); + return p; + } + void reset(pointer p = pointer()){ + clean(); + data_ = p; + } + void swap(unique_ptr& up){ + swap(data_, up.data_); + } + + element_type operator *()const{ return *data_; } + pointer operator ->()const{ return data_; } private: inline void clean(){ deleter(data_); @@ -46,6 +69,18 @@ namespace TinySTL{ element_type *data_; deleter_type deleter; }; + template + void swap(unique_ptr& x, unique_ptr& y){ + x.swap(y); + } + template + bool operator == (const unique_ptr& lhs, const unique_ptr& rhs){ + return lhs.get() == rhs.get(); + } + template + bool operator != (const unique_ptr& lhs, const unique_ptr& rhs){ + return !(lhs == rhs); + } template unique_ptr make_unique(Args&&... args){