1 #ifndef __SMARTPTR_HPP__ 2 #define __SMARTPTR_HPP__ 10 template <
typename _Ty >
13 static _Ty *
New() {
return new _Ty; }
14 static _Ty *
NewArray( std::size_t count ) {
return new _Ty[count]; }
18 template <
typename _Ty >
21 static void Delete( _Ty * p ) {
delete (_Ty *)p; }
29 virtual void _destroy() = 0;
30 virtual void _deleteThis() = 0;
47 template <
typename _HTy >
53 virtual void _destroy() {
delete _h; }
54 virtual void _deleteThis() {
delete this; }
60 template <
typename _HTy,
typename _Dt >
66 virtual void _destroy() { _dt(_h); }
67 virtual void _deleteThis() {
delete this; }
77 template <
typename _HTy >
95 template <
typename _Dt >
96 SimpleHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
98 template <
typename _HTy2 >
99 SimpleHandle( _HTy2 h, _HTy2 failVal ) { attachNew( h, failVal ); }
101 template <
typename _HTy2,
typename _Dt >
102 SimpleHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
120 template <
typename _HTy2 >
126 template <
typename _HTy2 >
138 template <
typename _Dt >
144 template <
typename _HTy2 >
149 template <
typename _HTy2,
typename _Dt >
155 void attach( SimpleHandleData
const & data )
157 _reset0( data.h, data.ctx );
160 template <
typename _HTy2 >
163 _reset0( data.
h, data.
ctx );
166 template <
typename _HTy2 >
174 SimpleHandleData r = _self;
175 _self.h =
static_cast<_HTy
>(0);
182 _reset0( static_cast<_HTy>(0), 0 );
185 _HTy
get()
const {
return _self.h; }
187 operator bool()
const {
return _self.ctx != 0; }
194 _HTy operator -> ()
const 201 template <
typename _HTy2 >
205 _self.ctx->release();
211 template <
typename _HTy2 >
214 _reset0( otherH, otherCtx );
215 otherH =
static_cast<_HTy2
>(0);
220 template <
typename _HTy2 >
229 template <
typename _HTy0 >
234 template <
typename _Ty >
245 template <
typename _Dt >
248 template <
typename _Ty2 >
251 template <
typename _Ty2,
typename _Dt >
265 template <
typename _Ty2 >
271 template <
typename _Ty2 >
280 MyBase::attachNew( p, (_Ty*)0 );
283 template <
typename _Dt >
286 MyBase::attachNew( p, (_Ty*)0, dt );
289 template <
typename _Ty2 >
292 MyBase::attachNew( p, (_Ty2*)0 );
295 template <
typename _Ty2,
typename _Dt >
298 MyBase::attachNew( p, (_Ty2*)0, dt );
305 template <
typename _Ty2 >
312 r.
_reset( p, this->_self.ctx );
323 template <
typename _Ty2 >
328 r.
_reset( p, this->_self.ctx );
334 template <
typename _Ty0 >
352 long volatile _weaks;
355 virtual void _destroy() = 0;
357 virtual void _deleteThis() = 0;
371 long count = (
long volatile &)_uses;
372 if ( count == 0 )
return false;
403 bool expired()
const {
return ( _uses == 0 ); }
412 template <
typename _HTy >
418 virtual void _destroy() {
delete _h; }
419 virtual void _deleteThis() {
delete this; }
425 template <
typename _HTy,
typename _Dt >
431 virtual void _destroy() { _dt(_h); }
432 virtual void _deleteThis() {
delete this; }
441 template <
typename _HTy >
460 template <
typename _Dt >
461 SharedHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
463 template <
typename _HTy2 >
466 template <
typename _HTy2,
typename _Dt >
467 SharedHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
485 template <
typename _HTy2 >
491 template <
typename _HTy2 >
503 template <
typename _Dt >
509 template <
typename _HTy2 >
515 template <
typename _HTy2,
typename _Dt >
526 void attach( SharedHandleData
const & data,
bool isIncRef )
530 _reset( data.h, data.ctx );
534 _reset0( data.h, data.ctx );
543 template <
typename _HTy2 >
548 _reset( data.
h, data.
ctx );
552 _reset0( data.
h, data.
ctx );
561 template <
typename _HTy2 >
576 SharedHandleData r = _self;
577 _self.h =
static_cast<_HTy
>(0);
589 _reset0( static_cast<_HTy>(0), 0 );
592 _HTy
get()
const {
return _self.h; }
594 operator bool()
const {
return _self.ctx != 0; }
601 _HTy operator -> ()
const 608 template <
typename _HTy2 >
618 template <
typename _HTy2 >
623 _reset0( otherH, otherCtx );
627 template <
typename _HTy2 >
635 template <
typename _HTy0 >
637 template <
typename _HTy0 >
641 template <
typename _Ty >
652 template <
typename _Dt >
655 template <
typename _Ty2 >
658 template <
typename _Ty2,
typename _Dt >
672 template <
typename _Ty2 >
678 template <
typename _Ty2 >
687 MyBase::attachNew( p, (_Ty*)0 );
690 template <
typename _Dt >
693 MyBase::attachNew( p, (_Ty*)0, dt );
696 template <
typename _Ty2 >
699 MyBase::attachNew( p, (_Ty2*)0 );
702 template <
typename _Ty2,
typename _Dt >
705 MyBase::attachNew( p, (_Ty2*)0, dt );
712 template <
typename _Ty2 >
718 r.
_reset( p, this->_self.ctx );
726 template <
typename _Ty2 >
734 template <
typename _Ty0 >
736 template <
typename _Ty0 >
741 template <
typename _HTy >
756 template <
typename _HTy2 >
762 template <
typename _HTy2 >
780 template <
typename _HTy2 >
786 template <
typename _HTy2 >
795 _reset( static_cast<_HTy>(0), 0 );
801 if ( !this->_sharedReset(&r) ) { }
808 bool expired()
const {
return ( !_self.ctx || _self.ctx->expired() ); }
810 operator bool() {
return _self.ctx != 0; }
811 operator bool()
const {
return _self.ctx != 0; }
815 template <
typename _HTy2 >
818 if ( _self.ctx && _self.ctx->_incRefNz() )
821 pSharedHandle->
_reset0( _self.h, _self.ctx );
828 template <
typename _HTy2 >
833 if ( _self.ctx != 0 )
834 _self.ctx->decWRef();
836 _self.ctx = otherCtx;
840 template <
typename _HTy2 >
848 template <
typename _HTy0 >
853 template <
typename _Ty >
862 template <
typename _Ty2 >
865 this->_reset( other.
_self.h, other.
_self.ctx );
868 template <
typename _Ty2 >
871 this->_reset( other.
_self.h, other.
_self.ctx );
886 template <
typename _Ty2 >
892 template <
typename _Ty2 >
902 if ( !this->_sharedReset(&r) ) { }
906 template <
typename _Ty0 >
910 template <
typename _Ty >
916 template <
typename _Ty,
typename _Dt >
922 template <
typename _Ty >
928 template <
typename _Ty,
typename _Dt >
937 #endif // __SMARTPTR_HPP__ SimpleHandle< _Ty * > MyBase
bool _sharedReset(SharedHandle< _HTy2 > *pSharedHandle) const
Shared*PTR* call reset。用于从Weak*PTR*创建Shared*PTR*.
WeakPointer(WeakPointer const &other)
SharedHandle(_HTy2 h, _HTy2 failVal)
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个shared的资源引用计数,减少自身计数。管理另一个shared的资源
void _reset0(_HTy2 newH, SimpleDeleterContext *newCtx)
释放自身资源,管理新资源
void attach(_HTy2 h, SharedDeleterContext *ctx, bool isIncRef)
管理一个资源
static void Delete(_Ty *p)
void release()
销毁资源和SimpleDeleterContext自身
SimpleCustomDeleterContext(_HTy h, _Dt dt)
SimpleHandle(SimpleHandle< _HTy2 > const &other)
SimpleDefaultDeleterContext(_HTy h)
SharedPointer(_Ty2 *p, _Dt dt)
SharedPointer(_Ty *p, _Dt dt)
bool expired() const
资源是否已过期
void _reset0(_HTy2 newH, SharedDeleterContext *newCtx)
减少自身引用计数,管理新资源
long useCount() const
资源引用计数
SharedPointer< _Ty > lock() const
void attach(typename SimpleHandle< _HTy2 >::SimpleHandleData const &data)
virtual ~SharedDeleterContext()
void _reset(SharedHandle< _HTy2 > const &other)
增加另一个shared的资源引用计数,减少自身计数。管理另一个shared的资源
long LongAtomicCompareExchange(long volatile *p, long exchange, long comparand)
原子化操作,*p若和comparand相等,就把*p赋成exchange,返回值是初始的*p值
SharedHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
简单句柄类,管理各种资源的自动释放,赋值相当于传递管理权。
void _reset(WeakHandle< _HTy2 > const &other)
增加另一个弱引用计数,减少自身弱计数。管理另一个Weak*PTR*
SharedCustomDeleterContext(_HTy h, _Dt dt)
SharedDeleterContext * ctx
void attach(SimpleHandleData const &data)
void attachNew(_Ty2 *p, _Dt dt)
long LongAtomicDecrement(long volatile *p)
原子化使一个Long型变量-1,返回值是-1后的*p值
static void DeleteArray(_Ty *p)
WeakHandle< _Ty * > MyBase
void attachNew(_HTy h, _HTy failVal)
void delThis()
显式删除detach()出来的SimpleDeleterContext对象
SimpleHandle(_HTy2 h, _HTy2 failVal)
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个弱引用计数,减少自身弱计数。管理另一个Weak*PTR*
static _Ty * NewArray(std::size_t count)
WeakPointer(WeakPointer< _Ty2 > const &other)
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
#define DISABLE_OBJECT_COPY(clsname)
void decRef()
减少引用计数.当引用计数为0时销毁资源,并且销毁资源时减少弱引用计数.
SharedDefaultDeleterContext(_HTy h)
void attach(typename SharedHandle< _HTy2 >::SharedHandleData const &data, bool isIncRef)
管理一个资源
SharedHandle(_HTy h, _HTy failVal)
SharedHandle< _Ty * > MyBase
bool _incRefNz()
如果引用计数不是0,则增加引用计数。成功则返回true。
void attachNew(_HTy h, _HTy failVal, _Dt dt)
SimpleHandle(_HTy h, _HTy failVal)
WeakHandle(WeakHandle< _HTy2 > const &other)
SimplePointer(SimplePointer< _Ty2 > const &other)
SimpleHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
SharedHandleData peek() const
void attachNew(_Ty *p, _Dt dt)
SimpleDeleterContext * ctx
SharedPointer(SharedPointer const &other)
SharedPointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
WeakHandle(WeakHandle const &other)
virtual ~SimpleDeleterContext()
void _reset(SimpleHandle< _HTy2 > const &other)
释放自身资源,接管另一个simple的资源,另一个simple置零
long LongAtomicIncrement(long volatile *p)
原子化使一个Long型变量+1,返回值是+1后的*p值
void attach(SharedHandleData const &data, bool isIncRef)
管理一个资源
SharedHandle< _HTy >::SharedHandleData WeakHandleData
SimplePointer(_Ty2 *p, _Dt dt)
SimplePointer(_Ty *p, _Dt dt)
SharedHandle(SharedHandle< _HTy2 > const &other)
SimpleHandle(_HTy h, _HTy failVal, _Dt dt)
SimpleHandle(SimpleHandle const &other)
SharedHandleData detach()
void decWRef()
减少弱引用计数,当弱引用计数为0时销毁删除器场景对象
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
#define WINUX_FUNC_DECL(ret)
WeakPointer(SharedPointer< _Ty2 > const &other)
SimplePointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
bool expired() const
是否过期
SimplePointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
SharedPointer(SharedPointer< _Ty2 > const &other)
SharedHandle(_HTy h, _HTy failVal, _Dt dt)
SharedHandleData(_HTy h, SharedDeleterContext *ctx)
void _reset(_HTy2 &otherH, SimpleDeleterContext *&otherCtx)
释放自身资源,接管另一个simple的资源,另一个simple置零
long weakCount() const
弱引用计数
SharedHandle(SharedHandle const &other)
WeakHandle(SharedHandle< _HTy2 > const &other)
SimpleHandleData(_HTy h, SimpleDeleterContext *ctx)
void attachNew(_HTy h, _HTy failVal)
SimplePointer< _Ty > MakeSimple(_Ty *newObj)
void attach(_HTy2 h, SimpleDeleterContext *ctx)
void attachNew(_Ty *p, _Dt dt)
void attachNew(_HTy h, _HTy failVal, _Dt dt)
SharedPointer< _Ty > MakeShared(_Ty *newObj)
SimplePointer(SimplePointer const &other)
SharedHandle< _HTy > lock() const
void attachNew(_HTy2 h, _HTy2 failVal)
SimpleHandleData detach()
SharedPointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
void attachNew(_Ty2 *p, _Dt dt)
void attachNew(_HTy2 h, _HTy2 failVal)