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