- 唯一指针
unique_ptr
#ifndef _UNIQUE_PTR_H
#define __UNIQUE_H
#include <utility>
#include <iostream>
class Delete {
public:
template<typename T>
void operator()(T* p) const {
delete p;
}
};
template<typename T, typename D = Delete >
class unique_ptr {
public:
explicit unique_ptr(T* pp = nullptr, const D& dd = D()) :
un_ptr(pp), del(dd)
{
}
~unique_ptr()
{
del(un_ptr);
}
/* 不支持拷贝与赋值 */
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
/* 移动赋值与移动拷贝 */
unique_ptr(unique_ptr&& right_value) :
un_ptr(right_value.un_ptr), del(std::move(right_value.del)) {
right_value.un_ptr = nullptr;
}
unique_ptr& operator=(unique_ptr&& right_value) noexcept {
if (this != &right_value) {
std::cout << "operator && right_value " << std::endl;
del(&(**this));
un_ptr = right_value.un_ptr;
del = std::move(right_value.del);
right_value.un_ptr = nullptr;
}
return *this;
}
//u.release() u 放弃对指针的控制权,返回指针,并将 u 置为空
T* release() {
T* tmp = un_ptr;
un_ptr = nullptr;
return tmp;
}
/*
u.reset() 释放u指向的对象
u.reset(q) 如果提供了内置指针q,就令u指向这个对象
u.reset(nullptr) 将 u 置为空
*/
void reset(T* q = nullptr) {
del(un_ptr);
un_ptr = q;
}
void swap(unique_ptr& other) noexcept {
std::swap(un_ptr, other.un_ptr);
std::swap(del, other.del);
}
T* get() { return un_ptr; }
const D& get_deleter() { return del; }
T& operator*() { return *un_ptr; }
T* operator->() { return un_ptr; }
private:
T* un_ptr = nullptr;
D del;
};
#endif
- 共享指针
shared_ptr
template <typename T>
class shared_ptr {
private:
T* ptr;
int* ref_count;
void release() {
if (ref_count) {
--(*ref_count);
if (*ref_count == 0) {
delete ptr;
delete ref_count;
}
ptr = nullptr;
ref_count = nullptr;
}
}
public:
shared_ptr() : ptr(nullptr), ref_count(nullptr) {}
shared_ptr(T* p) : ptr(p), ref_count(new int(1)) {}
shared_ptr(const shared_ptr& other) : ptr(other.ptr), ref_count(other.ref_count) {
if (ref_count) {
++(*ref_count);
}
}
~shared_ptr() {
release();
}
shared_ptr& operator=(const shared_ptr& other) {
if (this != &other) {
release();
ptr = other.ptr;
ref_count = other.ref_count;
if (ref_count) {
++(*ref_count);
}
}
return *this;
}
T* get() const {
return ptr;
}
int use_count() const {
return ref_count ? *ref_count : 0;
}
void reset() {
release();
}
void reset(T* p) {
release();
ptr = p;
ref_count = new int(1);
}
T& operator*() const {
return *ptr;
}
T* operator->() const {
return ptr;
}
};
- 弱指针
weak_ptr
因为引入了弱引用,因此需要区分强弱引用计数,强引用计数归0时才释放对象(由shared_ptr管理),弱引用计数不影响(由weak_ptr管理),并且这两类型指针可以互相由对方构造,因此互相为友元类。弱引用实际上也持有管理对象的引用,只是不负责其生命周期的管理,只负责对应对象弱引用计数的管理,在弱引用计数为0时,如果对象强引用计数为0,将弱引用持有的计数器释放,否则不释放计数器。该指针可以通过lock方法来转换为可用(可访问对象资源)的共享指针。
class Counter
{
public:
Counter() :s(0), w(0){};
int s;
int w;
};
template<class T>
class WeakPtr
{
public://给出默认构造和拷贝构造,其中拷贝构造不能有从原始指针进行构造
WeakPtr()
{
_ptr = 0;
cnt = 0;
cout << "WeakPtr construct " << endl;
}
WeakPtr(SharePtr<T>& s) :
_ptr(s._ptr), cnt(s.cnt)
{
cout << "w con s" << endl;
cnt->w++;
}
WeakPtr(WeakPtr<T>& w) :
_ptr(w._ptr), cnt(w.cnt)
{
cnt->w++;
}
~WeakPtr()
{
release();
}
WeakPtr<T>& operator =(WeakPtr<T> & w)
{
if (this != &w)
{
release();
cnt = w.cnt;
cnt->w++;
_ptr = w._ptr;
}
return *this;
}
WeakPtr<T>& operator =(SharePtr<T> & s)
{
cout << "w = s" << endl;
release();
cnt = s.cnt;
cnt->w++;
_ptr = s._ptr;
return *this;
}
SharePtr<T> lock()
{
return SharePtr<T>(*this);
}
// 检查弱引用是否失效
bool expired()
{
if (cnt)
{
if (cnt->s >0)
{
cout << "empty " << cnt->s << endl;
return false;
}
}
return true;
}
friend class SharePtr<T>;//方便weak_ptr与share_ptr设置引用计数和赋值。
private:
void release()
{
cout << "into WeakPtr release" << endl;
if (cnt)
{
cnt->w--;
if (cnt->w <1 && cnt->s <1)
{
cout << "weakptr release" << endl;
cnt = NULL;
}
}
}
T* _ptr;
Counter* cnt;
};