fastdo  0.6.8
smartptr.hpp
浏览该文件的文档.
1 #ifndef __SMARTPTR_HPP__
2 #define __SMARTPTR_HPP__
3 //
4 // smartptr 提供智能指针相关的功能
5 //
6 
7 namespace winux
8 {
10 template < typename _Ty >
11 struct Allocator
12 {
13  static _Ty * New() { return new _Ty; }
14  static _Ty * NewArray( std::size_t count ) { return new _Ty[count]; }
15 };
16 
18 template < typename _Ty >
20 {
21  static void Delete( _Ty * p ) { delete (_Ty *)p; }
22  static void DeleteArray( _Ty * p ) { delete [] (_Ty *)p; }
23 };
24 
27 {
28 private:
29  virtual void _destroy() = 0;
30  virtual void _deleteThis() = 0;
31 protected:
33  virtual ~SimpleDeleterContext() { }
34 public:
36  void release()
37  {
38  this->_destroy();
39  this->_deleteThis();
40  }
41 
43  void delThis() { this->_deleteThis(); }
44 };
45 
47 template < typename _HTy >
49 {
50 public:
51  SimpleDefaultDeleterContext( _HTy h ) : _h(h) { }
52 private:
53  virtual void _destroy() { delete _h; }
54  virtual void _deleteThis() { delete this; }
55 
56  _HTy _h;
57 };
58 
60 template < typename _HTy, typename _Dt >
62 {
63 public:
64  SimpleCustomDeleterContext( _HTy h, _Dt dt ) : _h(h), _dt(dt) { }
65 private:
66  virtual void _destroy() { _dt(_h); }
67  virtual void _deleteThis() { delete this; }
68 
69  _HTy _h;
70  _Dt _dt;
71 };
72 
77 template < typename _HTy >
79 {
80 public:
81  typedef _HTy HType;
82 
84  {
85  _HTy h;
87  SimpleHandleData() : h(0), ctx(0) { }
88  SimpleHandleData( _HTy h, SimpleDeleterContext * ctx ) : h(h), ctx(ctx) { }
89  };
90 
92 
93  SimpleHandle( _HTy h, _HTy failVal ) { attachNew( h, failVal ); }
94 
95  template < typename _Dt >
96  SimpleHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
97 
98  template < typename _HTy2 >
99  SimpleHandle( _HTy2 h, _HTy2 failVal ) { attachNew( h, failVal ); }
100 
101  template < typename _HTy2, typename _Dt >
102  SimpleHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
103 
104  virtual ~SimpleHandle()
105  {
106  reset();
107  }
108 
109  SimpleHandle( SimpleHandle const & other )
110  {
111  _reset(other);
112  }
113 
114  SimpleHandle & operator = ( SimpleHandle const & other )
115  {
116  _reset(other);
117  return *this;
118  }
119 
120  template < typename _HTy2 >
122  {
123  _reset(other);
124  }
125 
126  template < typename _HTy2 >
127  SimpleHandle & operator = ( SimpleHandle<_HTy2> const & other )
128  {
129  _reset(other);
130  return *this;
131  }
132 
133  void attachNew( _HTy h, _HTy failVal )
134  {
135  this->_reset0( h, ( h != failVal ? new SimpleDefaultDeleterContext<_HTy>(h) : 0 ) );
136  }
137 
138  template < typename _Dt >
139  void attachNew( _HTy h, _HTy failVal, _Dt dt )
140  {
141  this->_reset0( h, ( h != failVal ? new SimpleCustomDeleterContext< _HTy, _Dt >( h, dt ) : 0 ) );
142  }
143 
144  template < typename _HTy2 >
145  void attachNew( _HTy2 h, _HTy2 failVal )
146  {
147  this->_reset0( h, ( h != failVal ? new SimpleDefaultDeleterContext<_HTy2>(h) : 0 ) );
148  }
149  template < typename _HTy2, typename _Dt >
150  void attachNew( _HTy2 h, _HTy2 failVal, _Dt dt )
151  {
152  this->_reset0( h, ( h != failVal ? new SimpleCustomDeleterContext< _HTy2, _Dt >( h, dt ) : 0 ) );
153  }
154 
155  void attach( SimpleHandleData const & data )
156  {
157  _reset0( data.h, data.ctx );
158  }
159 
160  template < typename _HTy2 >
161  void attach( typename SimpleHandle<_HTy2>::SimpleHandleData const & data )
162  {
163  _reset0( data.h, data.ctx );
164  }
165 
166  template < typename _HTy2 >
167  void attach( _HTy2 h, SimpleDeleterContext * ctx )
168  {
169  _reset0( h, ctx );
170  }
171 
172  SimpleHandleData detach()
173  {
174  SimpleHandleData r = _self;
175  _self.h = static_cast<_HTy>(0);
176  _self.ctx = 0;
177  return r;
178  }
179 
180  void reset()
181  {
182  _reset0( static_cast<_HTy>(0), 0 );
183  }
184 
185  _HTy get() const { return _self.h; }
186 
187  operator bool() const { return _self.ctx != 0; }
188 
189  _HTy operator -> ()
190  {
191  return _self.h;
192  }
193 
194  _HTy operator -> () const
195  {
196  return _self.h;
197  }
198 
199 protected:
201  template < typename _HTy2 >
202  void _reset0( _HTy2 newH, SimpleDeleterContext * newCtx )
203  {
204  if ( _self.ctx )
205  _self.ctx->release();
206  _self.h = newH;
207  _self.ctx = newCtx;
208  }
209 
211  template < typename _HTy2 >
212  void _reset( _HTy2 & otherH, SimpleDeleterContext * & otherCtx )
213  {
214  _reset0( otherH, otherCtx );
215  otherH = static_cast<_HTy2>(0);
216  otherCtx = 0;
217  }
218 
220  template < typename _HTy2 >
221  void _reset( SimpleHandle<_HTy2> const & other )
222  {
223  SimpleHandle<_HTy2> & o = const_cast< SimpleHandle<_HTy2> & >(other);
224  _reset( o._self.h, o._self.ctx );
225  }
226 
227  SimpleHandleData _self;
228 private:
229  template < typename _HTy0 >
230  friend class SimpleHandle;
231 };
232 
234 template < typename _Ty >
235 class SimplePointer : public SimpleHandle<_Ty*>
236 {
237 public:
239  typedef _Ty Type;
240 
242 
243  explicit SimplePointer( _Ty* p ) : MyBase( p, (_Ty*)0 ) { }
244 
245  template < typename _Dt >
246  SimplePointer( _Ty* p, _Dt dt ) : MyBase( p, (_Ty*)0, dt ) { }
247 
248  template < typename _Ty2 >
249  explicit SimplePointer( _Ty2* p ) : MyBase( p, (_Ty2*)0 ) { }
250 
251  template < typename _Ty2, typename _Dt >
252  SimplePointer( _Ty2* p, _Dt dt ) : MyBase( p, (_Ty2*)0, dt ) { }
253 
254  SimplePointer( SimplePointer const & other )
255  {
256  this->_reset(other);
257  }
258 
259  SimplePointer & operator = ( SimplePointer const & other )
260  {
261  this->_reset(other);
262  return *this;
263  }
264 
265  template < typename _Ty2 >
267  {
268  this->_reset(other);
269  }
270 
271  template < typename _Ty2 >
272  SimplePointer & operator = ( SimplePointer<_Ty2> const & other )
273  {
274  this->_reset(other);
275  return *this;
276  }
277 
278  void attachNew( _Ty * p )
279  {
280  MyBase::attachNew( p, (_Ty*)0 );
281  }
282 
283  template < typename _Dt >
284  void attachNew( _Ty * p, _Dt dt )
285  {
286  MyBase::attachNew( p, (_Ty*)0, dt );
287  }
288 
289  template < typename _Ty2 >
290  void attachNew( _Ty2 * p )
291  {
292  MyBase::attachNew( p, (_Ty2*)0 );
293  }
294 
295  template < typename _Ty2, typename _Dt >
296  void attachNew( _Ty2 * p, _Dt dt )
297  {
298  MyBase::attachNew( p, (_Ty2*)0, dt );
299  }
300 
305  template < typename _Ty2 >
307  {
309  typename SimplePointer<_Ty2>::HType p = dynamic_cast< typename SimplePointer<_Ty2>::HType >(this->_self.h);
310  if ( p != 0 )
311  {
312  r._reset( p, this->_self.ctx );
313  this->_self.h = static_cast<typename MyBase::HType>(0);
314  this->_self.ctx = 0;
315  }
316  return r;
317  }
318 
323  template < typename _Ty2 >
325  {
327  typename SimplePointer<_Ty2>::HType p = static_cast< typename SimplePointer<_Ty2>::HType >(this->_self.h);
328  r._reset( p, this->_self.ctx );
329  this->_self.h = static_cast<typename MyBase::HType>(0);
330  this->_self.ctx = 0;
331  return r;
332  }
333 
334  template < typename _Ty0 >
335  friend class SimplePointer;
336 };
337 
339 
341 WINUX_FUNC_DECL(long) LongAtomicIncrement( long volatile * p );
343 WINUX_FUNC_DECL(long) LongAtomicDecrement( long volatile * p );
345 WINUX_FUNC_DECL(long) LongAtomicCompareExchange( long volatile * p, long exchange, long comparand );
346 
349 {
350 private:
351  long volatile _uses;
352  long volatile _weaks;
353 
355  virtual void _destroy() = 0;
357  virtual void _deleteThis() = 0;
358 protected:
359  SharedDeleterContext() : _uses(1), _weaks(1) { }
360  virtual ~SharedDeleterContext() { }
361 
362 public:
366  bool _incRefNz()
367  {
368  for ( ; ; )
369  {
370  // loop until state is known
371  long count = (long volatile &)_uses;
372  if ( count == 0 ) return false;
373  if ( LongAtomicCompareExchange( &_uses, count + 1, count ) == count ) return true;
374  }
375  }
377  void incRef() { LongAtomicIncrement(&_uses); }
379  void decRef()
380  {
381  if ( LongAtomicDecrement(&_uses) == 0 )
382  {
383  this->_destroy();
384  this->decWRef();
385  }
386  }
387 
389  void incWRef() { LongAtomicIncrement(&_weaks); }
391  void decWRef()
392  {
393  if ( LongAtomicDecrement(&_weaks) == 0 )
394  {
395  this->_deleteThis();
396  }
397  }
398 
400  long useCount() const { return (_uses); }
401 
403  bool expired() const { return ( _uses == 0 ); }
404 
406  long weakCount() const { return (_weaks); }
407 
408  DISABLE_OBJECT_COPY(SharedDeleterContext)
409 };
410 
412 template < typename _HTy >
414 {
415 public:
416  SharedDefaultDeleterContext( _HTy h ) : _h(h) { }
417 private:
418  virtual void _destroy() { delete _h; }
419  virtual void _deleteThis() { delete this; }
420 
421  _HTy _h;
422 };
423 
425 template < typename _HTy, typename _Dt >
427 {
428 public:
429  SharedCustomDeleterContext( _HTy h, _Dt dt ) : _h(h), _dt(dt) { }
430 private:
431  virtual void _destroy() { _dt(_h); }
432  virtual void _deleteThis() { delete this; }
433 
434  _HTy _h;
435  _Dt _dt;
436 };
437 
441 template < typename _HTy >
443 {
444 public:
445  typedef _HTy HType;
446 
448  {
449  _HTy h;
451  SharedHandleData() : h(0), ctx(0) { }
452  SharedHandleData( _HTy h, SharedDeleterContext * ctx ) : h(h), ctx(ctx) { }
453  };
454 
457 
458  SharedHandle( _HTy h, _HTy failVal ) { attachNew( h, failVal ); }
459 
460  template < typename _Dt >
461  SharedHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
462 
463  template < typename _HTy2 >
464  SharedHandle( _HTy2 h, _HTy2 failVal ) { attachNew( h, failVal ); }
465 
466  template < typename _HTy2, typename _Dt >
467  SharedHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
468 
469  virtual ~SharedHandle()
470  {
471  reset();
472  }
473 
474  SharedHandle( SharedHandle const & other )
475  {
476  _reset(other);
477  }
478 
479  SharedHandle & operator = ( SharedHandle const & other )
480  {
481  _reset(other);
482  return *this;
483  }
484 
485  template < typename _HTy2 >
487  {
488  _reset(other);
489  }
490 
491  template < typename _HTy2 >
492  SharedHandle & operator = ( SharedHandle<_HTy2> const & other )
493  {
494  _reset(other);
495  return *this;
496  }
497 
498  void attachNew( _HTy h, _HTy failVal )
499  {
500  this->_reset0( h, ( h != failVal ? new SharedDefaultDeleterContext<_HTy>(h) : 0 ) );
501  }
502 
503  template < typename _Dt >
504  void attachNew( _HTy h, _HTy failVal, _Dt dt )
505  {
506  this->_reset0( h, ( h != failVal ? new SharedCustomDeleterContext< _HTy, _Dt >( h, dt ) : 0 ) );
507  }
508 
509  template < typename _HTy2 >
510  void attachNew( _HTy2 h, _HTy2 failVal )
511  {
512  this->_reset0( h, ( h != failVal ? new SharedDefaultDeleterContext<_HTy2>(h) : 0 ) );
513  }
514 
515  template < typename _HTy2, typename _Dt >
516  void attachNew( _HTy2 h, _HTy2 failVal, _Dt dt )
517  {
518  this->_reset0( h, ( h != failVal ? new SharedCustomDeleterContext< _HTy2, _Dt >( h, dt ) : 0 ) );
519  }
520 
526  void attach( SharedHandleData const & data, bool isIncRef )
527  {
528  if ( isIncRef )
529  {
530  _reset( data.h, data.ctx );
531  }
532  else
533  {
534  _reset0( data.h, data.ctx );
535  }
536  }
537 
543  template < typename _HTy2 >
544  void attach( typename SharedHandle<_HTy2>::SharedHandleData const & data, bool isIncRef )
545  {
546  if ( isIncRef )
547  {
548  _reset( data.h, data.ctx );
549  }
550  else
551  {
552  _reset0( data.h, data.ctx );
553  }
554  }
555 
561  template < typename _HTy2 >
562  void attach( _HTy2 h, SharedDeleterContext * ctx, bool isIncRef )
563  {
564  if ( isIncRef )
565  {
566  _reset( h, ctx );
567  }
568  else
569  {
570  _reset0( h, ctx );
571  }
572  }
573 
574  SharedHandleData detach()
575  {
576  SharedHandleData r = _self;
577  _self.h = static_cast<_HTy>(0);
578  _self.ctx = 0;
579  return r;
580  }
581 
582  SharedHandleData peek() const
583  {
584  return _self;
585  }
586 
587  void reset()
588  {
589  _reset0( static_cast<_HTy>(0), 0 );
590  }
591 
592  _HTy get() const { return _self.h; }
593 
594  operator bool() const { return _self.ctx != 0; }
595 
596  _HTy operator -> ()
597  {
598  return _self.h;
599  }
600 
601  _HTy operator -> () const
602  {
603  return _self.h;
604  }
605 
606 protected:
608  template < typename _HTy2 >
609  void _reset0( _HTy2 newH, SharedDeleterContext * newCtx )
610  {
611  if ( _self.ctx )
612  _self.ctx->decRef();
613  _self.h = newH;
614  _self.ctx = newCtx;
615  }
616 
618  template < typename _HTy2 >
619  void _reset( _HTy2 otherH, SharedDeleterContext * otherCtx )
620  {
621  if ( otherCtx )
622  otherCtx->incRef();
623  _reset0( otherH, otherCtx );
624  }
625 
627  template < typename _HTy2 >
628  void _reset( SharedHandle<_HTy2> const & other )
629  {
630  _reset( other._self.h, other._self.ctx );
631  }
632 
633  SharedHandleData _self;
634 private:
635  template < typename _HTy0 >
636  friend class SharedHandle;
637  template < typename _HTy0 >
638  friend class WeakHandle;
639 };
640 
641 template < typename _Ty >
642 class SharedPointer : public SharedHandle<_Ty*>
643 {
644 public:
646  typedef _Ty Type;
647 
649 
650  explicit SharedPointer( _Ty* p ) : MyBase( p, (_Ty*)0 ) { }
651 
652  template < typename _Dt >
653  SharedPointer( _Ty* p, _Dt dt ) : MyBase( p, (_Ty*)0, dt ) { }
654 
655  template < typename _Ty2 >
656  explicit SharedPointer( _Ty2* p ) : MyBase( p, (_Ty2*)0 ) { }
657 
658  template < typename _Ty2, typename _Dt >
659  SharedPointer( _Ty2* p, _Dt dt ) : MyBase( p, (_Ty2*)0, dt ) { }
660 
661  SharedPointer( SharedPointer const & other )
662  {
663  this->_reset(other);
664  }
665 
666  SharedPointer & operator = ( SharedPointer const & other )
667  {
668  this->_reset(other);
669  return *this;
670  }
671 
672  template < typename _Ty2 >
674  {
675  this->_reset(other);
676  }
677 
678  template < typename _Ty2 >
679  SharedPointer & operator = ( SharedPointer<_Ty2> const & other )
680  {
681  this->_reset(other);
682  return *this;
683  }
684 
685  void attachNew( _Ty * p )
686  {
687  MyBase::attachNew( p, (_Ty*)0 );
688  }
689 
690  template < typename _Dt >
691  void attachNew( _Ty * p, _Dt dt )
692  {
693  MyBase::attachNew( p, (_Ty*)0, dt );
694  }
695 
696  template < typename _Ty2 >
697  void attachNew( _Ty2 * p )
698  {
699  MyBase::attachNew( p, (_Ty2*)0 );
700  }
701 
702  template < typename _Ty2, typename _Dt >
703  void attachNew( _Ty2 * p, _Dt dt )
704  {
705  MyBase::attachNew( p, (_Ty2*)0, dt );
706  }
707 
712  template < typename _Ty2 >
714  {
716  typename SharedPointer<_Ty2>::HType p = dynamic_cast< typename SharedPointer<_Ty2>::HType >(this->_self.h);
717  if ( p != 0 )
718  r._reset( p, this->_self.ctx );
719  return r;
720  }
721 
726  template < typename _Ty2 >
728  {
730  r._reset( static_cast< typename SharedPointer<_Ty2>::HType >(this->_self.h), this->_self.ctx );
731  return r;
732  }
733 
734  template < typename _Ty0 >
735  friend class SharedPointer;
736  template < typename _Ty0 >
737  friend class WeakPointer;
738 };
739 
741 template < typename _HTy >
743 {
744 public:
745  typedef _HTy HType;
746 
748 
750 
751  virtual ~WeakHandle()
752  {
753  reset();
754  }
755 
756  template < typename _HTy2 >
758  {
759  _reset( other._self.h, other._self.ctx );
760  }
761 
762  template < typename _HTy2 >
763  WeakHandle & operator = ( SharedHandle<_HTy2> const & other )
764  {
765  _reset( other._self.h, other._self.ctx );
766  return *this;
767  }
768 
769  WeakHandle( WeakHandle const & other )
770  {
771  _reset(other);
772  }
773 
774  WeakHandle & operator = ( WeakHandle const & other )
775  {
776  _reset(other);
777  return *this;
778  }
779 
780  template < typename _HTy2 >
781  WeakHandle( WeakHandle<_HTy2> const & other )
782  {
783  _reset(other);
784  }
785 
786  template < typename _HTy2 >
787  WeakHandle & operator = ( WeakHandle<_HTy2> const & other )
788  {
789  _reset(other);
790  return *this;
791  }
792 
793  void reset()
794  {
795  _reset( static_cast<_HTy>(0), 0 );
796  }
797 
799  {
801  if ( !this->_sharedReset(&r) ) { }
802  return r;
803  }
804 
808  bool expired() const { return ( !_self.ctx || _self.ctx->expired() ); }
809 
810  operator bool() { return _self.ctx != 0; }
811  operator bool() const { return _self.ctx != 0; }
812 
813 protected:
815  template < typename _HTy2 >
816  bool _sharedReset( SharedHandle<_HTy2> * pSharedHandle ) const
817  {
818  if ( _self.ctx && _self.ctx->_incRefNz() )
819  {
820  // 由于之前_incRefNz()已经增加引用计数,因此这里当作新资源看待
821  pSharedHandle->_reset0( _self.h, _self.ctx );
822  return true;
823  }
824  return false;
825  }
826 
828  template < typename _HTy2 >
829  void _reset( _HTy2 otherH, SharedDeleterContext * otherCtx )
830  {
831  if ( otherCtx != 0 )
832  otherCtx->incWRef();
833  if ( _self.ctx != 0 )
834  _self.ctx->decWRef();
835  _self.h = otherH;
836  _self.ctx = otherCtx;
837  }
838 
840  template < typename _HTy2 >
841  void _reset( WeakHandle<_HTy2> const & other )
842  {
843  _reset( other._self.h, other._self.ctx );
844  }
845 
846  WeakHandleData _self;
847 
848  template < typename _HTy0 >
849  friend class WeakHandle;
850 };
851 
853 template < typename _Ty >
854 class WeakPointer : public WeakHandle<_Ty*>
855 {
856 public:
858  typedef _Ty Type;
859 
861 
862  template < typename _Ty2 >
864  {
865  this->_reset( other._self.h, other._self.ctx );
866  }
867 
868  template < typename _Ty2 >
869  WeakPointer & operator = ( SharedPointer<_Ty2> const & other )
870  {
871  this->_reset( other._self.h, other._self.ctx );
872  return *this;
873  }
874 
875  WeakPointer( WeakPointer const & other )
876  {
877  this->_reset(other);
878  }
879 
880  WeakPointer & operator = ( WeakPointer const & other )
881  {
882  this->_reset(other);
883  return *this;
884  }
885 
886  template < typename _Ty2 >
888  {
889  this->_reset(other);
890  }
891 
892  template < typename _Ty2 >
893  WeakPointer & operator = ( WeakPointer<_Ty2> const & other )
894  {
895  this->_reset(other);
896  return *this;
897  }
898 
900  {
902  if ( !this->_sharedReset(&r) ) { }
903  return r;
904  }
905 
906  template < typename _Ty0 >
907  friend class WeakPointer;
908 };
909 
910 template < typename _Ty >
911 inline SimplePointer<_Ty> MakeSimple( _Ty * newObj )
912 {
913  return SimplePointer<_Ty>(newObj);
914 }
915 
916 template < typename _Ty, typename _Dt >
917 inline SimplePointer<_Ty> MakeSimple( _Ty * newObj, _Dt dt )
918 {
919  return SimplePointer<_Ty>( newObj, dt );
920 }
921 
922 template < typename _Ty >
923 inline SharedPointer<_Ty> MakeShared( _Ty * newObj )
924 {
925  return SharedPointer<_Ty>(newObj);
926 }
927 
928 template < typename _Ty, typename _Dt >
929 inline SharedPointer<_Ty> MakeShared( _Ty * newObj, _Dt dt )
930 {
931  return SharedPointer<_Ty>( newObj, dt );
932 }
933 
934 
935 } // namespace winux
936 
937 #endif // __SMARTPTR_HPP__
SimpleHandle< _Ty * > MyBase
Definition: smartptr.hpp:238
bool _sharedReset(SharedHandle< _HTy2 > *pSharedHandle) const
Shared*PTR* call reset。用于从Weak*PTR*创建Shared*PTR*.
Definition: smartptr.hpp:816
void attachNew(_Ty *p)
Definition: smartptr.hpp:278
WeakPointer(WeakPointer const &other)
Definition: smartptr.hpp:875
弱句柄
Definition: smartptr.hpp:742
SimpleHandleData _self
Definition: smartptr.hpp:227
SharedHandle(_HTy2 h, _HTy2 failVal)
Definition: smartptr.hpp:464
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个shared的资源引用计数,减少自身计数。管理另一个shared的资源
Definition: smartptr.hpp:619
void _reset0(_HTy2 newH, SimpleDeleterContext *newCtx)
释放自身资源,管理新资源
Definition: smartptr.hpp:202
Shared自定义删除器场景
Definition: smartptr.hpp:426
void attach(_HTy2 h, SharedDeleterContext *ctx, bool isIncRef)
管理一个资源
Definition: smartptr.hpp:562
static void Delete(_Ty *p)
Definition: smartptr.hpp:21
void release()
销毁资源和SimpleDeleterContext自身
Definition: smartptr.hpp:36
SimpleCustomDeleterContext(_HTy h, _Dt dt)
Definition: smartptr.hpp:64
SimpleHandle(SimpleHandle< _HTy2 > const &other)
Definition: smartptr.hpp:121
SharedPointer(_Ty2 *p, _Dt dt)
Definition: smartptr.hpp:659
SimplePointer(_Ty2 *p)
Definition: smartptr.hpp:249
SharedPointer(_Ty *p, _Dt dt)
Definition: smartptr.hpp:653
Shared删除器场景基类
Definition: smartptr.hpp:348
bool expired() const
资源是否已过期
Definition: smartptr.hpp:403
Simple默认删除器场景
Definition: smartptr.hpp:48
void _reset0(_HTy2 newH, SharedDeleterContext *newCtx)
减少自身引用计数,管理新资源
Definition: smartptr.hpp:609
long useCount() const
资源引用计数
Definition: smartptr.hpp:400
SharedPointer< _Ty > lock() const
Definition: smartptr.hpp:899
#define WINUX_DLL
Definition: utilities.hpp:60
void attach(typename SimpleHandle< _HTy2 >::SimpleHandleData const &data)
Definition: smartptr.hpp:161
Simple自定义删除器场景
Definition: smartptr.hpp:61
void _reset(SharedHandle< _HTy2 > const &other)
增加另一个shared的资源引用计数,减少自身计数。管理另一个shared的资源
Definition: smartptr.hpp:628
long LongAtomicCompareExchange(long volatile *p, long exchange, long comparand)
原子化操作,*p若和comparand相等,就把*p赋成exchange,返回值是初始的*p值
SharedHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
Definition: smartptr.hpp:467
简单句柄类,管理各种资源的自动释放,赋值相当于传递管理权。
Definition: smartptr.hpp:78
void _reset(WeakHandle< _HTy2 > const &other)
增加另一个弱引用计数,减少自身弱计数。管理另一个Weak*PTR*
Definition: smartptr.hpp:841
SharedCustomDeleterContext(_HTy h, _Dt dt)
Definition: smartptr.hpp:429
void incRef()
增加引用计数
Definition: smartptr.hpp:377
void attach(SimpleHandleData const &data)
Definition: smartptr.hpp:155
void attachNew(_Ty2 *p, _Dt dt)
Definition: smartptr.hpp:296
long LongAtomicDecrement(long volatile *p)
原子化使一个Long型变量-1,返回值是-1后的*p值
static void DeleteArray(_Ty *p)
Definition: smartptr.hpp:22
WeakHandle< _Ty * > MyBase
Definition: smartptr.hpp:857
void attachNew(_HTy h, _HTy failVal)
Definition: smartptr.hpp:498
void delThis()
显式删除detach()出来的SimpleDeleterContext对象
Definition: smartptr.hpp:43
SimpleHandle(_HTy2 h, _HTy2 failVal)
Definition: smartptr.hpp:99
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个弱引用计数,减少自身弱计数。管理另一个Weak*PTR*
Definition: smartptr.hpp:829
void attachNew(_Ty2 *p)
Definition: smartptr.hpp:697
Simple删除器场景基类
Definition: smartptr.hpp:26
static _Ty * NewArray(std::size_t count)
Definition: smartptr.hpp:14
WeakPointer(WeakPointer< _Ty2 > const &other)
Definition: smartptr.hpp:887
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
Definition: smartptr.hpp:516
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:81
void decRef()
减少引用计数.当引用计数为0时销毁资源,并且销毁资源时减少弱引用计数.
Definition: smartptr.hpp:379
void attach(typename SharedHandle< _HTy2 >::SharedHandleData const &data, bool isIncRef)
管理一个资源
Definition: smartptr.hpp:544
SharedHandleData _self
Definition: smartptr.hpp:633
SharedHandle(_HTy h, _HTy failVal)
Definition: smartptr.hpp:458
SharedHandle< _Ty * > MyBase
Definition: smartptr.hpp:645
bool _incRefNz()
如果引用计数不是0,则增加引用计数。成功则返回true。
Definition: smartptr.hpp:366
void attachNew(_Ty *p)
Definition: smartptr.hpp:685
void attachNew(_HTy h, _HTy failVal, _Dt dt)
Definition: smartptr.hpp:504
SimpleHandle(_HTy h, _HTy failVal)
Definition: smartptr.hpp:93
WeakHandle(WeakHandle< _HTy2 > const &other)
Definition: smartptr.hpp:781
SimplePointer(SimplePointer< _Ty2 > const &other)
Definition: smartptr.hpp:266
SimpleHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
Definition: smartptr.hpp:102
SharedHandleData peek() const
Definition: smartptr.hpp:582
void attachNew(_Ty *p, _Dt dt)
Definition: smartptr.hpp:691
void incWRef()
增加弱引用计数
Definition: smartptr.hpp:389
SimpleDeleterContext * ctx
Definition: smartptr.hpp:86
SharedPointer(SharedPointer const &other)
Definition: smartptr.hpp:661
SharedPointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:713
WeakHandle(WeakHandle const &other)
Definition: smartptr.hpp:769
virtual ~SharedHandle()
Definition: smartptr.hpp:469
void _reset(SimpleHandle< _HTy2 > const &other)
释放自身资源,接管另一个simple的资源,另一个simple置零
Definition: smartptr.hpp:221
long LongAtomicIncrement(long volatile *p)
原子化使一个Long型变量+1,返回值是+1后的*p值
void attach(SharedHandleData const &data, bool isIncRef)
管理一个资源
Definition: smartptr.hpp:526
SharedHandle< _HTy >::SharedHandleData WeakHandleData
Definition: smartptr.hpp:747
Shared默认删除器场景
Definition: smartptr.hpp:413
SimplePointer(_Ty2 *p, _Dt dt)
Definition: smartptr.hpp:252
SimplePointer(_Ty *p, _Dt dt)
Definition: smartptr.hpp:246
SharedHandle(SharedHandle< _HTy2 > const &other)
Definition: smartptr.hpp:486
线程
Definition: threads.hpp:92
SimpleHandle(_HTy h, _HTy failVal, _Dt dt)
Definition: smartptr.hpp:96
SimpleHandle(SimpleHandle const &other)
Definition: smartptr.hpp:109
SharedHandleData detach()
Definition: smartptr.hpp:574
void decWRef()
减少弱引用计数,当弱引用计数为0时销毁删除器场景对象
Definition: smartptr.hpp:391
SharedPointer(_Ty2 *p)
Definition: smartptr.hpp:656
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
Definition: smartptr.hpp:150
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
WeakPointer(SharedPointer< _Ty2 > const &other)
Definition: smartptr.hpp:863
virtual ~WeakHandle()
Definition: smartptr.hpp:751
SimplePointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:306
bool expired() const
是否过期
Definition: smartptr.hpp:808
SimplePointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:324
SharedPointer(SharedPointer< _Ty2 > const &other)
Definition: smartptr.hpp:673
SharedHandle(_HTy h, _HTy failVal, _Dt dt)
Definition: smartptr.hpp:461
SharedHandleData(_HTy h, SharedDeleterContext *ctx)
Definition: smartptr.hpp:452
void _reset(_HTy2 &otherH, SimpleDeleterContext *&otherCtx)
释放自身资源,接管另一个simple的资源,另一个simple置零
Definition: smartptr.hpp:212
构造分配器
Definition: smartptr.hpp:11
long weakCount() const
弱引用计数
Definition: smartptr.hpp:406
SharedHandle(SharedHandle const &other)
Definition: smartptr.hpp:474
简单指针
Definition: smartptr.hpp:235
WeakHandle(SharedHandle< _HTy2 > const &other)
Definition: smartptr.hpp:757
SimpleHandleData(_HTy h, SimpleDeleterContext *ctx)
Definition: smartptr.hpp:88
void attachNew(_HTy h, _HTy failVal)
Definition: smartptr.hpp:133
SimplePointer< _Ty > MakeSimple(_Ty *newObj)
Definition: smartptr.hpp:911
void attach(_HTy2 h, SimpleDeleterContext *ctx)
Definition: smartptr.hpp:167
void attachNew(_Ty *p, _Dt dt)
Definition: smartptr.hpp:284
void attachNew(_Ty2 *p)
Definition: smartptr.hpp:290
void attachNew(_HTy h, _HTy failVal, _Dt dt)
Definition: smartptr.hpp:139
SharedPointer< _Ty > MakeShared(_Ty *newObj)
Definition: smartptr.hpp:923
SimplePointer(SimplePointer const &other)
Definition: smartptr.hpp:254
SharedHandle< _HTy > lock() const
Definition: smartptr.hpp:798
WeakHandleData _self
Definition: smartptr.hpp:846
析构释放器
Definition: smartptr.hpp:19
virtual ~SimpleHandle()
Definition: smartptr.hpp:104
void attachNew(_HTy2 h, _HTy2 failVal)
Definition: smartptr.hpp:510
SimpleHandleData detach()
Definition: smartptr.hpp:172
SharedPointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:727
static _Ty * New()
Definition: smartptr.hpp:13
void attachNew(_Ty2 *p, _Dt dt)
Definition: smartptr.hpp:703
跨平台基础功能库
Definition: archives.hpp:7
void attachNew(_HTy2 h, _HTy2 failVal)
Definition: smartptr.hpp:145
引用计数共享句柄,管理各种资源的自动释放
Definition: smartptr.hpp:442