浅析shared_ptr的enable_shared_from_this

浅析shared_ptr的enable_shared_from_this

背景

考虑以下场景,当一个对象被shared_ptr托管时,怎么从自身返回一个shared_ptr对象

struct A
{
public:
	std::shared_ptr<A> GetSharedPtrFromThis()
	{
		return std::shared_ptr<A>(this);
	}
};

class B
{
public:
	void SetCallback(const std::shared_ptr<A>& spA)
	{
		......
	}
};

std::shared_ptr<A> spA = std::make_shared<A>();
std::shared_ptr<B> spB = std::make_shared<B>();
spB->SetCallback(spA->GetSharedPtrFromThis());

因为B不知道A的生命周期,所以如果A提供裸指针,则可能存在crash风险(无法判断一个指针是否已经被释放),而直接用this来构造一个shared_ptr更是错误,原因是shared_ptr的指针和引用计数是分离的(多个shared_ptr指向同一块内存,但各自维护引用计数)。

解决方法

struct A : std::enable_shared_from_this<A>
{
public:
	std::shared_ptr<A> GetSharedPtr()
	{
		return shared_from_this();
	}

	std::weak_ptr<A> GetWeakPtr()
	{
		return weak_from_this();
	}
};

class B
{
public:
	void SetCallback(const std::shared_ptr<A>& spA)
	{

	}
};

spB->SetCallback(spA->GetSharedPtr());

原理

enable_shared_from_this类内部持有weak_ptr

template<class _Ty>
	class enable_shared_from_this
	{	// provide member functions that create shared_ptr to this
public:
	using _Esft_type = enable_shared_from_this;
	......
	mutable weak_ptr<_Ty> _Wptr;
	};

并且这个weak_ptr是在对象构造的时候初始化的,shared_ptr的构造函数里会判断当前类是否可以enable_from_this

template<class _Yty,
	class = void>
	struct _Can_enable_shared
		: false_type
	{	// detect unambiguous and accessible inheritance from enable_shared_from_this
	};

template<class _Other,
	class _Yty>
	void _Enable_shared_from_this1(const shared_ptr<_Other>& _This, _Yty * _Ptr, true_type)
	{	
	if (_Ptr && _Ptr->_Wptr.expired())
		{
		// weak_ptr赋值
		_Ptr->_Wptr = shared_ptr<remove_cv_t<_Yty>>(_This, const_cast<remove_cv_t<_Yty> *>(_Ptr));
		}
	}

template<class _Other,
	class _Yty>
	void _Enable_shared_from_this1(const shared_ptr<_Other>&, _Yty *, false_type)
	{	// don't enable shared_from_this
	}

template<class _Other,
	class _Yty>
	void _Enable_shared_from_this(const shared_ptr<_Other>& _This, _Yty * _Ptr)
	{	// possibly enable shared_from_this
	_Enable_shared_from_this1(_This, _Ptr, bool_constant<conjunction_v<
		negation<is_array<_Other>>,
		negation<is_volatile<_Yty>>,
		_Can_enable_shared<_Yty>>>{});
	}

_Can_enable_shared里判断是否能转换,如果是继承了enable_from_this,内部有定义_Esft_type,进而再确认是否能完成转换

template<class _Yty,
	class = void>
	struct _Can_enable_shared
		: false_type
	{	// detect unambiguous and accessible inheritance from enable_shared_from_this
	};

template<class _Yty>
	struct _Can_enable_shared<_Yty, void_t<typename _Yty::_Esft_type>>
		: is_convertible<remove_cv_t<_Yty> *, typename _Yty::_Esft_type *>::type
	{	// is_convertible is necessary to verify unambiguous inheritance
	};

template<class _Ty>
	class enable_shared_from_this
	{	// provide member functions that create shared_ptr to this
public:
	using _Esft_type = enable_shared_from_this;

总结

enable_shared_from_this类持有weak_ptr,并在shared_ptr构造时初始化。使用时再将此weak_ptr传递给外部或者利用weak_ptr再构造出shared_ptr传递出去,解决异步回调传参问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值