📄 memory
字号:
void *>::type * = 0)
: _Mybase(_STD forward<shared_ptr<_Ty2> >(_Right))
{ // construct shared_ptr object that takes resource from _Right
}
#if _HAS_CPP0X
template<class _Ux,
class _Dx>
shared_ptr(_STD unique_ptr<_Ux, _Dx>&& _Right)
{ // construct from unique_ptr
_Resetp(_Right.release(), _Right.get_deleter());
}
template<class _Ux,
class _Dx>
_Myt& operator=(unique_ptr<_Ux, _Dx>&& _Right)
{ // move from unique_ptr
shared_ptr(_STD move(_Right)).swap(*this);
return (*this);
}
#endif /* _HAS_CPP0X */
_Myt& operator=(_Myt&& _Right)
{ // construct shared_ptr object that takes resource from _Right
shared_ptr(_STD move(_Right)).swap(*this);
return (*this);
}
template<class _Ty2>
_Myt& operator=(shared_ptr<_Ty2>&& _Right)
{ // construct shared_ptr object that takes resource from _Right
shared_ptr(_STD move(_Right)).swap(*this);
return (*this);
}
void swap(_Myt&& _Right)
{ // exchange contents with movable _Right
_Mybase::swap(_STD move(_Right));
}
~shared_ptr()
{ // release resource
this->_Decref();
}
_Myt& operator=(const _Myt& _Right)
{ // assign shared ownership of resource owned by _Right
shared_ptr(_Right).swap(*this);
return (*this);
}
template<class _Ty2>
_Myt& operator=(const shared_ptr<_Ty2>& _Right)
{ // assign shared ownership of resource owned by _Right
shared_ptr(_Right).swap(*this);
return (*this);
}
template<class _Ty2>
_Myt& operator=(auto_ptr<_Ty2>& _Right)
{ // assign ownership of resource pointed to by _Right
shared_ptr(_Right).swap(*this);
return (*this);
}
void reset()
{ // release resource and convert to empty shared_ptr object
shared_ptr().swap(*this);
}
template<class _Ux>
void reset(_Ux *_Px)
{ // release, take ownership of _Px
shared_ptr(_Px).swap(*this);
}
template<class _Ux,
class _Dx>
void reset(_Ux *_Px, _Dx _Dt)
{ // release, take ownership of _Px, with deleter _Dt
shared_ptr(_Px, _Dt).swap(*this);
}
//#if _HAS_CPP0X
template<class _Ux,
class _Dx,
class _Alloc>
void reset(_Ux *_Px, _Dx _Dt, _Alloc _Ax)
{ // release, take ownership of _Px, with deleter _Dt, allocator _Ax
shared_ptr(_Px, _Dt, _Ax).swap(*this);
}
//#endif /* _HAS_CPP0X */
void swap(_Myt& _Other)
{ // swap pointers
this->_Swap(_Other);
}
_Ty *get() const
{ // return pointer to resource
return (this->_Get());
}
typename tr1::add_reference<_Ty>::type operator*() const
{ // return reference to resource
return (*this->_Get());
}
_Ty *operator->() const
{ // return pointer to resource
return (this->_Get());
}
bool unique() const
{ // return true if no other shared_ptr object owns this resource
return (this->use_count() == 1);
}
_OPERATOR_BOOL() const
{ // test if shared_ptr object owns no resource
return (this->_Get() != 0 ? _CONVERTIBLE_TO_TRUE : 0);
}
private:
template<class _Ux>
void _Resetp(_Ux *_Px)
{ // release, take ownership of _Px
_TRY_BEGIN // allocate control block and reset
_Resetp0(_Px, new _Ref_count<_Ux>(_Px));
_CATCH_ALL // allocation failed, delete resource
delete _Px;
_RERAISE;
_CATCH_END
}
template<class _Ux,
class _Dx>
void _Resetp(_Ux *_Px, _Dx _Dt)
{ // release, take ownership of _Px, deleter _Dt
_TRY_BEGIN // allocate control block and reset
_Resetp0(_Px, new _Ref_count_del<_Ux, _Dx>(_Px, _Dt));
_CATCH_ALL // allocation failed, delete resource
_Dt(_Px);
_RERAISE;
_CATCH_END
}
//#if _HAS_CPP0X
template<class _Ux,
class _Dx,
class _Alloc>
void _Resetp(_Ux *_Px, _Dx _Dt, _Alloc _Ax)
{ // release, take ownership of _Px, deleter _Dt, allocator _Ax
typedef _Ref_count_del_alloc<_Ux, _Dx, _Alloc> _Refd;
typename _Alloc::template rebind<_Refd>::other _Al = _Ax;
_TRY_BEGIN // allocate control block and reset
_Refd *_Ptr = _Al.allocate(1);
new (_Ptr) _Refd(_Px, _Dt, _Al);
_Resetp0(_Px, _Ptr);
_CATCH_ALL // allocation failed, delete resource
_Dt(_Px);
_RERAISE;
_CATCH_END
}
//#endif /* _HAS_CPP0X */
public:
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);
}
};
template<class _Ty1,
class _Ty2>
bool operator==(const shared_ptr<_Ty1>& _S1,
const shared_ptr<_Ty2>& _S2)
{ // test if shared_ptr == shared_ptr
return (_S1.get() == _S2.get());
}
template<class _Ty1,
class _Ty2>
bool operator!=(const shared_ptr<_Ty1>& _S1,
const shared_ptr<_Ty2>& _S2)
{ // test if shared_ptr != shared_ptr
return (!(_S1 == _S2));
}
template<class _Ty1,
class _Ty2>
bool operator<(const shared_ptr<_Ty1>& _S1,
const shared_ptr<_Ty2>& _S2)
{ // test if shared_ptr < shared_ptr
return (_S1.get() < _S2.get());
}
template<class _Ty1,
class _Ty2>
bool operator>=(const shared_ptr<_Ty1>& _S1,
const shared_ptr<_Ty2>& _S2)
{ // shared_ptr >= shared_ptr
return (!(_S1 < _S2));
}
template<class _Ty1,
class _Ty2>
bool operator>(const shared_ptr<_Ty1>& _S1,
const shared_ptr<_Ty2>& _S2)
{ // test if shared_ptr > shared_ptr
return (_S2 < _S1);
}
template<class _Ty1,
class _Ty2>
bool operator<=(const shared_ptr<_Ty1>& _S1,
const shared_ptr<_Ty2>& _S2)
{ // test if shared_ptr <= shared_ptr
return (!(_S2 < _S1));
}
template<class _Elem,
class _Traits,
class _Ty>
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Out,
const shared_ptr<_Ty>& _Px)
{ // write contained pointer to stream
return (_Out << _Px.get());
}
template<class _Ty>
void swap(shared_ptr<_Ty>& _Left,
shared_ptr<_Ty>& _Right)
{ // swap _Left and _Right shared_ptrs
_Left.swap(_Right);
}
template<class _Ty>
void swap(shared_ptr<_Ty>& _Left,
shared_ptr<_Ty>&& _Right)
{ // swap _Left and _Right shared_ptrs
_Left.swap(_Right);
}
template<class _Ty>
void swap(shared_ptr<_Ty>&& _Left,
shared_ptr<_Ty>& _Right)
{ // swap _Left and _Right shared_ptrs
_Right.swap(_Left);
}
template<class _Ty1,
class _Ty2>
shared_ptr<_Ty1> static_pointer_cast(const shared_ptr<_Ty2>& _Other)
{ // return shared_ptr object holding static_cast<_Ty1 *>(_Other.get())
return (shared_ptr<_Ty1>(_Other, _Static_tag()));
}
template<class _Ty1,
class _Ty2>
shared_ptr<_Ty1> const_pointer_cast(const shared_ptr<_Ty2>& _Other)
{ // return shared_ptr object holding const_cast<_Ty1 *>(_Other.get())
return (shared_ptr<_Ty1>(_Other, _Const_tag()));
}
template<class _Ty1,
class _Ty2>
shared_ptr<_Ty1> dynamic_pointer_cast(const shared_ptr<_Ty2>& _Other)
{ // return shared_ptr object holding dynamic_cast<_Ty1 *>(_Other.get())
return (shared_ptr<_Ty1>(_Other, _Dynamic_tag()));
}
template<class _Dx,
class _Ty>
_Dx *get_deleter(const shared_ptr<_Ty>& _Sx)
{ // return pointer to shared_ptr's deleter object if its type is _Ty
return ((_Dx *)_Sx._Get_deleter(typeid(_Dx)));
}
#if _HAS_CPP0X
// TEMPLATE CLASS _Ref_count_obj
template<class _Ty>
class _Ref_count_obj
: public _Ref_count_base
{ // handle reference counting for object in control block, no allocator
public:
#define _REF_COUNT_OBJ_CTOR
#define _INCL_FILE_xxshared
#include <xfwrap>
#undef _REF_COUNT_OBJ_CTOR
_Ty *_Getptr() const
{ // get pointer
return ((_Ty *)&_Storage);
}
private:
virtual void _Destroy()
{ // destroy managed resource
_Getptr()->~_Ty();
}
virtual void _Delete_this()
{ // destroy self
delete this;
}
typename aligned_storage<sizeof(_Ty),
alignment_of<_Ty>::value>::type _Storage;
};
// TEMPLATE CLASS _Ref_count_obj_alloc
template<class _Ty,
class _Alloc>
class _Ref_count_obj_alloc
: public _Ref_count_base
{ // handle reference counting for object in control block, allocator
public:
typedef _Ref_count_obj_alloc<_Ty, _Alloc> _Myty;
typedef typename _Alloc::template rebind<_Myty>::other _Myalty;
#define _REF_COUNT_OBJ_ALLOC_CTOR
#define _INCL_FILE_xxshared
#include <xfwrap>
#undef _REF_COUNT_OBJ_ALLOC_CTOR
_Ty *_Getptr() const
{ // get pointer
return ((_Ty *)&_Storage);
}
private:
virtual void _Destroy()
{ // destroy managed resource
_Getptr()->~_Ty();
}
virtual void _Delete_this()
{ // destroy self
_Myalty _Al = _Myal;
_Dest_val(_Al, this);
_Al.deallocate(this, 1);
}
typename aligned_storage<sizeof (_Ty),
alignment_of<_Ty>::value>::type _Storage;
_Myalty _Myal; // the stored allocator for this
};
#define _MAKE_SHARED
#define _INCL_FILE_xxshared
#include <xfwrap>
#undef _MAKE_SHARED
#endif /* _HAS_CPP0X */
// TEMPLATE CLASS weak_ptr
template<class _Ty>
class weak_ptr
: public _Ptr_base<_Ty>
{ // class for pointer to reference counted resource
typedef typename _Ptr_base<_Ty>::_Elem _Elem;
public:
weak_ptr()
{ // construct empty weak_ptr object
}
template<class _Ty2>
weak_ptr(const shared_ptr<_Ty2>& _Other,
typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
void *>::type * = 0)
{ // construct weak_ptr object for resource owned by _Other
this->_Resetw(_Other);
}
weak_ptr(const weak_ptr& _Other)
{ // construct weak_ptr object for resource pointed to by _Other
this->_Resetw(_Other);
}
template<class _Ty2>
weak_ptr(const weak_ptr<_Ty2>& _Other,
typename enable_if<is_convertible<_Ty2 *, _Ty *>::value,
void *>::type * = 0)
{ // construct weak_ptr object for resource pointed to by _Other
this->_Resetw(_Other);
}
~weak_ptr()
{ // release resource
this->_Decwref();
}
weak_ptr& operator=(const weak_ptr& _Right)
{ // assign from _Right
this->_Resetw(_Right);
return (*this);
}
template<class _Ty2>
weak_ptr& operator=(const weak_ptr<_Ty2>& _Right)
{ // assign from _Right
this->_Resetw(_Right);
return (*this);
}
template<class _Ty2>
weak_ptr& operator=(shared_ptr<_Ty2>& _Right)
{ // assign from _Right
this->_Resetw(_Right);
return (*this);
}
void reset()
{ // release resource, convert to null weak_ptr object
this->_Resetw();
}
void swap(weak_ptr& _Other)
{ // swap pointers
this->_Swap(_Other);
}
bool expired() const
{ // return true if resource no longer exists
return (this->_Expired());
}
shared_ptr<_Ty> lock() const
{ // convert to shared_ptr
return (shared_ptr<_Elem>(*this, false));
}
};
//template<class _Ty1,
// class _Ty2>
// bool operator<(const weak_ptr<_Ty1>& _W1,
// const weak_ptr<_Ty2>& _W2)
// { // return true if _S1 precedes _S2 (order defined by control block)
// return (_W1.owner_before(_W2));
// }
template<class _Ty>
void swap(weak_ptr<_Ty>& _W1, weak_ptr<_Ty>& _W2)
{ // swap contents of _W1 and _W2
_W1.swap(_W2);
}
// TEMPLATE CLASS enable_shared_from_this
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:
enable_shared_from_this()
{ // construct (do nothing)
}
enable_shared_from_this(const enable_shared_from_this&)
{ // construct (do nothing)
}
enable_shared_from_this& operator=(const enable_shared_from_this&)
{ // assign (do nothing)
return (*this);
}
~enable_shared_from_this()
{ // destroy (do nothing)
}
private:
template<class _Ty1,
class _Ty2>
friend void _Do_enable(
_Ty1 *,
enable_shared_from_this<_Ty2>*,
_Ref_count_base *);
mutable weak_ptr<_Ty> _Wptr;
};
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);
}
} // namespace tr1
_STD_END
#endif /* _HAS_TR1 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -