1 #ifndef __SMARTPTR_HPP__ 2 #define __SMARTPTR_HPP__ 47 virtual void _destroy() = 0;
49 virtual void _deleteThis() = 0;
53 template <
typename _HTy >
60 virtual void _destroy()
override {
delete _h; }
68 template <
typename _HTy,
typename _Dt >
87 template <
typename _HTy >
117 template <
typename _Dt >
118 SimpleHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
124 template <
typename _HTy2 >
132 template <
typename _HTy2,
typename _Dt >
133 SimpleHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
155 template <
typename _HTy2 >
162 template <
typename _HTy2 >
183 template <
typename _Dt >
193 template <
typename _HTy2 >
204 template <
typename _HTy2,
typename _Dt >
211 void attach( SimpleHandleData
const & data )
213 _reset0( data.h, data.ctx );
217 template <
typename _HTy2 >
220 _reset0( data.
h, data.
ctx );
224 template <
typename _HTy2 >
233 SimpleHandleData r = _self;
234 _self.h =
static_cast<_HTy
>(0);
242 _reset0( static_cast<_HTy>(0), 0 );
246 _HTy
get()
const {
return _self.h; }
250 operator bool()
const {
return _self.ctx != 0; }
260 _HTy operator -> ()
const 267 template <
typename _HTy2 >
271 _self.ctx->release();
277 template <
typename _HTy2 >
280 _reset0( otherH, otherCtx );
281 otherH =
static_cast<_HTy2
>(0);
286 template <
typename _HTy2 >
296 template <
typename _HTy0 >
301 template <
typename _Ty >
322 template <
typename _Dt >
328 template <
typename _Ty2 >
335 template <
typename _Ty2,
typename _Dt >
352 template <
typename _Ty2 >
359 template <
typename _Ty2 >
371 MyBase::attachNew( p, (_Ty*)0 );
378 template <
typename _Dt >
381 MyBase::attachNew( p, (_Ty*)0, dt );
387 template <
typename _Ty2 >
390 MyBase::attachNew( p, (_Ty2*)0 );
397 template <
typename _Ty2,
typename _Dt >
400 MyBase::attachNew( p, (_Ty2*)0, dt );
407 template <
typename _Ty2 >
414 r.
_reset( p, this->_self.ctx );
426 template <
typename _Ty2 >
431 r.
_reset( p, this->_self.ctx );
437 template <
typename _Ty0 >
451 template < typename _HTy >
453 template < typename _Ty >
455 template < typename _Ty >
459 template < typename... _Ty >
461 template <
typename... _Ty >
465 template <
typename _Yty,
typename = Vo
idT<> >
471 template <
typename _Yty >
472 struct _CanEnableShared<_Yty,
VoidT<typename _Yty::_EsftType> > : std::is_convertible<typename std::remove_cv<_Yty>::type *, typename _Yty::_EsftType *>::type
477 template <
typename _Other,
typename _Yty >
480 if ( ptr && ptr->_weakPtr.expired() )
487 template <
typename _Other,
typename _Yty >
493 template <
typename _Other,
typename _Yty >
507 virtual void _destroy() = 0;
509 virtual void _deleteThis() = 0;
520 long count = (
long volatile &)_uses;
521 if ( count == 0 )
return false;
552 bool expired()
const {
return ( _uses == 0 ); }
559 long volatile _weaks;
565 template <
typename _HTy >
580 template <
typename _HTy,
typename _Dt >
598 template <
typename _HTy >
628 template <
typename _Dt >
629 SharedHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
635 template <
typename _HTy2 >
643 template <
typename _HTy2,
typename _Dt >
644 SharedHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
647 template <
typename _HTy2 >
676 template <
typename _HTy2 >
683 template <
typename _HTy2 >
704 template <
typename _Dt >
714 template <
typename _HTy2 >
725 template <
typename _HTy2,
typename _Dt >
736 void attach( SharedHandleData
const & data,
bool isIncRef )
740 _reset( data.h, data.ctx );
744 _reset0( data.h, data.ctx );
753 template <
typename _HTy2 >
758 _reset( data.
h, data.
ctx );
762 _reset0( data.
h, data.
ctx );
771 template <
typename _HTy2 >
787 SharedHandleData r = _self;
788 _self.h =
static_cast<_HTy
>(0);
802 _reset0( static_cast<_HTy>(0), 0 );
806 _HTy
get()
const {
return _self.h; }
810 operator bool()
const {
return _self.ctx != 0; }
820 _HTy operator -> ()
const 827 template <
typename _HTy2 >
837 template <
typename _HTy2 >
842 _reset0( otherH, otherCtx );
846 template <
typename _HTy2 >
855 template <
typename _HTy0 >
857 template <
typename _HTy0 >
862 template <
typename _Ty >
883 template <
typename _Dt >
889 template <
typename _Ty2 >
896 template <
typename _Ty2,
typename _Dt >
900 template <
typename _Ty2 >
910 template <
typename _Ty2 >
913 if ( other._self.ctx )
914 other._self.ctx->incRef();
917 this->_self.ctx = other._self.ctx;
934 template <
typename _Ty2 >
941 template <
typename _Ty2 >
953 MyBase::attachNew( p, (_Ty*)0 );
961 template <
typename _Dt >
964 MyBase::attachNew( p, (_Ty*)0, dt );
971 template <
typename _Ty2 >
974 MyBase::attachNew( p, (_Ty2*)0 );
982 template <
typename _Ty2,
typename _Dt >
985 MyBase::attachNew( p, (_Ty2*)0, dt );
993 template <
typename _Ty2 >
999 r.
_reset( p, this->_self.ctx );
1007 template <
typename _Ty2 >
1016 template <
typename _Ty0 >
1018 template <
typename _Ty0 >
1023 template <
typename _HTy >
1037 template <
typename _HTy2 >
1044 template <
typename _HTy2 >
1071 template <
typename _HTy2 >
1078 template <
typename _HTy2 >
1088 _reset( static_cast<_HTy>(0), 0 );
1095 (void)this->_sharedReset(&r);
1102 bool expired()
const {
return ( !_self.ctx || _self.ctx->expired() ); }
1105 operator bool()
const {
return _self.ctx != 0; }
1109 template <
typename _HTy2 >
1112 if ( _self.ctx && _self.ctx->_incRefNz() )
1115 shared->
_reset0( _self.h, _self.ctx );
1122 template <
typename _HTy2 >
1125 if ( otherCtx != 0 )
1127 if ( _self.ctx != 0 )
1128 _self.ctx->decWRef();
1130 _self.ctx = otherCtx;
1134 template <
typename _HTy2 >
1143 template <
typename _HTy0 >
1145 template <
typename _HTy0 >
1150 template <
typename _Ty >
1163 template <
typename _Ty2 >
1166 this->_reset( other.
_self.h, other.
_self.ctx );
1170 template <
typename _Ty2 >
1173 this->_reset( other.
_self.h, other.
_self.ctx );
1180 this->_reset(other);
1186 this->_reset(other);
1191 template <
typename _Ty2 >
1194 this->_reset(other);
1198 template <
typename _Ty2 >
1201 this->_reset(other);
1209 (void)this->_sharedReset(&r);
1214 template <
typename _Ty0 >
1216 template <
typename _Ty0 >
1221 template <
typename _Ty >
1262 template <
typename _Other,
typename _Yty >
1269 template <
typename _Ty >
1276 template <
typename _Ty,
typename _Dt >
1283 template <
typename _Ty >
1290 template <
typename _Ty,
typename _Dt >
1299 #endif // __SMARTPTR_HPP__ XString< char > AnsiString
SimpleHandle< _Ty * > MyBase
基类SimpleHandle<_Ty*>
void attachNew(_Ty *p)
附加新指针,管理新对象
WeakPointer(WeakPointer const &other)
拷贝构造函数1
void _EnableSharedFromThis(SharedPointer< _Other > const &sharedThis, _Yty *ptr)
SharedHandle(_HTy2 h, _HTy2 failVal)
构造函数2_1。_HTy2为可直接转为_HTy的类型
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个SharedHandle的资源引用计数,减少自身计数。管理另一个SharedHandle的资源
void _reset0(_HTy2 newH, SimpleDeleterContext *newCtx)
释放自身资源,管理新资源
void attach(_HTy2 h, SharedDeleterContext *ctx, bool isIncRef)
附加其他SharedHandle对象detach()出的句柄和删除器场景。_HTy2为可直接转为_HTy的类型 ...
void release()
销毁资源和SimpleDeleterContext自身
SimpleCustomDeleterContext(_HTy h, _Dt dt)
SimpleHandle(SimpleHandle< _HTy2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
SimpleDefaultDeleterContext(_HTy h)
SharedPointer(_Ty2 *p, _Dt dt)
构造函数2_2
SimplePointer(_Ty2 *p)
构造函数2_1
SharedPointer(_Ty *p, _Dt dt)
构造函数1_2
Shared删除器场景基类/Shared引用计数场景
SharedPointer(_Ty *p)
构造函数1_1
bool expired() const
资源是否已过期
typename MakeVoid< _Ty... >::type VoidT
void _reset0(_HTy2 newH, SharedDeleterContext *newCtx)
减少自身引用计数,管理新资源
long useCount() const
资源引用计数
SharedPointer< _Ty > lock() const
锁定共享引用计数场景,创建一个SharedPointer持住资源
bool _sharedReset(SharedHandle< _HTy2 > *shared) const
调用SharedHandle::_reset0()。用于从WeakHandle创建SharedHandle
void attach(typename SimpleHandle< _HTy2 >::SimpleHandleData const &data)
附加其他SimpleHandle对象detach()出的句柄数据。_HTy2为可直接转为_HTy的类型
virtual ~SharedDeleterContext()
virtual void _destroy() override
销毁资源
virtual void _destroy() override
销毁资源
void _reset(SharedHandle< _HTy2 > const &other)
增加另一个SharedHandle的资源引用计数,减少自身计数。管理另一个SharedHandle的资源
virtual void _deleteThis() override
删除引用计数数据场景
long LongAtomicCompareExchange(long volatile *p, long exchange, long comparand)
原子化操作,*p若和comparand相等,就把*p赋成exchange,返回值是初始的*p值
SharedHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
构造函数2_2。_HTy2为可直接转为_HTy的类型
简单句柄类,管理各种资源的自动释放,赋值相当于传递管理权。
SharedPointer< const _Ty > sharedFromThis() const
void _reset(WeakHandle< _HTy2 > const &other)
增加另一个弱引用计数,减少自身弱计数。管理另一个WeakHandle
SharedCustomDeleterContext(_HTy h, _Dt dt)
SharedDeleterContext * ctx
共享删除器场景对象
void attach(SimpleHandleData const &data)
附加其他SimpleHandle对象detach()出的句柄数据
void attachNew(_Ty2 *p, _Dt dt)
附加新指针,管理新对象
long LongAtomicDecrement(long volatile *p)
原子化使一个Long型变量-1,返回值是-1后的*p值
WeakHandle< _Ty * > MyBase
基类WeakHandle<_Ty*>
virtual void _deleteThis() override
销毁SimpleDeleterContext自身
void attachNew(_HTy h, _HTy failVal)
附加新句柄,管理新资源
void delThis()
显式删除SimpleHandle::detach()出来的SimpleDeleterContext对象
virtual void _deleteThis() override
删除引用计数数据场景
SimpleHandle(_HTy2 h, _HTy2 failVal)
构造函数2_1。_HTy2为可直接转为_HTy的类型
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个弱引用计数,减少自身弱计数。管理另一个WeakHandle
void attachNew(_Ty2 *p)
附加新指针,管理新对象
WeakPointer(WeakPointer< _Ty2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
#define DISABLE_OBJECT_COPY(clsname)
void decRef()
减少引用计数。当引用计数为0时销毁资源,并且销毁资源时减少弱引用计数。
SharedDefaultDeleterContext(_HTy h)
WeakPointer< const _Ty > weakFromThis() const noexcept
SharedDeleterContext * getContext() const
void attach(typename SharedHandle< _HTy2 >::SharedHandleData const &data, bool isIncRef)
附加其他SharedHandle对象detach()出的句柄数据。_HTy2为可直接转为_HTy的类型
WeakPointer< _Ty > weakFromThis() noexcept
SimplePointer(_Ty *p)
构造函数1_1
SharedHandle(_HTy h, _HTy failVal)
构造函数1_1
SharedHandle< _Ty * > MyBase
基类SharedHandle<_Ty*>
virtual void _deleteThis() override
销毁SimpleDeleterContext自身
bool _incRefNz()
如果引用计数不是0,则增加引用计数。成功则返回true。
void attachNew(_Ty *p)
附加新指针,管理新对象
void attachNew(_HTy h, _HTy failVal, _Dt dt)
附加新句柄,管理新资源
SimpleHandle(_HTy h, _HTy failVal)
构造函数1_1
SharedHandle(WeakHandle< _HTy2 > const &other)
构造函数3。从弱句柄构建共享句柄
WeakHandle(WeakHandle< _HTy2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
SimplePointer(SimplePointer< _Ty2 > const &other)
拷贝构造函数2
SimpleHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
构造函数2_2。_HTy2为可直接转为_HTy的类型
virtual void _destroy() override
销毁资源
SharedHandleData peek() const
窥视句柄数据
void attachNew(_Ty *p, _Dt dt)
附加新指针,管理新对象
SimpleDeleterContext * ctx
简单删除器场景对象
SharedPointer(SharedPointer const &other)
拷贝构造函数1
virtual void _destroy() override
销毁资源
SharedPointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
WeakHandle(WeakHandle const &other)
拷贝构造函数1
virtual ~SimpleDeleterContext()
void _reset(SimpleHandle< _HTy2 > const &other)
释放自身资源,接管另一个SimpleHandle的资源,另一个SimpleHandle置零
long LongAtomicIncrement(long volatile *p)
原子化使一个Long型变量+1,返回值是+1后的*p值
void attach(SharedHandleData const &data, bool isIncRef)
附加其他SharedHandle对象detach()出的句柄数据
SharedHandle< _HTy >::SharedHandleData WeakHandleData
句柄数据结构体
SimplePointer(_Ty2 *p, _Dt dt)
构造函数2_2
SimpleDeleterContext * getContext() const
SimplePointer(_Ty *p, _Dt dt)
构造函数1_2
SharedHandle(SharedHandle< _HTy2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
SimpleHandle(_HTy h, _HTy failVal, _Dt dt)
构造函数1_2
SimpleHandle(SimpleHandle const &other)
拷贝构造函数1
SharedHandleData detach()
资源脱离管理,返回句柄数据
void decWRef()
减少弱引用计数,当弱引用计数为0时销毁删除器场景对象
SharedPointer(_Ty2 *p)
构造函数2_1
void _EnableSharedFromThis1(SharedPointer< _Other > const &sharedThis, _Yty *ptr, std::true_type)
SmartPtrError(SmartPtrErrorType err, AnsiString const &errStr)
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
#define WINUX_FUNC_DECL(ret)
WeakPointer(SharedPointer< _Ty2 > const &other)
构造函数1,从一个SharedPointer构建一个WeakPointer
SimplePointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
bool expired() const
是否过期
SimplePointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
SharedPointer(SharedPointer< _Ty2 > const &other)
拷贝构造函数2
SharedHandle(_HTy h, _HTy failVal, _Dt dt)
构造函数1_2
SharedHandleData(_HTy h, SharedDeleterContext *ctx)
void _reset(_HTy2 &otherH, SimpleDeleterContext *&otherCtx)
释放自身资源,接管另一个SimpleHandle的资源,另一个SimpleHandle置零
SharedPointer(SharedPointer< _Ty2 > const &other, _Ty2 *ptr) noexcept
构造函数4。别名构造
long weakCount() const
弱引用计数
SharedHandle(SharedHandle const &other)
拷贝构造函数1
WeakHandle(SharedHandle< _HTy2 > const &other)
构造函数1,从一个SharedHandle构建一个WeakHandle
SimpleHandleData(_HTy h, SimpleDeleterContext *ctx)
void attachNew(_HTy h, _HTy failVal)
附加新句柄,管理新资源
SimplePointer< _Ty > MakeSimple(_Ty *newObj)
创建一个SimplePointer来管理新对象资源
void attach(_HTy2 h, SimpleDeleterContext *ctx)
附加其他SimpleHandle对象detach()出的句柄和删除器场景。_HTy2为可直接转为_HTy的类型 ...
void attachNew(_Ty *p, _Dt dt)
附加新指针,管理新对象
void attachNew(_Ty2 *p)
附加新指针,管理新对象
void attachNew(_HTy h, _HTy failVal, _Dt dt)
附加新句柄,管理新资源
SharedPointer< _Ty > MakeShared(_Ty *newObj)
创建一个SharedPointer来管理新对象资源
SimplePointer(SimplePointer const &other)
拷贝构造函数1
SharedHandle< _HTy > lock() const
锁定共享引用计数场景,创建一个SharedHandle持住资源
EnableSharedFromThis(EnableSharedFromThis const &) noexcept
SharedPointer< _Ty > sharedFromThis()
SharedPointer(WeakPointer< _Ty2 > const &other)
构造函数3。从弱指针构建共享指针
void attachNew(_HTy2 h, _HTy2 failVal)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
SimpleHandleData detach()
资源脱离管理,返回句柄数据
SharedPointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
constexpr EnableSharedFromThis() noexcept
void attachNew(_Ty2 *p, _Dt dt)
附加新指针,管理新对象
void attachNew(_HTy2 h, _HTy2 failVal)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
引用计数共享句柄,管理各种资源的自动释放,赋值则引用计数加一。