1 #ifndef __SMARTPTR_HPP__ 2 #define __SMARTPTR_HPP__ 7 template <
typename _Ty >
10 static _Ty *
New() {
return new _Ty; }
11 static _Ty *
NewArray( std::size_t count ) {
return new _Ty[count]; }
15 template <
typename _Ty >
18 static void Delete( _Ty * p ) {
delete (_Ty *)p; }
26 virtual void _destroy() = 0;
27 virtual void _deleteThis() = 0;
44 template <
typename _HTy >
50 virtual void _destroy() {
delete _h; }
51 virtual void _deleteThis() {
delete this; }
57 template <
typename _HTy,
typename _Dt >
63 virtual void _destroy() { _dt(_h); }
64 virtual void _deleteThis() {
delete this; }
74 template <
typename _HTy >
92 template <
typename _Dt >
93 SimpleHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
95 template <
typename _HTy2 >
96 SimpleHandle( _HTy2 h, _HTy2 failVal ) { attachNew( h, failVal ); }
98 template <
typename _HTy2,
typename _Dt >
99 SimpleHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
117 template <
typename _HTy2 >
123 template <
typename _HTy2 >
135 template <
typename _Dt >
141 template <
typename _HTy2 >
146 template <
typename _HTy2,
typename _Dt >
152 void attach( SimpleHandleData
const & data )
154 _reset0( data.h, data.ctx );
157 template <
typename _HTy2 >
160 _reset0( data.
h, data.
ctx );
163 template <
typename _HTy2 >
171 SimpleHandleData r = _self;
172 _self.h =
static_cast<_HTy
>(0);
179 _reset0( static_cast<_HTy>(0), 0 );
182 _HTy
get()
const {
return _self.h; }
184 operator bool()
const {
return _self.ctx != 0; }
191 _HTy operator -> ()
const 198 template <
typename _HTy2 >
202 _self.ctx->release();
208 template <
typename _HTy2 >
211 _reset0( otherH, otherCtx );
212 otherH =
static_cast<_HTy2
>(0);
217 template <
typename _HTy2 >
226 template <
typename _HTy0 >
231 template <
typename _Ty >
242 template <
typename _Dt >
245 template <
typename _Ty2 >
248 template <
typename _Ty2,
typename _Dt >
262 template <
typename _Ty2 >
268 template <
typename _Ty2 >
277 MyBase::attachNew( p, (_Ty*)0 );
280 template <
typename _Dt >
283 MyBase::attachNew( p, (_Ty*)0, dt );
286 template <
typename _Ty2 >
289 MyBase::attachNew( p, (_Ty2*)0 );
292 template <
typename _Ty2,
typename _Dt >
295 MyBase::attachNew( p, (_Ty2*)0, dt );
302 template <
typename _Ty2 >
309 r.
_reset( p, this->_self.ctx );
320 template <
typename _Ty2 >
325 r.
_reset( p, this->_self.ctx );
331 template <
typename _Ty0 >
349 long volatile _weaks;
352 virtual void _destroy() = 0;
354 virtual void _deleteThis() = 0;
368 long count = (
long volatile &)_uses;
369 if ( count == 0 )
return false;
400 bool expired()
const {
return ( _uses == 0 ); }
409 template <
typename _HTy >
415 virtual void _destroy() {
delete _h; }
416 virtual void _deleteThis() {
delete this; }
422 template <
typename _HTy,
typename _Dt >
428 virtual void _destroy() { _dt(_h); }
429 virtual void _deleteThis() {
delete this; }
438 template <
typename _HTy >
457 template <
typename _Dt >
458 SharedHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
460 template <
typename _HTy2 >
463 template <
typename _HTy2,
typename _Dt >
464 SharedHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
482 template <
typename _HTy2 >
488 template <
typename _HTy2 >
500 template <
typename _Dt >
506 template <
typename _HTy2 >
512 template <
typename _HTy2,
typename _Dt >
523 void attach( SharedHandleData
const & data,
bool isIncRef )
527 _reset( data.h, data.ctx );
531 _reset0( data.h, data.ctx );
540 template <
typename _HTy2 >
545 _reset( data.
h, data.
ctx );
549 _reset0( data.
h, data.
ctx );
558 template <
typename _HTy2 >
573 SharedHandleData r = _self;
574 _self.h =
static_cast<_HTy
>(0);
586 _reset0( static_cast<_HTy>(0), 0 );
589 _HTy
get()
const {
return _self.h; }
591 operator bool()
const {
return _self.ctx != 0; }
598 _HTy operator -> ()
const 605 template <
typename _HTy2 >
615 template <
typename _HTy2 >
620 _reset0( otherH, otherCtx );
624 template <
typename _HTy2 >
632 template <
typename _HTy0 >
634 template <
typename _HTy0 >
638 template <
typename _Ty >
649 template <
typename _Dt >
652 template <
typename _Ty2 >
655 template <
typename _Ty2,
typename _Dt >
669 template <
typename _Ty2 >
675 template <
typename _Ty2 >
684 MyBase::attachNew( p, (_Ty*)0 );
687 template <
typename _Dt >
690 MyBase::attachNew( p, (_Ty*)0, dt );
693 template <
typename _Ty2 >
696 MyBase::attachNew( p, (_Ty2*)0 );
699 template <
typename _Ty2,
typename _Dt >
702 MyBase::attachNew( p, (_Ty2*)0, dt );
709 template <
typename _Ty2 >
715 r.
_reset( p, this->_self.ctx );
723 template <
typename _Ty2 >
731 template <
typename _Ty0 >
733 template <
typename _Ty0 >
738 template <
typename _HTy >
753 template <
typename _HTy2 >
759 template <
typename _HTy2 >
777 template <
typename _HTy2 >
783 template <
typename _HTy2 >
792 _reset( static_cast<_HTy>(0), 0 );
798 if ( !this->_sharedReset(&r) ) { }
805 bool expired()
const {
return ( !_self.ctx || _self.ctx->expired() ); }
807 operator bool() {
return _self.ctx != 0; }
808 operator bool()
const {
return _self.ctx != 0; }
812 template <
typename _HTy2 >
815 if ( _self.ctx && _self.ctx->_incRefNz() )
818 pSharedHandle->
_reset0( _self.h, _self.ctx );
825 template <
typename _HTy2 >
830 if ( _self.ctx != 0 )
831 _self.ctx->decWRef();
833 _self.ctx = otherCtx;
837 template <
typename _HTy2 >
845 template <
typename _HTy0 >
850 template <
typename _Ty >
859 template <
typename _Ty2 >
862 this->_reset( other.
_self.h, other.
_self.ctx );
865 template <
typename _Ty2 >
868 this->_reset( other.
_self.h, other.
_self.ctx );
883 template <
typename _Ty2 >
889 template <
typename _Ty2 >
899 if ( !this->_sharedReset(&r) ) { }
903 template <
typename _Ty0 >
907 template <
typename _Ty >
913 template <
typename _Ty,
typename _Dt >
919 template <
typename _Ty >
925 template <
typename _Ty,
typename _Dt >
934 #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)