您的位置:首页 > 编程语言 > C语言/C++

Visual C++ 2015 下的 enable_shared_from_this 原理简析

2015-07-28 22:42 806 查看
一般来说,搞到一个
shared_ptr
有两种常见方式:

使用构造函数,如:

[code]  std::shared_ptr<foo> ptr{new foo{}};


使用
make_shared
allocate_shared
),如:

[code]  auto ptr = std::make_shared<foo>();


在 Visual C++ 2015 下,第二种方式也走的是构造函数:

[code]    // TEMPLATE FUNCTION make_shared
template<class _Ty,
    class... _Types> inline
        shared_ptr<_Ty> make_shared(_Types&&... _Args)
    {   // make a shared_ptr
    _Ref_count_obj<_Ty> *_Rx =
        new _Ref_count_obj<_Ty>(_STD forward<_Types>(_Args)...);

    shared_ptr<_Ty> _Ret;
    _Ret._Resetp0(_Rx->_Getptr(), _Rx);
    return (_Ret);
    }


如果你去翻阅 Visual C++ 2015 下的
<memory>
头文件,你会发现所有的 ctor 都将工作委托给某个叫做
_Resetp
的函数:

[code]template<class _Ux>
        explicit shared_ptr(_Ux *_Px)
        {   // construct shared_ptr object that owns _Px
        _Resetp(_Px);
        }


_Resetp
又会把工作委托给
_Resetp0


[code]
template<class _Ux>
        void _Resetp0(_Ux *_Px, _Ref_count_base *_Rx)
        {   // release resource and take ownership of _Px
        this->_Reset0(_Px, _Rx);
        _Enable_shared(_Px, _Rx);
        }
    };


_Resetp0
中会调用
_Enable_shared
函数:

[code]template<class _Ty>
    inline void _Enable_shared(_Ty *_Ptr, _Ref_count_base *_Refptr,
        typename _Ty::_EStype * = 0)
    {   // reset internal weak pointer
    if (_Ptr)
        _Do_enable(_Ptr,
            (enable_shared_from_this<typename _Ty::_EStype>*)_Ptr, _Refptr);
    }

inline void _Enable_shared(const volatile void *, const volatile void *)
    {   // not derived from enable_shared_from_this; do nothing
    }


注意:

如果
_Ty
有一个
_EStype
,那么执行第一个;

如果没有,执行第二个(no-op)。

现在让我们来看看
enable_shared_from_this


[code]
 template<class _Ty>
    class enable_shared_from_this
    {   // provide member functions that create shared_ptr to this
public:
    typedef _Ty _EStype;

    shared_ptr<_Ty> shared_from_this()
        {   // return shared_ptr
        return (shared_ptr<_Ty>(_Wptr));
        }

    shared_ptr<const _Ty> shared_from_this() const
        {   // return shared_ptr
        return (shared_ptr<const _Ty>(_Wptr));
        }

protected:
    _CONST_FUN enable_shared_from_this() _NOEXCEPT
        {   // construct (do nothing)
        }

    enable_shared_from_this(const enable_shared_from_this&) _NOEXCEPT
        {   // construct (do nothing)
        }

    enable_shared_from_this&
        operator=(const enable_shared_from_this&) _NOEXCEPT
        {   // assign (do nothing)
        return (*this);
        }

    ~enable_shared_from_this() _NOEXCEPT
        {   // destroy (do nothing)
        }

private:
    template<class _Ty1,
        class _Ty2>
        friend void _Do_enable(
            _Ty1 *,
            enable_shared_from_this<_Ty2>*,
            _Ref_count_base *);

    weak_ptr<_Ty> _Wptr;
    };


需要注意的:

它有一个上文提到的
_EStype


它有一个
weak_ptr
成员。

也就是说,实际上上一个
_Enable_shared
函数遇到
enable_shared_from_this
及其子类会执行第一个,其它的是 no-op。那么第一个干了什么?

[code]  template<class _Ty1,
    class _Ty2>
    inline void _Do_enable(
        _Ty1 *_Ptr,
        enable_shared_from_this<_Ty2> *_Es,
        _Ref_count_base *_Refptr)
    {   // reset internal weak pointer
    _Es->_Wptr._Resetw(_Ptr, _Refptr);
    }


哦,初始化了
enable_share_from_this
weak_ptr
。有了
weak_ptr
,其
share_from_this
无非就是:

[code]shared_ptr<_Ty> shared_from_this()
        {   // return shared_ptr
        return (shared_ptr<_Ty>(_Wptr));
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: