2
* Copyright 2010 Inalogic® Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License, as
6
* published by the Free Software Foundation; either version 2.1 or 3.0
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranties of
11
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
* PURPOSE. See the applicable version of the GNU Lesser General Public
13
* License for more details.
15
* You should have received a copy of both the GNU Lesser General Public
16
* License along with this program. If not, see <http://www.gnu.org/licenses/>
18
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
23
#ifndef WIDGETSMARTPOINTER_H
24
#define WIDGETSMARTPOINTER_H
31
RefCounts () : totalRefs_ (1), strongRefs_ (1)
35
NThreadSafeCounter totalRefs_;
36
NThreadSafeCounter strongRefs_;
44
///////////////////////////////////////////////////////
45
// forward definitions
53
//! A smart pointer class. Implemented as an intrusive smart pointer.
62
, deletewhenrefcounthitzero_ (true)
67
GenericSP (const GenericSP<T>& other)
70
, deletewhenrefcounthitzero_ (true)
73
refCounts_ = other.refCounts_;
74
deletewhenrefcounthitzero_ = other.deletewhenrefcounthitzero_;
78
refCounts_->strongRefs_.Increment();
79
refCounts_->totalRefs_.Increment();
85
@param other Parameter with a type derived from T.
88
GenericSP (const GenericSP<O>& other)
91
, deletewhenrefcounthitzero_ (true)
93
if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
95
ptr_ = (T *) other.ptr_;
96
refCounts_ = other.refCounts_;
97
deletewhenrefcounthitzero_ = other.deletewhenrefcounthitzero_;
102
refCounts_->strongRefs_.Increment();
103
refCounts_->totalRefs_.Increment();
107
//! Construction with a base pointer of type T.
109
@param ptr Start maintaining a reference count of the passed pointer.
110
@param deletewhenrefcounthitzero if True, the pointer is deleted when the ref count reach 0 (to be deprecated).
112
explicit GenericSP (T *ptr, bool deletewhenrefcounthitzero = true)
115
, deletewhenrefcounthitzero_ (true)
120
refCounts_ = new RefCounts;
121
deletewhenrefcounthitzero_ = deletewhenrefcounthitzero;;
125
//! Construction with a base pointer of type O that inherits from type T.
127
@param ptr Start maintaining a reference count of the passed pointer.
128
@param deletewhenrefcounthitzero if True, the pointer is deleted when the ref count reach 0 (to be deprecated).
130
template <typename O>
131
explicit GenericSP (O *ptr, bool deletewhenrefcounthitzero = true)
134
, deletewhenrefcounthitzero_ (true)
138
if (ptr->Type().IsDerivedFromType (T::StaticObjectType) )
141
refCounts_ = new RefCounts;
142
deletewhenrefcounthitzero_ = deletewhenrefcounthitzero;
146
// This is possible but check why!
147
// It can happen when doing something like this:
148
// Layout* layout = this;
149
// where this is a Baseobject.
155
//! Assignment of a smart pointer of type T.
157
@param other Smart pointer of type T.
159
GenericSP &operator = (const GenericSP<T>& other)
161
if (ptr_ != other.ptr_)
166
refCounts_ = other.refCounts_;
167
deletewhenrefcounthitzero_ = other.deletewhenrefcounthitzero_;
171
refCounts_->strongRefs_.Increment();
172
refCounts_->totalRefs_.Increment();
179
//! Assignment of a smart pointer of type O that inherits from type T.
181
@param other Smart pointer of type O.
183
template <typename O>
184
GenericSP &operator = (const GenericSP<O>& other)
186
if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
188
if (ptr_ != other.ptr_)
193
refCounts_ = other.refCounts_;
194
deletewhenrefcounthitzero_ = other.deletewhenrefcounthitzero_;
198
refCounts_->strongRefs_.Increment();
199
refCounts_->totalRefs_.Increment();
216
T &operator * () const
218
nuxAssert (ptr_ != 0);
222
T *operator -> () const
224
nuxAssert (ptr_ != 0);
228
//! Swap the content of 2 smart pointers.
230
@param other Smart pointer to swap with.
232
void Swap (GenericSP<T>& other)
234
std::swap (ptr_, other.ptr_);
235
std::swap (refCounts_, other.refCounts_);
238
//! Test validity of the smart pointer.
240
Return True if the internal pointer is not null.
242
bool operator () () const
247
//! Test validity of the smart pointer.
249
Return True if the internal pointer is null.
259
//! Test validity of the smart pointer.
261
Return True if the internal pointer is not null.
271
bool operator < (T *ptr) const
276
bool operator > (T *ptr) const
281
bool operator < (GenericSP<T> other) const
283
return (ptr_ < other.ptr_);
286
bool operator > (GenericSP<T> other) const
288
return (ptr_ > other.ptr_);
291
bool operator != (T *ptr) const
293
return (ptr_ != ptr);
296
bool operator == (T *ptr) const
298
return (ptr_ == ptr);
302
bool operator != (U *ptr) const
304
if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) )
307
return (ptr_ != ptr);
311
bool operator == (U *ptr) const
313
if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) )
316
return (ptr_ == ptr);
320
bool operator != (const GenericSP<U>& other) const
322
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
325
return ptr_ != other.ptr_;
329
bool operator == (const GenericSP<U>& other) const
331
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
334
return ptr_ == other.ptr_;
338
bool operator != (const GenericWeakSP<U>& other) const
340
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
343
return ptr_ != other.ptr_;
347
bool operator == (const GenericWeakSP<U>& other) const
349
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
352
return ptr_ == other.ptr_;
361
GenericSP (T *ptr, RefCounts *refCounts) : ptr_ (ptr), refCounts_ (refCounts)
365
void ReleaseReference()
373
refCounts_->strongRefs_.Decrement();
374
bool release = refCounts_->strongRefs_ == 0;
376
if (release && deletewhenrefcounthitzero_)
385
refCounts_->totalRefs_.Decrement();
386
bool release = refCounts_->totalRefs_ == 0;
398
RefCounts *refCounts_;
399
bool deletewhenrefcounthitzero_;
401
template <typename U>
402
friend GenericSP<U> Create ();
404
template <typename U, typename P1>
405
friend GenericSP<U> Create (P1 p1);
407
template <typename U, typename P1, typename P2>
408
friend GenericSP<U> Create (P1 p1, P2 p2);
410
template <typename U, typename P1, typename P2, typename P3>
411
friend GenericSP<U> Create (P1 p1, P2 p2, P3 p3);
413
template <typename U, typename P1, typename P2, typename P3, typename P4>
414
friend GenericSP<U> Create (P1 p1, P2 p2, P3 p3, P4 p4);
416
template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5>
417
friend GenericSP<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
419
template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
420
friend GenericSP<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6);
422
template <typename U, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
423
friend GenericSP<U> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7);
425
template <typename U>
426
friend GenericSP<U> WrapWSPtr (U *u);
428
template <typename O>
429
friend class GenericSP;
431
template <typename O>
432
friend class GenericWeakSP;
434
// template<typename T, typename U>
435
// friend bool operator == (const GenericSP<T>& a, const GenericSP<U>& b);
437
// template<typename T, typename U>
438
// friend bool operator != (const GenericSP<T>& a, const GenericSP<U>& b);
440
// template<typename T>
441
// friend bool operator == (const GenericSP<T>& a, T*);
444
friend bool operator == (U *, const GenericSP<U>& a);
446
// template<typename T>
447
// friend bool operator != (const GenericSP<T>& a, T*);
450
friend bool operator != (U *, const GenericSP<U>& a);
452
// template<typename T, typename U>
453
// friend bool operator == (const GenericSP<T>& a, const GenericWeakSP<U>& b);
455
// template<typename T, typename U>
456
// friend bool operator == (const GenericWeakSP<T>& a, const GenericSP<U>& b);
458
// template<typename T, typename U>
459
// friend bool operator != (const GenericSP<T>& a, const GenericWeakSP<U>& b);
461
// template<typename T, typename U>
462
// friend bool operator != (const GenericWeakSP<T>& a, const GenericSP<U>& b);
464
template <typename U, typename F>
465
friend GenericSP<U> staticCast (const GenericSP<F>& from);
467
template <typename U, typename F>
468
friend GenericSP<U> constCast (const GenericSP<F>& from);
470
template <typename U, typename F>
471
friend GenericSP<U> dynamicCast (const GenericSP<F>& from);
473
template <typename U, typename F>
474
friend GenericSP<U> checkedCast (const GenericSP<F>& from);
476
template <typename U, typename F>
477
friend GenericSP<U> queryCast (const GenericSP<F>& from);
481
//! A weak smart pointer class. Implemented as an intrusive smart pointer.
483
A weak smart pointer is built from a smart pointer or another weak smart pointer. It increments and decrements
484
the total reference count of an pointer. Even is the original pointer is destroyed, weak smart pointers still point
485
to the RefCounts pointers of the original pointer and can use it to check if the pointer is still valid or not.
487
template <typename T>
492
GenericWeakSP () : ptr_ (0), refCounts_ (0)
498
@param other Parameter with type T.
500
GenericWeakSP (const GenericWeakSP<T>& other)
503
refCounts_ = other.refCounts_;
507
refCounts_->totalRefs_.Increment();
513
@param other Parameter with a type derived from T.
515
template <typename O>
516
GenericWeakSP (const GenericWeakSP<O>& other)
520
if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
523
refCounts_ = other.refCounts_;
527
refCounts_->totalRefs_.Increment();
532
// //! Construction from a smart pointer of type T.
534
// @param other Maintains a weak smart pointer reference to this parameter.
536
// GenericWeakSP (const GenericSP<T>& other)
542
// ptr_ = other.ptr_;
543
// refCounts_ = other.refCounts_;
547
// refCounts_->totalRefs_.Increment();
552
//! Construction from a smart pointer of type O that inherits from type T.
554
@param other Maintains a weak smart pointer reference to this parameter.
556
template <typename O>
557
GenericWeakSP (const GenericSP<O>& other)
561
if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
564
refCounts_ = other.refCounts_;
568
refCounts_->totalRefs_.Increment();
573
//! Assignment of a weak smart pointer of type T.
575
@param other Weak smart pointer of type T.
577
GenericWeakSP &operator = (const GenericWeakSP<T>& other)
579
if (get () != other.get () )
584
refCounts_ = other.refCounts_;
588
refCounts_->totalRefs_.Increment();
595
//! Assignment of a weak smart pointer of Type O that inherits from type T.
597
@param other Weak smart pointer of type O.
599
template <typename O>
600
GenericWeakSP &operator = (const GenericWeakSP<O>& other)
602
if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
604
if (get () != other.get () )
609
refCounts_ = other.refCounts_;
613
refCounts_->totalRefs_.Increment();
625
//! Assignment of a smart pointer of Type O that inherits from type T.
627
@param other Maintains a weak smart pointer reference to this parameter.
629
template <typename O>
630
GenericWeakSP &operator = (const GenericSP<O>& other)
632
if (other.ptr_ && other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) )
634
if (get () != other.ptr_)
639
refCounts_ = other.refCounts_;
643
refCounts_->totalRefs_.Increment();
660
T &operator * () const
662
nuxAssert ( (refCounts_->strongRefs_.GetValue() != 0) && (ptr_ != 0) );
667
T *operator -> () const
669
nuxAssert ( (refCounts_->strongRefs_.GetValue() != 0) && (ptr_ != 0) );
674
void Swap (GenericWeakSP<T>& other)
676
std::swap (ptr_, other.ptr_);
677
std::swap (refCounts_, other.refCounts_);
680
bool operator < (T *ptr) const
685
bool operator > (T *ptr) const
690
bool operator < (GenericWeakSP<T> other) const
692
return (ptr_ < other.ptr_);
695
bool operator > (GenericWeakSP<T> other) const
697
return (ptr_ > other.ptr_);
700
bool operator != (T *ptr) const
702
return (ptr_ != ptr);
705
bool operator == (T *ptr) const
707
return (ptr_ == ptr);
711
bool operator != (U *ptr) const
713
if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) )
716
return (ptr_ != ptr);
720
bool operator == (U *ptr) const
722
if (ptr && (!ptr->Type().IsDerivedFromType (T::StaticObjectType) ) )
725
return (ptr_ == ptr);
729
bool operator != (const GenericWeakSP<U>& other) const
731
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
734
return ptr_ != other.ptr_;
738
bool operator == (const GenericWeakSP<U>& other) const
740
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
743
return ptr_ == other.ptr_;
747
bool operator != (const GenericSP<U>& other) const
749
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
752
return ptr_ != other.ptr_;
756
bool operator == (const GenericSP<U>& other) const
758
if (other.ptr_ && (!other.ptr_->Type().IsDerivedFromType (T::StaticObjectType) ) )
761
return ptr_ == other.ptr_;
769
bool IsValid () const
780
GenericWeakSP (T *ptr, RefCounts *refCounts) : ptr_ (ptr), refCounts_ (refCounts)
786
if (refCounts_ == 0 || (refCounts_->strongRefs_ == 0) )
794
// bool isNull () const
796
// return get () == 0;
799
void ReleaseReference ()
806
refCounts_->totalRefs_.Decrement();
807
bool release = refCounts_->totalRefs_ == 0;
819
RefCounts *refCounts_;
821
template <typename O>
822
friend class GenericWeakSP;
824
template <typename O>
825
friend class SmartPtr;
827
// template<typename T, typename U>
828
// friend bool operator == (const GenericWeakSP<T>& a, const GenericWeakSP<U>& b);
830
// template<typename T, typename U>
831
// friend bool operator != (const GenericWeakSP<T>& a, const GenericWeakSP<U>& b);
833
// template<typename T>
834
// friend bool operator == (const GenericWeakSP<T>& a, T*);
837
friend bool operator == (U *, const GenericWeakSP<U>& a);
839
// template<typename T>
840
// friend bool operator != (const GenericWeakSP<T>& a, T*);
843
friend bool operator != (U *, const GenericWeakSP<U>& a);
845
// template<typename T, typename U>
846
// friend bool operator == (const GenericSP<T>& a, const GenericWeakSP<U>& b);
848
// template<typename T, typename U>
849
// friend bool operator == (const GenericWeakSP<T>& a, const GenericSP<U>& b);
851
// template<typename T, typename U>
852
// friend bool operator != (const GenericSP<T>& a, const GenericWeakSP<U>& b);
854
// template<typename T, typename U>
855
// friend bool operator != (const GenericWeakSP<T>& a, const GenericSP<U>& b);
857
template <typename U, typename F>
858
friend GenericWeakSP<U> staticCast (const GenericWeakSP<F>& from);
860
template <typename U, typename F>
861
friend GenericWeakSP<U> constCast (const GenericWeakSP<F>& from);
863
template <typename U, typename F>
864
friend GenericWeakSP<U> dynamicCast (const GenericWeakSP<F>& from);
866
template <typename U, typename F>
867
friend GenericWeakSP<U> checkedCast (const GenericWeakSP<F>& from);
869
template <typename U, typename F>
870
friend GenericWeakSP<U> queryCast (const GenericWeakSP<F>& from);
873
///////////////////////////////////////////////////////
876
// template<typename T, typename U>
877
// inline bool operator == (const GenericSP<T>& a, const GenericSP<U>& b)
879
// return a.ptr_ == b.ptr_;
882
// template<typename T, typename U>
883
// inline bool operator == (const GenericWeakSP<T>& a, const GenericWeakSP<U>& b)
885
// return a.get () == b.get ();
888
// template<typename T, typename U>
889
// inline bool operator == (const GenericSP<T>& a, const GenericWeakSP<U>& b)
891
// return a.ptr_ == b.get ();
894
// template<typename T, typename U>
895
// inline bool operator == (const GenericWeakSP<T>& a, const GenericSP<U>& b)
897
// return a.get () == b.ptr_;
900
// template<typename T, typename U>
901
// inline bool operator != (const GenericSP<T>& a, const GenericSP<U>& b)
903
// return a.ptr_ != b.ptr_;
906
// template<typename T, typename U>
907
// inline bool operator != (const GenericWeakSP<T>& a, const GenericWeakSP<U>& b)
909
// return a.get () != b.get ();
912
// template<typename T, typename U>
913
// inline bool operator != (const GenericSP<T>& a, const GenericWeakSP<U>& b)
915
// return a.ptr_ != b.get ();
918
// template<typename T, typename U>
919
// inline bool operator != (const GenericWeakSP<T>& a, const GenericSP<U>& b)
921
// return a.get () != b.ptr_;
924
// template<typename T>
925
// inline bool operator == (const GenericSP<T>& a, T* ptr)
927
// return a.ptr_ == ptr;
931
inline bool operator == (T *ptr, const GenericSP<T>& a)
933
return a.ptr_ == ptr;
936
// template<typename T>
937
// inline bool operator != (const GenericSP<T>& a, T* ptr)
939
// return a.ptr_ != ptr;
943
inline bool operator != (T *ptr, const GenericSP<T>& a)
945
return a.ptr_ != ptr;
948
// template<typename T>
949
// inline bool operator == (const GenericWeakSP<T>& a, T* ptr)
951
// return a.ptr_ == ptr;
955
inline bool operator == (T *ptr, const GenericWeakSP<T>& a)
957
return a.ptr_ == ptr;
960
// template<typename T>
961
// inline bool operator != (const GenericWeakSP<T>& a, T* ptr)
963
// return a.ptr_ != ptr;
967
inline bool operator != (T *ptr, const GenericWeakSP<T>& a)
969
return a.ptr_ != ptr;
972
///////////////////////////////////////////////////////
973
// creation functions
975
template <typename T>
976
GenericSP<T> Create ()
978
RefCounts *rc = new RefCounts;
983
return GenericSP<T> (t, rc);
992
template <typename T, typename P1>
993
GenericSP<T> Create (P1 p1)
995
RefCounts *rc = new RefCounts;
1000
return GenericSP<T> (t, rc);
1009
template <typename T, typename P1, typename P2>
1010
GenericSP<T> Create (P1 p1, P2 p2)
1012
RefCounts *rc = new RefCounts;
1016
T *t = new T (p1, p2);
1017
return GenericSP<T> (t, rc);
1026
template <typename T, typename P1, typename P2, typename P3>
1027
GenericSP<T> Create (P1 p1, P2 p2, P3 p3)
1029
RefCounts *rc = new RefCounts;
1033
T *t = new T (p1, p2, p3);
1034
return GenericSP<T> (t, rc);
1043
template <typename T, typename P1, typename P2, typename P3, typename P4>
1044
GenericSP<T> Create (P1 p1, P2 p2, P3 p3, P4 p4)
1046
RefCounts *rc = new RefCounts;
1050
T *t = new T (p1, p2, p3, p4);
1051
return GenericSP<T> (t, rc);
1060
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5>
1061
GenericSP<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
1063
RefCounts *rc = new RefCounts;
1067
T *t = new T (p1, p2, p3, p4, p5);
1068
return GenericSP<T> (t, rc);
1077
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
1078
GenericSP<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
1080
RefCounts *rc = new RefCounts;
1084
T *t = new T (p1, p2, p3, p4, p5, p6);
1085
return GenericSP<T> (t, rc);
1094
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
1095
GenericSP<T> Create (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7)
1097
RefCounts *rc = new RefCounts;
1101
T *t = new T (p1, p2, p3, p4, p5, p6, p7);
1102
return GenericSP<T> (t, rc);
1111
template <typename T>
1112
GenericSP<T> WrapWSPtr (T *t)
1116
return GenericSP<T> ();
1121
RefCounts *rc = new RefCounts;
1123
return GenericSP<T> (t, rc);
1132
///////////////////////////////////////////////////////
1135
template <typename U, typename F>
1136
GenericSP<U> staticCast (const GenericSP<F>& from)
1140
return GenericSP<U>();
1143
U *ptr = static_cast <U *> (from.ptr_);
1144
RefCounts *refCounts = from.refCounts_;
1148
refCounts->strongRefs_.Increment();
1149
refCounts->totalRefs_.Increment();
1152
return GenericSP<U> (ptr, refCounts);
1155
template <typename T, typename F>
1156
GenericSP<T> constCast (const GenericSP<F>& from)
1160
return GenericSP<T>();
1163
T *ptr = const_cast <T *> (from.ptr_);
1164
RefCounts *refCounts = from.refCounts_;
1168
refCounts->strongRefs_.Increment();
1169
refCounts->totalRefs_.Increment();
1172
return GenericSP<T> (ptr, refCounts);
1175
template <typename T, typename F>
1176
GenericSP<T> dynamicCast (const GenericSP<F>& from)
1180
return GenericSP<T>();
1183
T *ptr = &dynamic_cast <T &> (*from.ptr_);
1184
RefCounts *refCounts = from.refCounts_;
1188
refCounts->strongRefs_.Increment();
1189
refCounts->totalRefs_.Increment();
1192
return GenericSP<T> (ptr, refCounts);
1195
template <typename T, typename F>
1196
GenericSP<T> queryCast (const GenericSP<F>& from)
1198
T *ptr = dynamic_cast <T *> (from.ptr_);
1202
return GenericSP<T>();
1205
RefCounts *refCounts = from.refCounts_;
1209
refCounts->strongRefs_.Increment();
1210
refCounts->totalRefs_.Increment();
1213
return GenericSP<T> (ptr, refCounts);
1216
template <typename T, typename F>
1217
GenericSP<T> checkedCast (const GenericSP<F>& from)
1221
return GenericSP<T>();
1224
nuxAssert (dynamic_cast<T *> (from.ptr_) != 0);
1226
T *ptr = static_cast <T *> (from.ptr_);
1227
RefCounts *refCounts = from.refCounts_;
1231
refCounts->strongRefs_.Increment();
1232
refCounts->totalRefs_.Increment();
1235
return GenericSP<T> (ptr, refCounts);
1238
template <typename U, typename F>
1239
GenericWeakSP<U> staticCast (const GenericWeakSP<F>& from)
1241
if (from.get () == 0)
1243
return GenericWeakSP<U>();
1246
U *ptr = static_cast <U *> (from.ptr_);
1247
RefCounts *refCounts = from.refCounts_;
1251
refCounts->totalRefs_.Increment();
1254
return GenericWeakSP<U> (ptr, refCounts);
1257
template <typename T, typename F>
1258
GenericWeakSP<T> constCast (const GenericWeakSP<F>& from)
1260
if (from.get () == 0)
1262
return GenericWeakSP<T>();
1265
T *ptr = const_cast <T *> (from.ptr_);
1266
RefCounts *refCounts = from.refCounts_;
1270
refCounts->totalRefs_.Increment();
1273
return GenericWeakSP<T> (ptr, refCounts);
1276
template <typename T, typename F>
1277
GenericWeakSP<T> dynamicCast (const GenericWeakSP<F>& from)
1279
if (from.get () == 0)
1281
return GenericWeakSP<T>();
1284
T *ptr = &dynamic_cast <T &> (*from.ptr_);
1285
RefCounts *refCounts = from.refCounts_;
1289
refCounts->totalRefs_.Increment();
1292
return GenericWeakSP<T> (ptr, refCounts);
1295
template <typename T, typename F>
1296
GenericWeakSP<T> queryCast (const GenericWeakSP<F>& from)
1298
T *ptr = dynamic_cast <T *> (from.get () );
1302
return GenericWeakSP<T>();
1305
RefCounts *refCounts = from.refCounts_;
1309
refCounts->totalRefs_.Increment();
1312
return GenericWeakSP<T> (ptr, refCounts);
1315
template <typename T, typename F>
1316
GenericWeakSP<T> checkedCast (const GenericWeakSP<F>& from)
1318
if (from.get () == 0)
1320
return GenericWeakSP<T>();
1323
nuxAssert (dynamic_cast<T *> (from.ptr_) != 0);
1325
T *ptr = static_cast <T *> (from.ptr_);
1326
RefCounts *refCounts = from.refCounts_;
1330
refCounts->totalRefs_.Increment();
1333
return GenericWeakSP<T> (ptr, refCounts);
1336
///////////////////////////////////////////////////////
1337
// std specializations
1339
template <typename T>
1340
inline void swap (GenericSP<T>& t1, GenericSP<T>& t2)
1345
template <typename T>
1346
inline void swap (GenericWeakSP<T>& t1, GenericWeakSP<T>& t2)
1353
#endif // WIDGETSMARTPOINTER_H