CPlusPlusThings/learn_class/modern_cpp_30/smart_ptr/auto_scope.cpp

102 lines
2.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by light on 19-12-9.
//
#include "../RAII/shape.h"
template <typename T> class auto_ptr {
public:
explicit auto_ptr(T *ptr = nullptr) noexcept : ptr_(ptr) {}
~auto_ptr() noexcept { delete ptr_; }
T &operator*() const noexcept { return *ptr_; }
T *operator->() const noexcept { return ptr_; }
operator bool() const noexcept { return ptr_; }
T *get() const noexcept { return ptr_; }
// 拷贝构造,被复制放释放原来指针的所有权,交给复制方
// 始终只有一个对象管理一块空间
auto_ptr(auto_ptr &other) noexcept { ptr_ = other.release(); }
// copy and swap 始终只有一个对象有管理这块空间的权限
auto_ptr &operator=(auto_ptr &rhs) noexcept {
// auto_ptr tmp(rhs.release());
// tmp.swap(*this);
// s上述两行等价于下面一行
auto_ptr(rhs.release()).swap(*this);
return *this;
}
// 原来的指针释放所有权
T *release() noexcept {
T *ptr = ptr_;
ptr_ = nullptr;
return ptr;
}
void swap(auto_ptr &rhs) noexcept {
using std::swap;
swap(ptr_, rhs.ptr_); // 转移指针所有权
}
private:
T *ptr_;
};
template <typename T> void swap(auto_ptr<T> &lhs, auto_ptr<T> &rhs) noexcept {
lhs.swap(rhs);
}
template <class T>
class scoped_ptr // noncopyable
{
public:
explicit scoped_ptr(T *ptr = 0) noexcept : ptr_(ptr) {}
~scoped_ptr() noexcept { delete ptr_; }
void reset(T *p = 0) noexcept { scoped_ptr(p).swap(*this); }
T &operator*() const noexcept { return *ptr_; }
T *operator->() const noexcept { return ptr_; }
T *get() const noexcept { return ptr_; }
void swap(scoped_ptr &rhs) noexcept {
using std::swap;
swap(ptr_, rhs.ptr_);
}
private:
T *ptr_;
scoped_ptr(scoped_ptr const &);
scoped_ptr &operator=(scoped_ptr const &);
};
template <typename T>
void swap(scoped_ptr<T> &lhs, scoped_ptr<T> &rhs) noexcept {
lhs.swap(rhs);
}
int main() {
auto_ptr<shape> ptr1{create_shape(shape_type::circle)};
auto_ptr<shape> ptr2{ptr1};
if (ptr1.get() == nullptr && ptr2.get())
cout << "拷贝构造ptr1释放了所有权,ptr2获得了所有权" << endl;
ptr1 = ptr1;
auto_ptr<shape> ptr3{create_shape(shape_type::rectangle)};
ptr1 = ptr3;
if (ptr3.get() == nullptr && ptr1.get())
cout << "赋值操作:始终只有一个对象管理一个区块!ptr3释放了所有权,"
"ptr1获得了所有权"
<< endl;
scoped_ptr<shape> sptr1{create_shape(shape_type::circle)};
// scoped_ptr<shape> sptr2{sptr1}; // error 不可拷贝
}