fastdo  0.6.16
smartptr.hpp
浏览该文件的文档.
1 #ifndef __SMARTPTR_HPP__
2 #define __SMARTPTR_HPP__
3 //
4 // smartptr 提供智能指针相关的功能
5 //
6 
7 namespace winux
8 {
10 class SmartPtrError : public Error
11 {
12 public:
14  {
22  };
23 
24  SmartPtrError( SmartPtrErrorType err, AnsiString const & errStr ) : Error( err, errStr ) { }
25 };
26 
29 {
30 protected:
32  virtual ~SimpleDeleterContext() { }
33 
34 public:
36  void release()
37  {
38  this->_destroy();
39  this->_deleteThis();
40  }
41 
43  void delThis() { this->_deleteThis(); }
44 
45 protected:
47  virtual void _destroy() = 0;
49  virtual void _deleteThis() = 0;
50 };
51 
53 template < typename _HTy >
55 {
56 public:
57  SimpleDefaultDeleterContext( _HTy h ) : _h(h) { }
58 
59 protected:
60  virtual void _destroy() override { delete _h; }
61  virtual void _deleteThis() override { delete this; }
62 
63 private:
64  _HTy _h;
65 };
66 
68 template < typename _HTy, typename _Dt >
70 {
71 public:
72  SimpleCustomDeleterContext( _HTy h, _Dt dt ) : _h(h), _dt(dt) { }
73 
74 protected:
75  virtual void _destroy() override { _dt(_h); }
76  virtual void _deleteThis() override { delete this; }
77 
78 private:
79  _HTy _h;
80  _Dt _dt;
81 };
82 
87 template < typename _HTy >
89 {
90 public:
92  typedef _HTy HType;
93 
96  {
97  _HTy h;
99  SimpleHandleData() : h(0), ctx(0) { }
100  SimpleHandleData( _HTy h, SimpleDeleterContext * ctx ) : h(h), ctx(ctx) { }
101  };
102 
105 
110  SimpleHandle( _HTy h, _HTy failVal ) { attachNew( h, failVal ); }
111 
117  template < typename _Dt >
118  SimpleHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
119 
124  template < typename _HTy2 >
125  SimpleHandle( _HTy2 h, _HTy2 failVal ) { attachNew( h, failVal ); }
126 
132  template < typename _HTy2, typename _Dt >
133  SimpleHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
134 
137  {
138  reset();
139  }
140 
142  SimpleHandle( SimpleHandle const & other )
143  {
144  _reset(other);
145  }
146 
148  SimpleHandle & operator = ( SimpleHandle const & other )
149  {
150  _reset(other);
151  return *this;
152  }
153 
155  template < typename _HTy2 >
157  {
158  _reset(other);
159  }
160 
162  template < typename _HTy2 >
163  SimpleHandle & operator = ( SimpleHandle<_HTy2> const & other )
164  {
165  _reset(other);
166  return *this;
167  }
168 
173  void attachNew( _HTy h, _HTy failVal )
174  {
175  this->_reset0( h, ( h != failVal ? new SimpleDefaultDeleterContext<_HTy>(h) : 0 ) );
176  }
177 
183  template < typename _Dt >
184  void attachNew( _HTy h, _HTy failVal, _Dt dt )
185  {
186  this->_reset0( h, ( h != failVal ? new SimpleCustomDeleterContext< _HTy, _Dt >( h, dt ) : 0 ) );
187  }
188 
193  template < typename _HTy2 >
194  void attachNew( _HTy2 h, _HTy2 failVal )
195  {
196  this->_reset0( h, ( h != failVal ? new SimpleDefaultDeleterContext<_HTy2>(h) : 0 ) );
197  }
198 
204  template < typename _HTy2, typename _Dt >
205  void attachNew( _HTy2 h, _HTy2 failVal, _Dt dt )
206  {
207  this->_reset0( h, ( h != failVal ? new SimpleCustomDeleterContext< _HTy2, _Dt >( h, dt ) : 0 ) );
208  }
209 
211  void attach( SimpleHandleData const & data )
212  {
213  _reset0( data.h, data.ctx );
214  }
215 
217  template < typename _HTy2 >
218  void attach( typename SimpleHandle<_HTy2>::SimpleHandleData const & data )
219  {
220  _reset0( data.h, data.ctx );
221  }
222 
224  template < typename _HTy2 >
225  void attach( _HTy2 h, SimpleDeleterContext * ctx )
226  {
227  _reset0( h, ctx );
228  }
229 
231  SimpleHandleData detach()
232  {
233  SimpleHandleData r = _self;
234  _self.h = static_cast<_HTy>(0);
235  _self.ctx = 0;
236  return r;
237  }
238 
240  void reset()
241  {
242  _reset0( static_cast<_HTy>(0), 0 );
243  }
244 
246  _HTy get() const { return _self.h; }
247  SimpleDeleterContext * getContext() const { return _self.ctx; }
248 
250  operator bool() const { return _self.ctx != 0; }
251 
253  bool operator < ( SimpleHandle const & other ) const { return _self.h < other._self.h; }
254 
255  _HTy operator -> ()
256  {
257  return _self.h;
258  }
259 
260  _HTy operator -> () const
261  {
262  return _self.h;
263  }
264 
265 protected:
267  template < typename _HTy2 >
268  void _reset0( _HTy2 newH, SimpleDeleterContext * newCtx )
269  {
270  if ( _self.ctx )
271  _self.ctx->release();
272  _self.h = newH;
273  _self.ctx = newCtx;
274  }
275 
277  template < typename _HTy2 >
278  void _reset( _HTy2 & otherH, SimpleDeleterContext * & otherCtx )
279  {
280  _reset0( otherH, otherCtx );
281  otherH = static_cast<_HTy2>(0);
282  otherCtx = 0;
283  }
284 
286  template < typename _HTy2 >
287  void _reset( SimpleHandle<_HTy2> const & other )
288  {
289  SimpleHandle<_HTy2> & o = const_cast< SimpleHandle<_HTy2> & >(other);
290  _reset( o._self.h, o._self.ctx );
291  }
292 
293  SimpleHandleData _self;
294 
295 private:
296  template < typename _HTy0 >
297  friend class SimpleHandle;
298 };
299 
301 template < typename _Ty >
302 class SimplePointer : public SimpleHandle<_Ty*>
303 {
304 public:
308  typedef _Ty Type;
309 
312 
316  explicit SimplePointer( _Ty* p ) : MyBase( p, (_Ty*)0 ) { }
317 
322  template < typename _Dt >
323  SimplePointer( _Ty* p, _Dt dt ) : MyBase( p, (_Ty*)0, dt ) { }
324 
328  template < typename _Ty2 >
329  explicit SimplePointer( _Ty2* p ) : MyBase( p, (_Ty2*)0 ) { }
330 
335  template < typename _Ty2, typename _Dt >
336  SimplePointer( _Ty2* p, _Dt dt ) : MyBase( p, (_Ty2*)0, dt ) { }
337 
339  SimplePointer( SimplePointer const & other )
340  {
341  this->_reset(other);
342  }
343 
345  SimplePointer & operator = ( SimplePointer const & other )
346  {
347  this->_reset(other);
348  return *this;
349  }
350 
352  template < typename _Ty2 >
354  {
355  this->_reset(other);
356  }
357 
359  template < typename _Ty2 >
360  SimplePointer & operator = ( SimplePointer<_Ty2> const & other )
361  {
362  this->_reset(other);
363  return *this;
364  }
365 
369  void attachNew( _Ty * p )
370  {
371  MyBase::attachNew( p, (_Ty*)0 );
372  }
373 
378  template < typename _Dt >
379  void attachNew( _Ty * p, _Dt dt )
380  {
381  MyBase::attachNew( p, (_Ty*)0, dt );
382  }
383 
387  template < typename _Ty2 >
388  void attachNew( _Ty2 * p )
389  {
390  MyBase::attachNew( p, (_Ty2*)0 );
391  }
392 
397  template < typename _Ty2, typename _Dt >
398  void attachNew( _Ty2 * p, _Dt dt )
399  {
400  MyBase::attachNew( p, (_Ty2*)0, dt );
401  }
402 
407  template < typename _Ty2 >
409  {
411  typename SimplePointer<_Ty2>::HType p = dynamic_cast< typename SimplePointer<_Ty2>::HType >(this->_self.h);
412  if ( p != 0 )
413  {
414  r._reset( p, this->_self.ctx );
415  this->_self.h = static_cast<typename MyBase::HType>(0);
416  this->_self.ctx = 0;
417  }
418  return r;
419  }
420 
426  template < typename _Ty2 >
428  {
430  typename SimplePointer<_Ty2>::HType p = static_cast< typename SimplePointer<_Ty2>::HType >(this->_self.h);
431  r._reset( p, this->_self.ctx );
432  this->_self.h = static_cast<typename MyBase::HType>(0);
433  this->_self.ctx = 0;
434  return r;
435  }
436 
437  template < typename _Ty0 >
438  friend class SimplePointer;
439 };
440 
442 
444 WINUX_FUNC_DECL(long) LongAtomicIncrement( long volatile * p );
446 WINUX_FUNC_DECL(long) LongAtomicDecrement( long volatile * p );
448 WINUX_FUNC_DECL(long) LongAtomicCompareExchange( long volatile * p, long exchange, long comparand );
449 
450 // 前向声明
451 template < typename _HTy >
453 template < typename _Ty >
455 template < typename _Ty >
457 
458 // For SFINAE
459 template < typename... _Ty >
460 struct MakeVoid { typedef void type; };
461 template < typename... _Ty >
462 using VoidT = typename MakeVoid<_Ty...>::type;
463 
464 // Detect unambiguous and accessible inheritance from EnableSharedFromThis
465 template < typename _Yty, typename = VoidT<> >
466 struct _CanEnableShared : std::false_type
467 {
468 };
469 
470 // is_convertible is necessary to verify unambiguous inheritance
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
473 {
474 };
475 
476 // enable sharedFromThis
477 template < typename _Other, typename _Yty >
478 void _EnableSharedFromThis1( SharedPointer<_Other> const & sharedThis, _Yty * ptr, std::true_type )
479 {
480  if ( ptr && ptr->_weakPtr.expired() )
481  {
482  ptr->_weakPtr = SharedPointer<typename std::remove_cv<_Yty>::type>( sharedThis, const_cast<typename std::remove_cv<_Yty>::type *>(ptr) );
483  }
484 }
485 
486 // don't enable sharedFromThis
487 template < typename _Other, typename _Yty >
488 void _EnableSharedFromThis1( SharedPointer<_Other> const &, _Yty *, std::false_type )
489 {
490 }
491 
492 // possibly enable sharedFromThis
493 template < typename _Other, typename _Yty >
494 void _EnableSharedFromThis( SharedPointer<_Other> const & sharedThis, _Yty * ptr )
495 {
496  _EnableSharedFromThis1( sharedThis, ptr, std::integral_constant< bool, _CanEnableShared<_Yty>::value >() );
497 }
498 
501 {
502 protected:
503  SharedDeleterContext() : _uses(1), _weaks(1) { }
504  virtual ~SharedDeleterContext() { }
505 
507  virtual void _destroy() = 0;
509  virtual void _deleteThis() = 0;
510 
511 public:
515  bool _incRefNz()
516  {
517  for ( ; ; )
518  {
519  // loop until state is known
520  long count = (long volatile &)_uses;
521  if ( count == 0 ) return false;
522  if ( LongAtomicCompareExchange( &_uses, count + 1, count ) == count ) return true;
523  }
524  }
526  void incRef() { LongAtomicIncrement(&_uses); }
528  void decRef()
529  {
530  if ( LongAtomicDecrement(&_uses) == 0 )
531  {
532  this->_destroy();
533  this->decWRef();
534  }
535  }
536 
538  void incWRef() { LongAtomicIncrement(&_weaks); }
540  void decWRef()
541  {
542  if ( LongAtomicDecrement(&_weaks) == 0 )
543  {
544  this->_deleteThis();
545  }
546  }
547 
549  long useCount() const { return (_uses); }
550 
552  bool expired() const { return ( _uses == 0 ); }
553 
555  long weakCount() const { return (_weaks); }
556 
557 private:
558  long volatile _uses;
559  long volatile _weaks;
560 
562 };
563 
565 template < typename _HTy >
567 {
568 public:
569  SharedDefaultDeleterContext( _HTy h ) : _h(h) { }
570 
571 protected:
572  virtual void _destroy() override { delete _h; }
573  virtual void _deleteThis() override { delete this; }
574 
575 private:
576  _HTy _h;
577 };
578 
580 template < typename _HTy, typename _Dt >
582 {
583 public:
584  SharedCustomDeleterContext( _HTy h, _Dt dt ) : _h(h), _dt(dt) { }
585 
586 protected:
587  virtual void _destroy() override { _dt(_h); }
588  virtual void _deleteThis() override { delete this; }
589 
590 private:
591  _HTy _h;
592  _Dt _dt;
593 };
594 
598 template < typename _HTy >
600 {
601 public:
603  typedef _HTy HType;
604 
607  {
608  _HTy h;
610  SharedHandleData() : h(0), ctx(0) { }
611  SharedHandleData( _HTy h, SharedDeleterContext * ctx ) : h(h), ctx(ctx) { }
612  };
613 
616 
621  SharedHandle( _HTy h, _HTy failVal ) { attachNew( h, failVal ); }
622 
628  template < typename _Dt >
629  SharedHandle( _HTy h, _HTy failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
630 
635  template < typename _HTy2 >
636  SharedHandle( _HTy2 h, _HTy2 failVal ) { attachNew( h, failVal ); }
637 
643  template < typename _HTy2, typename _Dt >
644  SharedHandle( _HTy2 h, _HTy2 failVal, _Dt dt ) { attachNew( h, failVal, dt ); }
645 
647  template < typename _HTy2 >
648  explicit SharedHandle( WeakHandle<_HTy2> const & other )
649  {
650  if ( !other._sharedReset(this) )
651  {
652  throw SmartPtrError( SmartPtrError::errBadWeakHandle, AnsiString("Bad WeakHandle<") + typeid(_HTy2).name() + ">" );
653  }
654  }
655 
658  {
659  reset();
660  }
661 
663  SharedHandle( SharedHandle const & other )
664  {
665  _reset(other);
666  }
667 
669  SharedHandle & operator = ( SharedHandle const & other )
670  {
671  _reset(other);
672  return *this;
673  }
674 
676  template < typename _HTy2 >
678  {
679  _reset(other);
680  }
681 
683  template < typename _HTy2 >
684  SharedHandle & operator = ( SharedHandle<_HTy2> const & other )
685  {
686  _reset(other);
687  return *this;
688  }
689 
694  void attachNew( _HTy h, _HTy failVal )
695  {
696  this->_reset0( h, ( h != failVal ? new SharedDefaultDeleterContext<_HTy>(h) : 0 ) );
697  }
698 
704  template < typename _Dt >
705  void attachNew( _HTy h, _HTy failVal, _Dt dt )
706  {
707  this->_reset0( h, ( h != failVal ? new SharedCustomDeleterContext< _HTy, _Dt >( h, dt ) : 0 ) );
708  }
709 
714  template < typename _HTy2 >
715  void attachNew( _HTy2 h, _HTy2 failVal )
716  {
717  this->_reset0( h, ( h != failVal ? new SharedDefaultDeleterContext<_HTy2>(h) : 0 ) );
718  }
719 
725  template < typename _HTy2, typename _Dt >
726  void attachNew( _HTy2 h, _HTy2 failVal, _Dt dt )
727  {
728  this->_reset0( h, ( h != failVal ? new SharedCustomDeleterContext< _HTy2, _Dt >( h, dt ) : 0 ) );
729  }
730 
736  void attach( SharedHandleData const & data, bool isIncRef )
737  {
738  if ( isIncRef )
739  {
740  _reset( data.h, data.ctx );
741  }
742  else
743  {
744  _reset0( data.h, data.ctx );
745  }
746  }
747 
753  template < typename _HTy2 >
754  void attach( typename SharedHandle<_HTy2>::SharedHandleData const & data, bool isIncRef )
755  {
756  if ( isIncRef )
757  {
758  _reset( data.h, data.ctx );
759  }
760  else
761  {
762  _reset0( data.h, data.ctx );
763  }
764  }
765 
771  template < typename _HTy2 >
772  void attach( _HTy2 h, SharedDeleterContext * ctx, bool isIncRef )
773  {
774  if ( isIncRef )
775  {
776  _reset( h, ctx );
777  }
778  else
779  {
780  _reset0( h, ctx );
781  }
782  }
783 
785  SharedHandleData detach()
786  {
787  SharedHandleData r = _self;
788  _self.h = static_cast<_HTy>(0);
789  _self.ctx = 0;
790  return r;
791  }
792 
794  SharedHandleData peek() const
795  {
796  return _self;
797  }
798 
800  void reset()
801  {
802  _reset0( static_cast<_HTy>(0), 0 );
803  }
804 
806  _HTy get() const { return _self.h; }
807  SharedDeleterContext * getContext() const { return _self.ctx; }
808 
810  operator bool() const { return _self.ctx != 0; }
811 
813  bool operator < ( SharedHandle const & other ) const { return _self.h < other._self.h; }
814 
815  _HTy operator -> ()
816  {
817  return _self.h;
818  }
819 
820  _HTy operator -> () const
821  {
822  return _self.h;
823  }
824 
825 protected:
827  template < typename _HTy2 >
828  void _reset0( _HTy2 newH, SharedDeleterContext * newCtx )
829  {
830  if ( _self.ctx )
831  _self.ctx->decRef();
832  _self.h = newH;
833  _self.ctx = newCtx;
834  }
835 
837  template < typename _HTy2 >
838  void _reset( _HTy2 otherH, SharedDeleterContext * otherCtx )
839  {
840  if ( otherCtx )
841  otherCtx->incRef();
842  _reset0( otherH, otherCtx );
843  }
844 
846  template < typename _HTy2 >
847  void _reset( SharedHandle<_HTy2> const & other )
848  {
849  _reset( other._self.h, other._self.ctx );
850  }
851 
852  SharedHandleData _self;
853 
854 private:
855  template < typename _HTy0 >
856  friend class SharedHandle;
857  template < typename _HTy0 >
858  friend class WeakHandle;
859 };
860 
862 template < typename _Ty >
863 class SharedPointer : public SharedHandle<_Ty*>
864 {
865 public:
869  typedef _Ty Type;
870 
873 
877  explicit SharedPointer( _Ty* p ) { attachNew(p); }
878 
883  template < typename _Dt >
884  SharedPointer( _Ty* p, _Dt dt ) { attachNew( p, dt ); }
885 
889  template < typename _Ty2 >
890  explicit SharedPointer( _Ty2* p ) { attachNew(p); }
891 
896  template < typename _Ty2, typename _Dt >
897  SharedPointer( _Ty2* p, _Dt dt ) { attachNew( p, dt ); }
898 
900  template < typename _Ty2 >
901  explicit SharedPointer( WeakPointer<_Ty2> const & other )
902  {
903  if ( !other._sharedReset(this) )
904  {
905  throw SmartPtrError( SmartPtrError::errBadWeakPointer, AnsiString("Bad WeakPointer<") + typeid(_Ty2).name() + ">" );
906  }
907  }
908 
910  template < typename _Ty2 >
911  SharedPointer( SharedPointer<_Ty2> const & other, _Ty2 * ptr ) noexcept
912  {
913  if ( other._self.ctx )
914  other._self.ctx->incRef();
915 
916  this->_self.h = ptr;
917  this->_self.ctx = other._self.ctx;
918  }
919 
921  SharedPointer( SharedPointer const & other )
922  {
923  this->_reset(other);
924  }
925 
927  SharedPointer & operator = ( SharedPointer const & other )
928  {
929  this->_reset(other);
930  return *this;
931  }
932 
934  template < typename _Ty2 >
936  {
937  this->_reset(other);
938  }
939 
941  template < typename _Ty2 >
942  SharedPointer & operator = ( SharedPointer<_Ty2> const & other )
943  {
944  this->_reset(other);
945  return *this;
946  }
947 
951  void attachNew( _Ty * p )
952  {
953  MyBase::attachNew( p, (_Ty*)0 );
954  _EnableSharedFromThis( *this, p );
955  }
956 
961  template < typename _Dt >
962  void attachNew( _Ty * p, _Dt dt )
963  {
964  MyBase::attachNew( p, (_Ty*)0, dt );
965  _EnableSharedFromThis( *this, p );
966  }
967 
971  template < typename _Ty2 >
972  void attachNew( _Ty2 * p )
973  {
974  MyBase::attachNew( p, (_Ty2*)0 );
975  _EnableSharedFromThis( *this, p );
976  }
977 
982  template < typename _Ty2, typename _Dt >
983  void attachNew( _Ty2 * p, _Dt dt )
984  {
985  MyBase::attachNew( p, (_Ty2*)0, dt );
986  _EnableSharedFromThis( *this, p );
987  }
988 
993  template < typename _Ty2 >
995  {
997  typename SharedPointer<_Ty2>::HType p = dynamic_cast< typename SharedPointer<_Ty2>::HType >(this->_self.h);
998  if ( p != 0 )
999  r._reset( p, this->_self.ctx );
1000  return r;
1001  }
1002 
1007  template < typename _Ty2 >
1009  {
1011  r._reset( static_cast< typename SharedPointer<_Ty2>::HType >(this->_self.h), this->_self.ctx );
1012  return r;
1013  }
1014 
1015 private:
1016  template < typename _Ty0 >
1017  friend class SharedPointer;
1018  template < typename _Ty0 >
1019  friend class WeakPointer;
1020 };
1021 
1023 template < typename _HTy >
1024 class WeakHandle
1025 {
1026 public:
1028  typedef _HTy HType;
1029 
1032 
1035 
1037  template < typename _HTy2 >
1039  {
1040  _reset( other._self.h, other._self.ctx );
1041  }
1042 
1044  template < typename _HTy2 >
1045  WeakHandle & operator = ( SharedHandle<_HTy2> const & other )
1046  {
1047  _reset( other._self.h, other._self.ctx );
1048  return *this;
1049  }
1050 
1053  {
1054  reset();
1055  }
1056 
1058  WeakHandle( WeakHandle const & other )
1059  {
1060  _reset(other);
1061  }
1062 
1064  WeakHandle & operator = ( WeakHandle const & other )
1065  {
1066  _reset(other);
1067  return *this;
1068  }
1069 
1071  template < typename _HTy2 >
1073  {
1074  _reset(other);
1075  }
1076 
1078  template < typename _HTy2 >
1079  WeakHandle & operator = ( WeakHandle<_HTy2> const & other )
1080  {
1081  _reset(other);
1082  return *this;
1083  }
1084 
1086  void reset()
1087  {
1088  _reset( static_cast<_HTy>(0), 0 );
1089  }
1090 
1093  {
1095  (void)this->_sharedReset(&r);
1096  return r;
1097  }
1098 
1102  bool expired() const { return ( !_self.ctx || _self.ctx->expired() ); }
1103 
1105  operator bool() const { return _self.ctx != 0; }
1106 
1107 protected:
1109  template < typename _HTy2 >
1110  bool _sharedReset( SharedHandle<_HTy2> * shared ) const
1111  {
1112  if ( _self.ctx && _self.ctx->_incRefNz() )
1113  {
1114  // 由于之前_incRefNz()已经增加引用计数,因此这里当作新资源看待
1115  shared->_reset0( _self.h, _self.ctx );
1116  return true;
1117  }
1118  return false;
1119  }
1120 
1122  template < typename _HTy2 >
1123  void _reset( _HTy2 otherH, SharedDeleterContext * otherCtx )
1124  {
1125  if ( otherCtx != 0 )
1126  otherCtx->incWRef();
1127  if ( _self.ctx != 0 )
1128  _self.ctx->decWRef();
1129  _self.h = otherH;
1130  _self.ctx = otherCtx;
1131  }
1132 
1134  template < typename _HTy2 >
1135  void _reset( WeakHandle<_HTy2> const & other )
1136  {
1137  _reset( other._self.h, other._self.ctx );
1138  }
1139 
1140  WeakHandleData _self;
1141 
1142 private:
1143  template < typename _HTy0 >
1144  friend class WeakHandle;
1145  template < typename _HTy0 >
1146  friend class SharedHandle;
1147 };
1148 
1150 template < typename _Ty >
1151 class WeakPointer : public WeakHandle<_Ty*>
1152 {
1153 public:
1157  typedef _Ty Type;
1158 
1161 
1163  template < typename _Ty2 >
1165  {
1166  this->_reset( other._self.h, other._self.ctx );
1167  }
1168 
1170  template < typename _Ty2 >
1171  WeakPointer & operator = ( SharedPointer<_Ty2> const & other )
1172  {
1173  this->_reset( other._self.h, other._self.ctx );
1174  return *this;
1175  }
1176 
1178  WeakPointer( WeakPointer const & other )
1179  {
1180  this->_reset(other);
1181  }
1182 
1184  WeakPointer & operator = ( WeakPointer const & other )
1185  {
1186  this->_reset(other);
1187  return *this;
1188  }
1189 
1191  template < typename _Ty2 >
1193  {
1194  this->_reset(other);
1195  }
1196 
1198  template < typename _Ty2 >
1199  WeakPointer & operator = ( WeakPointer<_Ty2> const & other )
1200  {
1201  this->_reset(other);
1202  return *this;
1203  }
1204 
1207  {
1209  (void)this->_sharedReset(&r);
1210  return r;
1211  }
1212 
1213 private:
1214  template < typename _Ty0 >
1215  friend class WeakPointer;
1216  template < typename _Ty0 >
1217  friend class SharedPointer;
1218 };
1219 
1220 
1221 template < typename _Ty >
1223 {
1224 public:
1226 
1228  { // return shared_ptr
1229  return SharedPointer<_Ty>(_weakPtr);
1230  }
1231 
1233  { // return shared_ptr
1234  return SharedPointer<const _Ty>(_weakPtr);
1235  }
1236 
1238  { // return weak_ptr
1239  return _weakPtr;
1240  }
1241 
1243  { // return weak_ptr
1244  return _weakPtr;
1245  }
1246 
1247 protected:
1248  constexpr EnableSharedFromThis() noexcept : _weakPtr()
1249  { // construct
1250  }
1251 
1252  EnableSharedFromThis( EnableSharedFromThis const & ) noexcept : _weakPtr()
1253  { // construct (must value-initialize _Wptr)
1254  }
1255 
1256  EnableSharedFromThis& operator = ( EnableSharedFromThis const & ) noexcept
1257  { // assign (must not change _Wptr)
1258  return (*this);
1259  }
1260 
1261 private:
1262  template < typename _Other, typename _Yty >
1263  friend void _EnableSharedFromThis1( SharedPointer<_Other> const & sharedThis, _Yty * ptr, std::true_type );
1264 
1265  mutable WeakPointer<_Ty> _weakPtr;
1266 };
1267 
1269 template < typename _Ty >
1270 inline SimplePointer<_Ty> MakeSimple( _Ty * newObj )
1271 {
1272  return SimplePointer<_Ty>(newObj);
1273 }
1274 
1276 template < typename _Ty, typename _Dt >
1277 inline SimplePointer<_Ty> MakeSimple( _Ty * newObj, _Dt dt )
1278 {
1279  return SimplePointer<_Ty>( newObj, dt );
1280 }
1281 
1283 template < typename _Ty >
1284 inline SharedPointer<_Ty> MakeShared( _Ty * newObj )
1285 {
1286  return SharedPointer<_Ty>(newObj);
1287 }
1288 
1290 template < typename _Ty, typename _Dt >
1291 inline SharedPointer<_Ty> MakeShared( _Ty * newObj, _Dt dt )
1292 {
1293  return SharedPointer<_Ty>( newObj, dt );
1294 }
1295 
1296 
1297 } // namespace winux
1298 
1299 #endif // __SMARTPTR_HPP__
XString< char > AnsiString
Definition: utilities.hpp:257
SimpleHandle< _Ty * > MyBase
基类SimpleHandle<_Ty*>
Definition: smartptr.hpp:306
void attachNew(_Ty *p)
附加新指针,管理新对象
Definition: smartptr.hpp:369
WeakPointer(WeakPointer const &other)
拷贝构造函数1
Definition: smartptr.hpp:1178
弱句柄
Definition: smartptr.hpp:452
void _EnableSharedFromThis(SharedPointer< _Other > const &sharedThis, _Yty *ptr)
Definition: smartptr.hpp:494
~SharedHandle()
析构函数
Definition: smartptr.hpp:657
SimpleHandleData _self
Definition: smartptr.hpp:293
SharedHandle(_HTy2 h, _HTy2 failVal)
构造函数2_1。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:636
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个SharedHandle的资源引用计数,减少自身计数。管理另一个SharedHandle的资源
Definition: smartptr.hpp:838
~SimpleHandle()
析构函数
Definition: smartptr.hpp:136
void _reset0(_HTy2 newH, SimpleDeleterContext *newCtx)
释放自身资源,管理新资源
Definition: smartptr.hpp:268
Shared自定义删除器场景
Definition: smartptr.hpp:581
void attach(_HTy2 h, SharedDeleterContext *ctx, bool isIncRef)
附加其他SharedHandle对象detach()出的句柄和删除器场景。_HTy2为可直接转为_HTy的类型 ...
Definition: smartptr.hpp:772
void release()
销毁资源和SimpleDeleterContext自身
Definition: smartptr.hpp:36
SimpleCustomDeleterContext(_HTy h, _Dt dt)
Definition: smartptr.hpp:72
SimpleHandle(SimpleHandle< _HTy2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:156
SharedPointer(_Ty2 *p, _Dt dt)
构造函数2_2
Definition: smartptr.hpp:897
SimplePointer(_Ty2 *p)
构造函数2_1
Definition: smartptr.hpp:329
SharedPointer(_Ty *p, _Dt dt)
构造函数1_2
Definition: smartptr.hpp:884
Shared删除器场景基类/Shared引用计数场景
Definition: smartptr.hpp:500
SharedPointer(_Ty *p)
构造函数1_1
Definition: smartptr.hpp:877
bool expired() const
资源是否已过期
Definition: smartptr.hpp:552
Simple默认删除器场景
Definition: smartptr.hpp:54
typename MakeVoid< _Ty... >::type VoidT
Definition: smartptr.hpp:462
void _reset0(_HTy2 newH, SharedDeleterContext *newCtx)
减少自身引用计数,管理新资源
Definition: smartptr.hpp:828
long useCount() const
资源引用计数
Definition: smartptr.hpp:549
~WeakHandle()
析构函数
Definition: smartptr.hpp:1052
SharedPointer< _Ty > lock() const
锁定共享引用计数场景,创建一个SharedPointer持住资源
Definition: smartptr.hpp:1206
#define WINUX_DLL
Definition: utilities.hpp:60
bool _sharedReset(SharedHandle< _HTy2 > *shared) const
调用SharedHandle::_reset0()。用于从WeakHandle创建SharedHandle
Definition: smartptr.hpp:1110
void attach(typename SimpleHandle< _HTy2 >::SimpleHandleData const &data)
附加其他SimpleHandle对象detach()出的句柄数据。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:218
Simple自定义删除器场景
Definition: smartptr.hpp:69
virtual void _destroy() override
销毁资源
Definition: smartptr.hpp:60
virtual void _destroy() override
销毁资源
Definition: smartptr.hpp:572
void _reset(SharedHandle< _HTy2 > const &other)
增加另一个SharedHandle的资源引用计数,减少自身计数。管理另一个SharedHandle的资源
Definition: smartptr.hpp:847
virtual void _deleteThis() override
删除引用计数数据场景
Definition: smartptr.hpp:588
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的类型
Definition: smartptr.hpp:644
简单句柄类,管理各种资源的自动释放,赋值相当于传递管理权。
Definition: smartptr.hpp:88
SharedPointer< const _Ty > sharedFromThis() const
Definition: smartptr.hpp:1232
void _reset(WeakHandle< _HTy2 > const &other)
增加另一个弱引用计数,减少自身弱计数。管理另一个WeakHandle
Definition: smartptr.hpp:1135
_Ty Type
指向的类型
Definition: smartptr.hpp:1157
SharedCustomDeleterContext(_HTy h, _Dt dt)
Definition: smartptr.hpp:584
void incRef()
增加引用计数
Definition: smartptr.hpp:526
SharedDeleterContext * ctx
共享删除器场景对象
Definition: smartptr.hpp:609
void attach(SimpleHandleData const &data)
附加其他SimpleHandle对象detach()出的句柄数据
Definition: smartptr.hpp:211
void attachNew(_Ty2 *p, _Dt dt)
附加新指针,管理新对象
Definition: smartptr.hpp:398
long LongAtomicDecrement(long volatile *p)
原子化使一个Long型变量-1,返回值是-1后的*p值
WeakHandle< _Ty * > MyBase
基类WeakHandle<_Ty*>
Definition: smartptr.hpp:1155
virtual void _deleteThis() override
销毁SimpleDeleterContext自身
Definition: smartptr.hpp:61
void attachNew(_HTy h, _HTy failVal)
附加新句柄,管理新资源
Definition: smartptr.hpp:694
void delThis()
显式删除SimpleHandle::detach()出来的SimpleDeleterContext对象
Definition: smartptr.hpp:43
virtual void _deleteThis() override
删除引用计数数据场景
Definition: smartptr.hpp:573
SimpleHandle(_HTy2 h, _HTy2 failVal)
构造函数2_1。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:125
void _reset(_HTy2 otherH, SharedDeleterContext *otherCtx)
增加另一个弱引用计数,减少自身弱计数。管理另一个WeakHandle
Definition: smartptr.hpp:1123
void attachNew(_Ty2 *p)
附加新指针,管理新对象
Definition: smartptr.hpp:972
Simple删除器场景基类
Definition: smartptr.hpp:28
WeakPointer(WeakPointer< _Ty2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:1192
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:726
_HTy HType
句柄类型
Definition: smartptr.hpp:1028
#define DISABLE_OBJECT_COPY(clsname)
Definition: utilities.hpp:85
void decRef()
减少引用计数。当引用计数为0时销毁资源,并且销毁资源时减少弱引用计数。
Definition: smartptr.hpp:528
WeakPointer< const _Ty > weakFromThis() const noexcept
Definition: smartptr.hpp:1242
SharedHandle()
构造函数0
Definition: smartptr.hpp:615
SharedDeleterContext * getContext() const
Definition: smartptr.hpp:807
void attach(typename SharedHandle< _HTy2 >::SharedHandleData const &data, bool isIncRef)
附加其他SharedHandle对象detach()出的句柄数据。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:754
SharedPointer()
构造函数0
Definition: smartptr.hpp:872
WeakPointer< _Ty > weakFromThis() noexcept
Definition: smartptr.hpp:1237
SimplePointer(_Ty *p)
构造函数1_1
Definition: smartptr.hpp:316
SharedHandleData _self
Definition: smartptr.hpp:852
SharedHandle(_HTy h, _HTy failVal)
构造函数1_1
Definition: smartptr.hpp:621
SharedHandle< _Ty * > MyBase
基类SharedHandle<_Ty*>
Definition: smartptr.hpp:867
_Ty Type
指向的类型
Definition: smartptr.hpp:308
_Ty Type
指向的类型
Definition: smartptr.hpp:869
SimpleHandle()
构造函数0
Definition: smartptr.hpp:104
virtual void _deleteThis() override
销毁SimpleDeleterContext自身
Definition: smartptr.hpp:76
bool _incRefNz()
如果引用计数不是0,则增加引用计数。成功则返回true。
Definition: smartptr.hpp:515
void attachNew(_Ty *p)
附加新指针,管理新对象
Definition: smartptr.hpp:951
void attachNew(_HTy h, _HTy failVal, _Dt dt)
附加新句柄,管理新资源
Definition: smartptr.hpp:705
SimpleHandle(_HTy h, _HTy failVal)
构造函数1_1
Definition: smartptr.hpp:110
SharedHandle(WeakHandle< _HTy2 > const &other)
构造函数3。从弱句柄构建共享句柄
Definition: smartptr.hpp:648
WeakHandle(WeakHandle< _HTy2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:1072
SimplePointer(SimplePointer< _Ty2 > const &other)
拷贝构造函数2
Definition: smartptr.hpp:353
SimpleHandle(_HTy2 h, _HTy2 failVal, _Dt dt)
构造函数2_2。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:133
智能指针错误
Definition: smartptr.hpp:10
virtual void _destroy() override
销毁资源
Definition: smartptr.hpp:75
SharedHandleData peek() const
窥视句柄数据
Definition: smartptr.hpp:794
引用计数共享指针
Definition: smartptr.hpp:456
_HTy HType
句柄类型
Definition: smartptr.hpp:92
void attachNew(_Ty *p, _Dt dt)
附加新指针,管理新对象
Definition: smartptr.hpp:962
_HTy HType
句柄类型
Definition: smartptr.hpp:603
void incWRef()
增加弱引用计数
Definition: smartptr.hpp:538
SimpleDeleterContext * ctx
简单删除器场景对象
Definition: smartptr.hpp:98
SharedPointer(SharedPointer const &other)
拷贝构造函数1
Definition: smartptr.hpp:921
SimplePointer()
构造函数0
Definition: smartptr.hpp:311
virtual void _destroy() override
销毁资源
Definition: smartptr.hpp:587
SharedPointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:994
WeakHandle(WeakHandle const &other)
拷贝构造函数1
Definition: smartptr.hpp:1058
void _reset(SimpleHandle< _HTy2 > const &other)
释放自身资源,接管另一个SimpleHandle的资源,另一个SimpleHandle置零
Definition: smartptr.hpp:287
long LongAtomicIncrement(long volatile *p)
原子化使一个Long型变量+1,返回值是+1后的*p值
void attach(SharedHandleData const &data, bool isIncRef)
附加其他SharedHandle对象detach()出的句柄数据
Definition: smartptr.hpp:736
SharedHandle< _HTy >::SharedHandleData WeakHandleData
句柄数据结构体
Definition: smartptr.hpp:1031
Shared默认删除器场景
Definition: smartptr.hpp:566
SimplePointer(_Ty2 *p, _Dt dt)
构造函数2_2
Definition: smartptr.hpp:336
SimpleDeleterContext * getContext() const
Definition: smartptr.hpp:247
WeakHandle()
构造函数0
Definition: smartptr.hpp:1034
SimplePointer(_Ty *p, _Dt dt)
构造函数1_2
Definition: smartptr.hpp:323
SharedHandle(SharedHandle< _HTy2 > const &other)
拷贝构造函数2。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:677
线程
Definition: threads.hpp:149
SimpleHandle(_HTy h, _HTy failVal, _Dt dt)
构造函数1_2
Definition: smartptr.hpp:118
SimpleHandle(SimpleHandle const &other)
拷贝构造函数1
Definition: smartptr.hpp:142
SharedHandleData detach()
资源脱离管理,返回句柄数据
Definition: smartptr.hpp:785
void decWRef()
减少弱引用计数,当弱引用计数为0时销毁删除器场景对象
Definition: smartptr.hpp:540
SharedPointer(_Ty2 *p)
构造函数2_1
Definition: smartptr.hpp:890
void _EnableSharedFromThis1(SharedPointer< _Other > const &sharedThis, _Yty *ptr, std::true_type)
Definition: smartptr.hpp:478
SmartPtrError(SmartPtrErrorType err, AnsiString const &errStr)
Definition: smartptr.hpp:24
void attachNew(_HTy2 h, _HTy2 failVal, _Dt dt)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:205
#define WINUX_FUNC_DECL(ret)
Definition: utilities.hpp:64
WeakPointer(SharedPointer< _Ty2 > const &other)
构造函数1,从一个SharedPointer构建一个WeakPointer
Definition: smartptr.hpp:1164
SimplePointer< _Ty2 > cast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:408
void reset()
释放资源并重置
Definition: smartptr.hpp:240
bool expired() const
是否过期
Definition: smartptr.hpp:1102
SimplePointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:427
SharedPointer(SharedPointer< _Ty2 > const &other)
拷贝构造函数2
Definition: smartptr.hpp:935
void reset()
释放并重置
Definition: smartptr.hpp:1086
SharedHandle(_HTy h, _HTy failVal, _Dt dt)
构造函数1_2
Definition: smartptr.hpp:629
SharedHandleData(_HTy h, SharedDeleterContext *ctx)
Definition: smartptr.hpp:611
void _reset(_HTy2 &otherH, SimpleDeleterContext *&otherCtx)
释放自身资源,接管另一个SimpleHandle的资源,另一个SimpleHandle置零
Definition: smartptr.hpp:278
SharedPointer(SharedPointer< _Ty2 > const &other, _Ty2 *ptr) noexcept
构造函数4。别名构造
Definition: smartptr.hpp:911
long weakCount() const
弱引用计数
Definition: smartptr.hpp:555
SharedHandle(SharedHandle const &other)
拷贝构造函数1
Definition: smartptr.hpp:663
WeakPointer()
构造函数0
Definition: smartptr.hpp:1160
简单指针
Definition: smartptr.hpp:302
WeakHandle(SharedHandle< _HTy2 > const &other)
构造函数1,从一个SharedHandle构建一个WeakHandle
Definition: smartptr.hpp:1038
SimpleHandleData(_HTy h, SimpleDeleterContext *ctx)
Definition: smartptr.hpp:100
错误类
Definition: utilities.hpp:838
void attachNew(_HTy h, _HTy failVal)
附加新句柄,管理新资源
Definition: smartptr.hpp:173
SimplePointer< _Ty > MakeSimple(_Ty *newObj)
创建一个SimplePointer来管理新对象资源
Definition: smartptr.hpp:1270
void attach(_HTy2 h, SimpleDeleterContext *ctx)
附加其他SimpleHandle对象detach()出的句柄和删除器场景。_HTy2为可直接转为_HTy的类型 ...
Definition: smartptr.hpp:225
void attachNew(_Ty *p, _Dt dt)
附加新指针,管理新对象
Definition: smartptr.hpp:379
void attachNew(_Ty2 *p)
附加新指针,管理新对象
Definition: smartptr.hpp:388
void attachNew(_HTy h, _HTy failVal, _Dt dt)
附加新句柄,管理新资源
Definition: smartptr.hpp:184
SharedPointer< _Ty > MakeShared(_Ty *newObj)
创建一个SharedPointer来管理新对象资源
Definition: smartptr.hpp:1284
SimplePointer(SimplePointer const &other)
拷贝构造函数1
Definition: smartptr.hpp:339
SharedHandle< _HTy > lock() const
锁定共享引用计数场景,创建一个SharedHandle持住资源
Definition: smartptr.hpp:1092
EnableSharedFromThis(EnableSharedFromThis const &) noexcept
Definition: smartptr.hpp:1252
SharedPointer< _Ty > sharedFromThis()
Definition: smartptr.hpp:1227
SharedPointer(WeakPointer< _Ty2 > const &other)
构造函数3。从弱指针构建共享指针
Definition: smartptr.hpp:901
WeakHandleData _self
Definition: smartptr.hpp:1140
void reset()
释放资源并重置
Definition: smartptr.hpp:800
void attachNew(_HTy2 h, _HTy2 failVal)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:715
SimpleHandleData detach()
资源脱离管理,返回句柄数据
Definition: smartptr.hpp:231
SharedPointer< _Ty2 > ensureCast()
把指针由_Ty转换成_Ty2类型
Definition: smartptr.hpp:1008
constexpr EnableSharedFromThis() noexcept
Definition: smartptr.hpp:1248
void attachNew(_Ty2 *p, _Dt dt)
附加新指针,管理新对象
Definition: smartptr.hpp:983
跨平台基础功能库
Definition: archives.hpp:7
void attachNew(_HTy2 h, _HTy2 failVal)
附加新句柄,管理新资源。_HTy2为可直接转为_HTy的类型
Definition: smartptr.hpp:194
引用计数共享句柄,管理各种资源的自动释放,赋值则引用计数加一。
Definition: smartptr.hpp:599