📄 memory
字号:
#if _HAS_CPP0X
_STD_BEGIN
// TEMPLATE CLASS unique_pointer AND HELPERS
// TEMPLATE CLASS default_delete
template<class _Ty>
struct default_delete
{ // default deleter for unique_ptr
typedef default_delete<_Ty> _Myt;
default_delete()
{ // default construct
}
template<class _Ty2>
default_delete(const default_delete<_Ty2>&)
{ // construct from another default_delete
}
void operator()(_Ty *_Ptr) const
{ // delete a pointer
if (0 < sizeof (_Ty)) // won't compile for incomplete type
delete _Ptr;
}
};
template<class _Ty>
struct default_delete<_Ty[]>
{ // default deleter for unique_ptr to array of unknown size
typedef default_delete<_Ty> _Myt;
default_delete()
{ // default construct
}
void operator()(_Ty *_Ptr) const
{ // delete a pointer
if (0 < sizeof (_Ty)) // won't compile for incomplete type
delete[] _Ptr;
}
template<class _Other>
void operator()(_Other *) const; // not defined
};
// FUNCTION _Has_pointer_type
_STD tr1::_No _Has_pointer_type(...);
template<class _Ty>
_STD tr1::_Yes _Has_pointer_type(_Ty *,
typename _Ty::pointer * = 0);
template<class _Ty,
class _Dx,
bool>
struct _Hold_pointer_type
{ // wraps pointer type when _Dx::pointer absent
typedef _Ty *pointer;
};
template<class _Ty,
class _Dx>
struct _Hold_pointer_type<_Ty, _Dx, true>
{ // wraps pointer type when _Dx::pointer present
typedef typename _Dx::pointer pointer;
};
#define _DELETER_POINTER_TYPE(_Ty, _Dx) \
typename _Hold_pointer_type<_Ty, _Dx, \
_IS_YES(_Has_pointer_type((_Dx *)0))>::pointer
// TEMPLATE CLASS _Unique_ptr_base
template<class _Ty,
class _Dx,
bool _Empty_deleter>
class _Unique_ptr_base
{ // stores pointer and deleter
public:
typedef typename tr1::remove_reference<_Dx>::type _Dx_noref;
typedef _DELETER_POINTER_TYPE(_Ty, _Dx_noref) pointer;
_Unique_ptr_base(pointer _Ptr, _Dx _Dt)
: _Myptr(_Ptr), _Mydel(_Dt)
{ // construct with pointer and deleter
}
template<class _Ptr2,
class _Dx2>
_Unique_ptr_base(_Ptr2 _Ptr, _Dx2 _Dt)
: _Myptr(_Ptr), _Mydel(_Dt)
{ // construct with compatible pointer and deleter
}
_Dx_noref& get_deleter()
{ // return reference to deleter
return (_Mydel);
}
const _Dx_noref& get_deleter() const
{ // return const reference to deleter
return (_Mydel);
}
pointer _Myptr; // the managed pointer
_Dx _Mydel; // the deleter
};
template<class _Ty,
class _Dx>
class _Unique_ptr_base<_Ty, _Dx, true>
: public _Dx
{ // store pointer and empty deleter
public:
typedef _Dx _Mybase;
typedef typename tr1::remove_reference<_Dx>::type _Dx_noref;
typedef _DELETER_POINTER_TYPE(_Ty, _Dx_noref) pointer;
_Unique_ptr_base(pointer _Ptr, _Dx _Dt)
: _Myptr(_Ptr), _Mybase(_Dt)
{ // construct with pointer and deleter
}
template<class _Ptr2,
class _Dx2>
_Unique_ptr_base(_Ptr2 _Ptr, _Dx2 _Dt)
: _Myptr(_Ptr), _Mybase(_Dt)
{ // construct with compatible pointer and deleter
}
_Dx_noref& get_deleter()
{ // return reference to deleter
return (*this);
}
const _Dx_noref& get_deleter() const
{ // return const reference to deleter
return (*this);
}
pointer _Myptr; // the managed pointer
};
// TEMPLATE CLASS unique_ptr SCALAR
template<class _Ty,
class _Dx> // = default_delete<_Ty>
class unique_ptr
: public _Unique_ptr_base<_Ty, _Dx,
tr1::is_empty<_Dx>::value
|| tr1::is_same<default_delete<_Ty>, _Dx>::value>
{ // non-copyable pointer to an object
public:
typedef unique_ptr<_Ty, _Dx> _Myt;
typedef _Unique_ptr_base<_Ty, _Dx,
tr1::is_empty<_Dx>::value
|| tr1::is_same<default_delete<_Ty>, _Dx>::value> _Mybase;
typedef typename _Mybase::pointer pointer;
typedef _Ty element_type;
typedef _Dx deleter_type;
unique_ptr()
: _Mybase(pointer(), _Dx())
{ // default construct
static_assert(!is_pointer<_Dx>::value,
"unique_ptr constructed with null deleter pointer");
}
#if defined(_NATIVE_NULLPTR_SUPPORTED) \
&& !defined(_DO_NOT_USE_NULLPTR_IN_STL)
unique_ptr(_STD nullptr_t)
: _Mybase(pointer(), _Dx())
{ // null pointer construct
static_assert(!is_pointer<_Dx>::value,
"unique_ptr constructed with null deleter pointer");
}
_Myt& operator=(_STD nullptr_t)
{ // assign a null pointer
reset();
return (*this);
}
#endif /* defined(_NATIVE_NULLPTR_SUPPORTED) etc. */
explicit unique_ptr(pointer _Ptr)
: _Mybase(_Ptr, _Dx())
{ // construct with pointer
static_assert(!is_pointer<_Dx>::value,
"unique_ptr constructed with null deleter pointer");
}
unique_ptr(pointer _Ptr,
typename _If<tr1::is_reference<_Dx>::value, _Dx,
const typename tr1::remove_reference<_Dx>::type&>::_Type _Dt)
: _Mybase(_Ptr, _Dt)
{ // construct with pointer and (maybe const) deleter&
}
unique_ptr(pointer _Ptr, typename tr1::remove_reference<_Dx>::type&& _Dt)
: _Mybase(_Ptr, _STD move(_Dt))
{ // construct by moving deleter
// static_assert(!tr1::is_reference<_Dx>::value,
// "unique_ptr constructed with reference to rvalue deleter");
}
unique_ptr(unique_ptr&& _Right)
: _Mybase(_Right.release(),
_STD forward<_Dx>(_Right.get_deleter()))
{ // construct by moving _Right
}
template<class _Ty2,
class _Dx2>
unique_ptr(unique_ptr<_Ty2, _Dx2>&& _Right)
: _Mybase(_Right.release(),
_STD forward<_Dx2>(_Right.get_deleter()))
{ // construct by moving _Right
}
template<class _Ty2,
class _Dx2>
_Myt& operator=(unique_ptr<_Ty2, _Dx2>&& _Right)
{ // assign by moving _Right
reset(_Right.release());
this->get_deleter() = _STD move(_Right.get_deleter());
return (*this);
}
_Myt& operator=(_Myt&& _Right)
{ // assign by moving _Right
if (this != &_Right)
{ // different, do the move
reset(_Right.release());
this->get_deleter() = _STD move(_Right.get_deleter());
}
return (*this);
}
void swap(_Myt&& _Right)
{ // swap elements
if (this != &_Right)
{ // different, do the swap
_Swap_adl(this->_Myptr, _Right._Myptr);
_Swap_adl(this->get_deleter(),
_Right.get_deleter());
}
}
void swap(_Myt& _Right)
{ // swap elements
_Swap_adl(this->_Myptr, _Right._Myptr);
_Swap_adl(this->get_deleter(),
_Right.get_deleter());
}
~unique_ptr()
{ // destroy the object
_Delete();
}
typename tr1::add_reference<_Ty>::type operator*() const
{ // return reference to object
return (*this->_Myptr);
}
pointer operator->() const
{ // return pointer to class object
return (&**this);
}
pointer get() const
{ // return pointer to object
return (this->_Myptr);
}
_OPERATOR_BOOL() const
{ // test for non-null pointer
return (this->_Myptr != pointer() ? _CONVERTIBLE_TO_TRUE : 0);
}
pointer release()
{ // yield ownership of pointer
pointer _Ans = this->_Myptr;
this->_Myptr = pointer();
return (_Ans);
}
void reset(pointer _Ptr = pointer())
{ // establish new pointer
if (_Ptr != this->_Myptr)
{ // different pointer, delete old and reassign
_Delete();
this->_Myptr = _Ptr;
}
}
private:
void _Delete()
{ // delete the pointer
if (this->_Myptr != pointer())
this->get_deleter()(this->_Myptr);
}
unique_ptr(const _Myt&); // not defined
template<class _Ty2,
class _Dx2>
unique_ptr(const unique_ptr<_Ty2, _Dx2>&); // not defined
_Myt& operator=(const _Myt&); // not defined
template<class _Ty2,
class _Dx2>
_Myt& operator=(const unique_ptr<_Ty2, _Dx2>&); // not defined
};
// TEMPLATE CLASS unique_ptr ARRAY
template<class _Ty,
class _Dx>
class unique_ptr<_Ty[], _Dx>
: public _Unique_ptr_base<_Ty, _Dx,
tr1::is_empty<_Dx>::value
|| tr1::is_same<default_delete<_Ty[]>, _Dx>::value>
{ // non-copyable pointer to an array object
public:
typedef unique_ptr<_Ty[], _Dx> _Myt;
typedef _Unique_ptr_base<_Ty, _Dx,
tr1::is_empty<_Dx>::value
|| tr1::is_same<default_delete<_Ty[]>, _Dx>::value> _Mybase;
typedef typename _Mybase::pointer pointer;
typedef _Ty element_type;
typedef _Dx deleter_type;
unique_ptr()
: _Mybase(pointer(), _Dx())
{ // default construct
static_assert(!is_pointer<_Dx>::value,
"unique_ptr constructed with null deleter pointer");
}
explicit unique_ptr(pointer _Ptr)
: _Mybase(_Ptr, _Dx())
{ // construct with pointer
static_assert(!is_pointer<_Dx>::value,
"unique_ptr constructed with null deleter pointer");
}
unique_ptr(pointer _Ptr,
typename _If<tr1::is_reference<_Dx>::value, _Dx,
const typename tr1::remove_reference<_Dx>::type&>::_Type _Dt)
: _Mybase(_Ptr, _Dt)
{ // construct with pointer and (maybe const) deleter&
}
public:
unique_ptr(pointer _Ptr, typename tr1::remove_reference<_Dx>::type&& _Dt)
: _Mybase(_Ptr, _STD move(_Dt))
{ // construct by moving deleter
// static_assert(!tr1::is_reference<_Dx>::value,
// "unique_ptr constructed with reference to rvalue deleter");
}
unique_ptr(unique_ptr&& _Right)
: _Mybase(_Right.release(),
_STD forward<_Dx>(_Right.get_deleter()))
{ // construct by moving _Right
}
private:
template<class _Ty2,
class _Dx2>
unique_ptr(unique_ptr<_Ty2, _Dx2>&& _Right); // not defined
template<class _Ty2,
class _Dx2>
_Myt& operator=(unique_ptr<_Ty2, _Dx2>&& _Right); // not defined
public:
_Myt& operator=(_Myt&& _Right)
{ // assign by moving _Right
if (this != &_Right)
{ // different, do the swap
reset(_Right.release());
this->get_deleter() = _STD move(_Right.get_deleter());
}
return (*this);
}
void swap(_Myt&& _Right)
{ // swap elements
if (this != &_Right)
{ // different, do the move
_Swap_adl(this->_Myptr, _Right._Myptr);
_Swap_adl(this->get_deleter(),
_Right.get_deleter());
}
}
#if defined(_NATIVE_NULLPTR_SUPPORTED) \
&& !defined(_DO_NOT_USE_NULLPTR_IN_STL)
unique_ptr(_STD nullptr_t)
: _Mybase(pointer(), _Dx())
{ // null pointer construct
static_assert(!is_pointer<_Dx>::value,
"unique_ptr constructed with null deleter pointer");
}
_Myt& operator=(_STD nullptr_t)
{ // assign a null pointer
reset();
return (*this);
}
void reset(_STD nullptr_t)
{ // establish new null pointer
if (this->_Myptr != 0)
{ // different pointer, delete old and reassign
_Delete();
this->_Myptr = 0;
}
}
#endif /* defined(_NATIVE_NULLPTR_SUPPORTED) etc. */
void swap(_Myt& _Right)
{ // swap elements
_Swap_adl(this->_Myptr, _Right._Myptr);
_Swap_adl(this->get_deleter(), _Right.get_deleter());
}
~unique_ptr()
{ // destroy the object
_Delete();
}
typename tr1::add_reference<_Ty>::type operator[](size_t _Idx) const
{ // return reference to object
return (this->_Myptr[_Idx]);
}
pointer get() const
{ // return pointer to object
return (this->_Myptr);
}
_OPERATOR_BOOL() const
{ // test for non-null pointer
return (this->_Myptr != 0 ? _CONVERTIBLE_TO_TRUE : 0);
}
pointer release()
{ // yield ownership of pointer
pointer _Ans = this->_Myptr;
this->_Myptr = pointer();
return (_Ans);
}
void reset(pointer _Ptr = pointer())
{ // establish new pointer
if (_Ptr != this->_Myptr)
{ // different pointer, delete old and reassign
_Delete();
this->_Myptr = _Ptr;
}
}
private:
template<class _Ptr2>
explicit unique_ptr(_Ptr2); // not defined
template<class _Ptr2,
class _Dx2>
unique_ptr(_Ptr2, _Dx2); // not defined
unique_ptr(const _Myt&); // not defined
template<class _Ty2,
class _Dx2>
unique_ptr(const unique_ptr<_Ty2, _Dx2>&); // not defined
_Myt& operator=(const _Myt&); // not defined
template<class _Ty2,
class _Dx2>
_Myt& operator=(const unique_ptr<_Ty2, _Dx2>&); // not defined
template<class _Ptr2>
void reset(_Ptr2); // not defined
void _Delete()
{ // delete the pointer
this->get_deleter()(this->_Myptr);
}
};
template<class _Ty,
class _Dx>
void swap(unique_ptr<_Ty, _Dx>& _Left,
unique_ptr<_Ty, _Dx>& _Right)
{ // swap _Left with _Right
_Left.swap(_Right);
}
template<class _Ty,
class _Dx>
void swap(unique_ptr<_Ty, _Dx>& _Left,
unique_ptr<_Ty, _Dx>&& _Right)
{ // swap _Left with rvalue _Right
_Left.swap(_Right);
}
template<class _Ty,
class _Dx>
void swap(unique_ptr<_Ty, _Dx>&& _Left,
unique_ptr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -