智能指针实现猜想

通过强引用和弱引用实现的自动内存管理预想,仅实现了管理内存的部分,仍有许多C++11的功能未能实现。

SharedPtr => std::shared_ptr
SentinelPtr => std::weak_ptr (为什么这样取,因为SentinelPtr 行为上更像是SharedPtr 的观察窗口)

首先,实现了SharedPtr, 这个很好理解。只要出现拷贝行为就将引用计数+1,由于多个对象间共享计数器,因此计数器必须也是指针。

难点在于,引入SentinelPtr后,同时保持SharedPtr 的行为时的条件控制。

比较形象的话,我们可以将强引用看作值的计数器,那么弱指针就相当于强引用的计数器。

欠缺:
(-) 功能还没完全实现,只是将涉及计数的部分实现了。
(-) 计数器非线程安全

template<typename T>
class SentinelPtr;

template <typename T>
class SharedPtr {
	size_t* ref_counter_ = nullptr;// 强引用计数器
	T* val_ = nullptr;              // 值引用

	size_t * soft_ref_counter_ = nullptr; // 弱引用计数器

	// template <T>
	friend class SentinelPtr<T>;
public:
	SharedPtr operator=(const SharedPtr& rhs) = delete;
	
	SharedPtr():ref_counter_(nullptr), val_(nullptr), soft_ref_counter_(nullptr){

	}

	SharedPtr(T val) :val_(new T(val)), ref_counter_(new size_t(1)), soft_ref_counter_(new size_t(1)) {

	}

	SharedPtr(T* val_pointer)  {
		if (val_pointer != nullptr) {
			val_ = val_pointer;
			ref_counter_ = new size_t(1);
			soft_ref_counter_ = new size_t(1);
		}
		else {
			val_              = nullptr;
			ref_counter_      = nullptr;
			soft_ref_counter_ = nullptr;
		}
	}

	SharedPtr(const SharedPtr<T>& rhs) {
		// 歧义: 引用计数本身必须自增,const的表达有点奇怪

		if(val_ != rhs.val_) try_release();

		soft_ref_counter_ = rhs.soft_ref_counter_;
		ref_counter_ = rhs.ref_counter_;
		val_ = rhs.val_;


		//  rhs通过默认构造的
		if (val_ == nullptr) return;

		assert(soft_ref_counter_ != nullptr);
		assert(ref_counter_      != nullptr);

		(*soft_ref_counter_)++;
		(*ref_counter_)++;
	}

	~SharedPtr() {
		if (val_ == nullptr) return;

		assert(ref_counter_ != nullptr);
		assert(val_ != nullptr);

		try_release();
		soft_ref_counter_ = nullptr;
		ref_counter_      =  nullptr;
		val_              = nullptr;
	}

public:
	size_t use_count()const {
		if (ref_counter_) return *ref_counter_;
		return 0;
	}

private:

	// 释放了val就return true
	void try_release() {
		// 可能通过空指针构造的对象
		if (!soft_ref_counter_) return;

		// 弱引用计数, 后面再一起结算
		(*soft_ref_counter_)--;

		assert(ref_counter_ != nullptr);
		(*ref_counter_)--;

		// 强引用计数为0, 释放值
		if (*ref_counter_ == 0) {
			delete val_;
			val_ = nullptr;
		}

		// 弱引用计数为0, 释放所有计数器
		if ((*soft_ref_counter_) == 0) {
			delete ref_counter_;
			delete soft_ref_counter_;

			ref_counter_ = nullptr;
			soft_ref_counter_ = nullptr;

		}
	}
};

template<typename T>
class SentinelPtr {
	size_t*  ref_counter_;      // 强引用计数器
	size_t*  soft_ref_counter_; // 弱引用计数器(相当于强引用器的引用计数)
	T*       val_;              // 提升值

public:

	SharedPtr<T> update() {
		if (!soft_ref_counter_ || !ref_counter_) return SharedPtr<T>;


		SharedPtr<T> ex;
		ex.ref_counter_      = ref_counter_;
		ex.soft_ref_counter_ = soft_ref_counter_;
		ex.val_              = val_;

		(*soft_ref_counter_)++;
		(*ref_counter_)++;

		return ex;
	}

	SentinelPtr(SharedPtr<T> sha_ptr) {
		ref_counter_ = sha_ptr.ref_counter_;
		soft_ref_counter_ = sha_ptr.soft_ref_counter_;
		val_ = sha_ptr.val_;


		if (soft_ref_counter_) {
			assert(ref_counter_ != nullptr);
			(*soft_ref_counter_)++;
		}
		else {
			assert(ref_counter_ == nullptr);
			assert(soft_ref_counter_ == nullptr);
			assert(val_ == nullptr);
		}
	}

	SentinelPtr(const SentinelPtr<T>& st_ptr) {
		if(soft_ref_counter_ != st_ptr)
			try_release();

		ref_counter_ = st_ptr.ref_counter_;
		soft_ref_counter_ = st_ptr.soft_ref_counter_;
		val_ = st_ptr.val_;

		if (soft_ref_counter_) {
			assert(ref_counter_ != nullptr);
			(*soft_ref_counter_)++;
		}
		else {
			assert(ref_counter_ == nullptr);
			assert(soft_ref_counter_ == nullptr);
			assert(val_ == nullptr);
		}
	}

	~SentinelPtr() {
		try_release();
		ref_counter_ = nullptr;
		val_ = nullptr;
		soft_ref_counter_ = nullptr;
	}

	/*
	size_t use_count() {
		if (!soft_ref_counter_ || !ref_counter_) return 0;

		return (*ref_counter_);
	}
	*/


private:
	void try_release() {
		if (!soft_ref_counter_) return;

		assert(ref_counter_ != nullptr);

		(*soft_ref_counter_)--;

		if (*soft_ref_counter_ == 0) {
			delete soft_ref_counter_;
			delete ref_counter_;

			soft_ref_counter_ = nullptr;
			ref_counter_      = nullptr;
		}
	}
};




int main()
{	
	// default ctor
	{
		SharedPtr<int> emp_ptr;

	}

	// ctor(val)
	{
		SharedPtr<int> ptr(4);
		assert(ptr.use_count() == 1);
	}

	// ctor(valpointer)
	{
		SharedPtr<int> b(new int(4));
		assert(b.use_count() == 1);
	}

	// dctr
	{
		SharedPtr<int> outer(4);

		{
			SharedPtr<int> inner(outer);
			assert(outer.use_count() == inner.use_count());
			assert(inner.use_count() == 2);
		}

		assert(outer.use_count() == 1);
	}



	{
		SharedPtr<int> outer(4);
		SentinelPtr<int> p = outer;

		assert(outer.use_count() == 1);
	}


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值