CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
smart_ptr.h
1
2#ifndef _aspose_system_smart_ptr_h_
3#define _aspose_system_smart_ptr_h_
4
5
6#include "smart_ptr_counter.h"
7#include "detail.h"
8#include "select_type.h"
9
10#include <system/details/makeobject_leakage_detector.h>
11
12#include <utility>
13#include <system/diagnostics/debug.h>
14#include <type_traits>
15#include <ostream>
16
17
18namespace System {
19
20class TypeInfo;
21class SmartPtrInfo;
22template <typename T> class ASPOSECPP_SHARED_CLASS Array;
23template <typename T> class SmartPtr;
24
25#if defined(__DBG_TOOLS)
26namespace DBG {
27 struct for_each_member_visitor;
28}
29#endif
30
31namespace Details {
32 class ObjectsBag;
33
36 template <typename T> struct ArrayTypeResolver
37 {
39 using type = void;
41 using value_type = void;
42 };
45 template <typename T> struct ArrayTypeResolver<Array<T>>
46 {
48 using type = Array<T>;
50 using value_type = T;
51 };
52
59 template <typename X, typename IdxType> typename Array<X>::UnderlyingType& GetByIndex(const SmartPtr<Array<X>> *ptr, IdxType idx);
63 template <typename T> Array<T>* CreateArray(Array<T>*);
64}
67enum class SmartPtrMode : char
68{
70 Shared,
72 Weak
73};
74
76struct EmptyArrayInitializer {};
78
178template <class T>
179class ASPOSECPP_SHARED_CLASS SmartPtr
180{
181public:
183 typedef T Pointee_;
187 typedef typename System::Details::ArrayTypeResolver<T>::type ArrayType;
189 using ValueType = typename System::Details::SelectType<typename System::Details::ArrayTypeResolver<T>::value_type>::type;
193 : m_data(mode)
194 {
195 m_data.SetNull();
196 }
199 SmartPtr(std::nullptr_t = nullptr, SmartPtrMode mode = SmartPtrMode::Shared)
200 : m_data(mode)
201 {
202 m_data.SetNull();
203 }
208 : m_data(mode)
209 {
210 Lock(object);
211 }
216 : m_data(mode)
217 {
218 Lock(ptr);
219 }
224 template<class Q, typename = typename std::enable_if<System::detail::is_pointer_convertible<Q, Pointee_>::type::value>::type>
226 : m_data(mode)
227 {
228 Lock(x);
229 }
234 : m_data(mode)
235 {
236 if (m_data.GetMode() == x.m_data.GetMode())
237 {
238 m_data.SetNull();
239 m_data.swapPointers(x.m_data);
240 }
241 else
242 {
243 if (m_data.GetMode() == SmartPtrMode::Shared)
244 MoveSharedFromWeak(std::move(x));
245 else
246 MoveWeakFromShared(std::move(x));
247 }
248 }
253 template <typename Y>
255 : m_data(mode)
256 {
257 InitArray(this, src);
258 }
261 template <typename Y, typename = typename std::enable_if<std::is_same<Y, EmptyArrayInitializer>::value, void>::type>
262 explicit SmartPtr(const Y &)
263 {
264 Lock(Details::CreateArray((Pointee_*)nullptr));
265 }
326 template <typename P>
328 : m_data(mode)
329 {
330 if (ptr == nullptr || p == nullptr)
331 m_data.SetNull();
332 else if (m_data.GetMode() == SmartPtrMode::Weak)
333 {
334 if (ptr.m_data.GetMode() == SmartPtrMode::Weak)
335 m_data.WeakSetPointer(
336 p
338 , ptr.m_data.WeakGetCounter()->WeakRefAdded()
339 );
340 else
341 m_data.WeakSetPointer(
342 p
344#ifdef ENABLE_EXTERNAL_REFCOUNT
345 , ptr.m_data.SharedGetCounter()->WeakRefAdded()
346#else
348#endif
349 );
350 }
351 else
352 {
353 if (ptr.m_data.GetMode() == SmartPtrMode::Weak)
354 m_data.SharedSetPointer(
355 p
357 , ptr.m_data.WeakGetCounter()->Lock()
358#ifdef ENABLE_EXTERNAL_REFCOUNT
359 , ptr.m_data.WeakGetCounter()
360#endif
361 );
362 else
363 m_data.SharedSetPointer(
364 p
367#ifdef ENABLE_EXTERNAL_REFCOUNT
368 , ptr.m_data.SharedGetCounter()
369#endif
370 );
371 }
372 }
375 {
376 delete ReleaseAndGetObjectToDelete();
377 }
381 SmartPtr_& operator = (SmartPtr_&& x) noexcept
382 {
383 Pointee_ *const this_pointee = m_data.GetPointee();
384 Pointee_ *const other_pointee = x.m_data.GetPointee();
385
386 if (this_pointee == nullptr)
387 {
388 if (other_pointee == nullptr)
389 return *this;
390
391 if (x.m_data.GetMode() == SmartPtrMode::Shared)
392 {
393 if (m_data.GetMode() == SmartPtrMode::Shared)
394 m_data.swapPointers(x.m_data);
395 else
396 MoveWeakFromShared(std::move(x));
397 }
398 else
399 {
400 if (x.m_data.WeakGetCounter()->GetObject() == nullptr)
401 return *this;
402 if (m_data.GetMode() == SmartPtrMode::Weak)
403 m_data.swapPointers(x.m_data);
404 else
405 MoveSharedFromWeak(std::move(x));
406 }
407 }
408 else
409 {
410 if (other_pointee == nullptr)
411 {
412 if (m_data.GetMode() == SmartPtrMode::Shared)
413 delete ReleaseSharedAndGetObjectToDelete();
414 else
415 ReleaseWeak();
416 m_data.SetNull();
417 }
418 else if (x.m_data.GetMode() == SmartPtrMode::Shared)
419 {
420 if (m_data.GetMode() == SmartPtrMode::Shared)
421 {
422 if (this_pointee != other_pointee || m_data.SharedGetOwned() != x.m_data.SharedGetOwned())
423 m_data.swapPointers(x.m_data);
424 }
425 else
426 {
427 System::Detail::SmartPtrCounter *const releaser = m_data.WeakGetCounter();
428 if (this_pointee != other_pointee || releaser->GetObject() != x.m_data.SharedGetOwned())
429 {
430 MoveWeakFromShared(std::move(x));
431 ReleaseWeak(releaser);
432 }
433 }
434 }
435 else
436 {
437 System::Detail::SmartPtrCounter *const other_counter = x.m_data.WeakGetCounter();
438 if (other_counter->GetObject() == nullptr)
439 {
440 if (m_data.GetMode() == SmartPtrMode::Shared)
441 delete ReleaseSharedAndGetObjectToDelete();
442 else
443 ReleaseWeak();
444 m_data.SetNull();
445 }
446 else if (m_data.GetMode() == SmartPtrMode::Weak)
447 {
448 if (this_pointee != other_pointee || m_data.WeakGetCounter() != other_counter)
449 m_data.swapPointers(x.m_data);
450 }
451 else
452 {
453 if (this_pointee != other_pointee || m_data.SharedGetOwned() != other_counter->GetObject())
454 {
455 SharedRefReleaser *const releaser = GetSharedReleaser();
456 MoveSharedFromWeak(std::move(x));
457 delete ReleaseSharedAndGetObjectToDelete(releaser);
458 }
459 }
460 }
461 }
462
463 return *this;
464 }
468 SmartPtr_& operator = (const SmartPtr_ &x)
469 {
470 Assign(x);
471 return *this;
472 }
477 template <typename Q>
478 SmartPtr_& operator = (const SmartPtr<Q> &x)
479 {
480 Assign(x);
481 return *this;
482 }
486 SmartPtr_& operator = (Pointee_ *p)
487 {
488 if (GetPointer() != p)
489 {
490 SmartPtr_ temp(p, get_Mode());
491 m_data.swapPointers(temp.m_data);
492 }
493 return *this;
494 }
497 SmartPtr_& operator = (std::nullptr_t)
498 {
499 delete ReleaseAndGetObjectToDelete();
500 m_data.SetNull();
501 return *this;
502 }
506 Pointee_* operator -> () const
507 {
508 return GetObjectNotNull();
509 }
512 bool operator == (std::nullptr_t) const
513 {
514 return m_data.IsNull();
515 }
518 Pointee_* get() const
519 {
520 return m_data.GetPointee();
521 }
525 {
526 return m_data.SharedGetPointee();
527 }
530 void reset(Pointee_ *ptr)
531 {
532 delete ReleaseAndGetObjectToDelete();
533 Lock(ptr);
534 }
536 void reset()
537 {
538 delete ReleaseAndGetObjectToDelete();
539 m_data.SetNull();
540 }
544 {
545 return m_data.GetMode();
546 }
549 bool IsShared() const
550 {
551 return m_data.GetMode() == SmartPtrMode::Shared;
552 }
555 bool IsWeak() const
556 {
557 return m_data.GetMode() == SmartPtrMode::Weak;
558 }
561 bool IsAliasingPtr() const
562 {
563 Object* pointee = nullptr;
564 Object* owner = nullptr;
565 if (m_data.GetMode() == SmartPtrMode::Shared)
566 {
567 pointee = m_data.SharedGetObject();
568 owner = m_data.SharedGetOwned();
569 }
570 else
571 {
572 pointee = m_data.WeakGetObject();
573 System::Detail::SmartPtrCounter* counter = m_data.WeakGetCounterOrNull();
574 owner = counter == nullptr ? nullptr : counter->GetObject();
575 }
576 return pointee != owner;
577 }
581 {
582 Object* owner = nullptr;
583 if (m_data.GetMode() == SmartPtrMode::Shared)
584 {
585 owner = m_data.SharedGetOwned();
586 }
587 else
588 {
589 System::Detail::SmartPtrCounter* counter = m_data.WeakGetCounterOrNull();
590 if (counter) {
591 owner = counter->GetObject();
592 }
593 }
594
595 return owner;
596 }
597
602 {
603 return SmartPtr_(get());
604 }
665 {
666 if (m_data.GetMode() == mode)
667 return;
668 SmartPtr_ ptr(*this, mode);
669 m_data.swap(ptr.m_data);
670 }
674 {
675 return *GetObjectNotNull();
676 }
679 explicit operator bool() const noexcept
680 {
681 return *this != nullptr;
682 }
685 bool operator !() const noexcept
686 {
687 return *this == nullptr;
688 }
693 template<class Y>
694 bool operator < (Y* p) const
695 {
696 return GetObjectOrNull() < p;
697 }
702 template<class Y>
703 bool operator < (SmartPtr<Y> const& x) const
704 {
705 return GetObjectOrNull() < x.GetObjectOrNull();
706 }
709 static const System::TypeInfo& Type()
710 {
711 return Pointee_::Type();
712 }
716 template<class Y>
718 {
719 return m_data.IsNull() ? SmartPtr<Y>() : SmartPtr<Y>(*this, static_cast<Y*>(m_data.GetPointee()));
720 }
724 template<class Y>
726 {
727 return m_data.IsNull() ? SmartPtr<Y>() : SmartPtr<Y>(*this, dynamic_cast<Y*>(m_data.GetPointee()));
728 }
732 template<class Y>
734 {
735 return m_data.IsNull() ? SmartPtr<Y>() : SmartPtr<Y>(*this, const_cast<Y*>(m_data.GetPointee()));
736 }
741 template<class Y, typename Check = std::false_type>
742 std::enable_if_t<std::is_same<Y, T>::value, SmartPtr<Y>> Cast() const
743 {
744 return *this;
745 }
750 template <class Y, typename Check = std::false_type>
751 std::enable_if_t<!std::is_same<Y, T>::value && std::is_base_of<Y, T>::value, SmartPtr<Y>> Cast() const
752 {
753 return m_data.IsNull() ? SmartPtr<Y>() : SmartPtr<Y>(*this, static_cast<Y*>(m_data.GetPointee()));
754 }
759 template <class Y, typename Check = std::false_type>
760 std::enable_if_t<Check::value && !std::is_same<Y, T>::value && !std::is_base_of<Y, T>::value, SmartPtr<Y>> Cast() const
761 {
762 auto result = m_data.IsNull() ? SmartPtr<Y>() : SmartPtr<Y>(*this, dynamic_cast<Y*>(m_data.GetPointee()));
763 if ((*this) && !result)
764 System::Detail::SmartPtrCounter::ThrowInvalidCastException();
765
766 return result;
767 }
772 template <class Y, typename Check = std::false_type>
773 std::enable_if_t<!Check::value && !std::is_same<Y, T>::value && !std::is_base_of<Y, T>::value, SmartPtr<Y>> Cast() const
774 {
775 return m_data.IsNull() ? SmartPtr<Y>() : SmartPtr<Y>(*this, dynamic_cast<Y*>(m_data.GetPointee()));
776 }
780 bool Is(const System::TypeInfo &target) const;
784 {
785 if (m_data.IsNull())
786 return nullptr;
787 else if (m_data.GetMode() == SmartPtrMode::Shared)
788 return m_data.SharedGetObject();
789 else
790 return m_data.WeakGetObject();
791 }
794 SmartPtr<Object> ToObjectPtr() const;
798 {
799 return m_data.GetPointee();
800 }
804 {
805 if (m_data.SharedGetPointee() == nullptr) //Asserts we're in shared pointer mode
806 return 0;
807 else
808#ifdef ENABLE_EXTERNAL_REFCOUNT
809 return m_data.SharedGetCounter()->SharedCount();
810#else
811 return m_data.SharedGetOwned()->SharedCount();
812#endif
813 }
816 void SetContainedTemplateWeakPtr(uint32_t argument) const;
821 template <typename IdxType>
822 decltype(System::Details::GetByIndex(std::declval<const SmartPtr_*>(), std::declval<IdxType>())) operator[] (IdxType idx) const
823 {
824 return System::Details::GetByIndex(this, idx);
825 }
826
830 template <typename Q = T> auto begin() -> decltype(std::declval<Q>().begin())
831 {
832 return GetObjectNotNull()->begin();
833 }
834
838 template <typename Q = T> auto end() -> decltype(std::declval<Q>().end())
839 {
840 return GetObjectNotNull()->end();
841 }
842
846 template <typename Q = T> auto begin() const -> decltype(std::declval<const Q>().begin())
847 {
848 return GetObjectNotNull()->begin();
849 }
850
854 template <typename Q = T> auto end() const -> decltype(std::declval<const Q>().end())
855 {
856 return GetObjectNotNull()->end();
857 }
858
862 template <typename Q = T> auto cbegin() const -> decltype(std::declval<const Q>().cbegin())
863 {
864 return GetObjectNotNull()->cbegin();
865 }
866
870 template <typename Q = T> auto cend() const -> decltype(std::declval<const Q>().cend())
871 {
872 return GetObjectNotNull()->cend();
873 }
874
877 int GetHashCode() const
878 {
879 return GetHashCodeImpl(static_cast<Pointee_*>(nullptr));
880 }
881
886 {
887 if (m_data.IsNull())
888 System::Detail::SmartPtrCounter::ThrowNullReferenceException();
889 return m_data.GetPointee();
890 }
891
892protected:
893 template <class Q> friend class SmartPtr;
894 friend class SmartPtrInfo;
895 friend class System::Details::ObjectsBag;
896#if defined(__DBG_TOOLS)
897 friend struct DBG::for_each_member_visitor;
898#endif
901 void Lock(Pointee_ *object)
902 {
903 if (object == nullptr)
904 m_data.SetNull();
905 else
906 {
907 if (m_data.GetMode() == SmartPtrMode::Shared)
908#ifdef ENABLE_EXTERNAL_REFCOUNT
909 m_data.SharedSetPointer(object, object->Object::SharedRefAdded(), object->Object::GetCounter());
910#else
911 m_data.SharedSetPointer(object, object->Object::SharedRefAdded());
912#endif
913 else
914 m_data.WeakSetPointer(object, detail::cast_statically_or_dynamically<Pointee_, Object>::cast(object), object->Object::WeakRefAdded());
915 }
916 }
920 template <class Q>
921 void Lock(const SmartPtr<Q> &ptr)
922 {
923 if (ptr.m_data.IsNull())
924 m_data.SetNull();
925 else if (m_data.GetMode() == SmartPtrMode::Weak)
926 {
927 if (ptr.m_data.GetMode() == SmartPtrMode::Weak)
928 LockWeakFromWeak(ptr);
929 else
930 LockWeakFromShared(ptr);
931 }
932 else
933 {
934 if (ptr.m_data.GetMode() == SmartPtrMode::Weak)
935 LockSharedFromWeak(ptr);
936 else
937 LockSharedFromShared(ptr);
938 }
939 }
943 template <typename Q>
945 {
946 m_data.SharedSetPointer(
948 , ptr.m_data.SharedGetObject()
950#ifdef ENABLE_EXTERNAL_REFCOUNT
951 , ptr.m_data.SharedGetCounter()
952#endif
953 );
954 }
958 template <typename Q>
960 {
961 m_data.SharedSetPointer(
963 , ptr.m_data.WeakGetObject()
964 , ptr.m_data.WeakGetCounter()->Lock()
965#ifdef ENABLE_EXTERNAL_REFCOUNT
966 , ptr.m_data.WeakGetCounter()
967#endif
968 );
969 }
973 template <typename Q>
975 {
976 m_data.WeakSetPointer(
978 , ptr.m_data.SharedGetObject()
979#ifdef ENABLE_EXTERNAL_REFCOUNT
980 , ptr.IsAliasingPtr() ? ptr.m_data.SharedGetObject()->WeakRefAdded() : ptr.m_data.SharedGetCounter()->WeakRefAdded()
981#else
983#endif
984 );
985 }
989 template <typename Q>
991 {
992 m_data.WeakSetPointer(
994 , ptr.m_data.WeakGetObject()
995 , ptr.m_data.WeakGetCounter()->WeakRefAdded()
996 );
997 }
1000 {
1001 if (m_data.HoldsReference())
1002 {
1003 if (m_data.GetMode() == SmartPtrMode::Shared)
1004 return ReleaseSharedAndGetObjectToDelete();
1005 else
1006 ReleaseWeak();
1007 }
1008 return nullptr;
1009 }
1010#ifdef ENABLE_EXTERNAL_REFCOUNT
1012 typedef System::Detail::SmartPtrCounter SharedRefReleaser;
1015 SharedRefReleaser* GetSharedReleaser() const
1016 {
1017 return m_data.SharedGetCounter();
1018 }
1019#else
1025 {
1026 return m_data.SharedGetOwned();
1027 }
1028#endif
1031 static Object* ReleaseSharedAndGetObjectToDelete(SharedRefReleaser *releaser);
1034 {
1035 return ReleaseSharedAndGetObjectToDelete(GetSharedReleaser());
1036 }
1039 static void ReleaseWeak(System::Detail::SmartPtrCounter *counter)
1040 {
1041 counter->WeakRefRemoved();
1042 }
1045 {
1046 m_data.WeakGetCounter()->WeakRefRemoved();
1047 }
1051 {
1052 if (x.m_data.IsNull())
1053 m_data.SetNull();
1054 else
1055 {
1056 m_data.SharedSetPointer(
1057 x.m_data.WeakGetPointee()
1058 , x.m_data.WeakGetCounter()->Lock()
1059#ifdef ENABLE_EXTERNAL_REFCOUNT
1060 , x.m_data.WeakGetCounter()
1061#endif
1062 );
1063 x.ReleaseWeak();
1064 x.m_data.SetNull();
1065 }
1066 }
1070 {
1071 if (x.m_data.IsNull())
1072 m_data.SetNull();
1073 else
1074 {
1075 m_data.WeakSetPointer(
1076 x.m_data.SharedGetPointee()
1077 , x.m_data.SharedGetObject()
1078#ifdef ENABLE_EXTERNAL_REFCOUNT
1079 , x.m_data.SharedGetCounter()->WeakRefAdded()
1080#else
1081 , x.m_data.SharedGetOwned()->WeakRefAdded()
1082#endif
1083 );
1084 delete x.ReleaseSharedAndGetObjectToDelete();
1085 x.m_data.SetNull();
1086 }
1087 }
1093 template <typename X, typename Y>
1094 static void InitArray(SmartPtr<Array<X>> *ptr, const SmartPtr<Array<Y>> &src)
1095 {
1096 T *const destination = new T(src->get_Length());
1097 ptr->Lock(destination);
1098 std::copy(src->data().begin(), src->data().end(), destination->data().begin());
1099 }
1103 template <typename Q, typename R = decltype(std::declval<Q*>()->GetHashCode())>
1105 {
1106 if (m_data.IsNull())
1107 return 0;
1108 else
1109 return GetPointer()->GetHashCode();
1110 }
1112 int GetHashCodeImpl(void*) const
1113 {
1114 if (m_data.IsNull())
1115 return 0;
1116 else if (m_data.GetMode() == SmartPtrMode::Shared)
1117 return m_data.SharedGetObject()->GetHashCode();
1118 else
1119 return m_data.WeakGetCounter()->GetObject()->GetHashCode();
1120 }
1124 template <typename Q>
1125 void Assign(const SmartPtr<Q> &x)
1126 {
1127 Pointee_ *const this_pointee = m_data.GetPointee();
1128 Pointee_ *const other_pointee = x.m_data.GetPointee();
1129
1130 if (this_pointee == nullptr)
1131 {
1132 if (other_pointee == nullptr)
1133 return;
1135 {
1136 if (m_data.GetMode() == SmartPtrMode::Shared)
1137 LockSharedFromShared(x);
1138 else
1139 LockWeakFromShared(x);
1140 }
1141 else
1142 {
1143 if (x.m_data.WeakGetCounter()->GetObject() == nullptr)
1144 return;
1145 if (m_data.GetMode() == SmartPtrMode::Shared)
1146 LockSharedFromWeak(x);
1147 else
1148 LockWeakFromWeak(x);
1149 }
1150 }
1151 else if (x.m_data.GetMode() == SmartPtrMode::Shared)
1152 {
1153 if (other_pointee == nullptr)
1154 {
1155 if (m_data.GetMode() == SmartPtrMode::Shared)
1156 delete ReleaseSharedAndGetObjectToDelete();
1157 else
1158 ReleaseWeak();
1159 m_data.SetNull();
1160 }
1161 else if (m_data.GetMode() == SmartPtrMode::Shared)
1162 {
1163 if (this_pointee != other_pointee || m_data.SharedGetOwned() != x.m_data.SharedGetOwned())
1164 {
1165 SharedRefReleaser *const releaser = GetSharedReleaser();
1166 LockSharedFromShared(x);
1167 delete ReleaseSharedAndGetObjectToDelete(releaser);
1168 }
1169 }
1170 else
1171 {
1172 if (this_pointee != other_pointee || m_data.WeakGetCounter()->GetObject() != x.m_data.SharedGetOwned())
1173 {
1174 ReleaseWeak();
1175 LockWeakFromShared(x);
1176 }
1177 }
1178 }
1179 else
1180 {
1181 if (other_pointee == nullptr)
1182 {
1183 if (m_data.GetMode() == SmartPtrMode::Shared)
1184 delete ReleaseSharedAndGetObjectToDelete();
1185 else
1186 ReleaseWeak();
1187 m_data.SetNull();
1188 return;
1189 }
1190
1191 Object *const other_owned = x.m_data.WeakGetCounter()->GetObject();
1192 if (other_owned == nullptr)
1193 {
1194 if (m_data.GetMode() == SmartPtrMode::Shared)
1195 delete ReleaseSharedAndGetObjectToDelete();
1196 else
1197 ReleaseWeak();
1198 m_data.SetNull();
1199 }
1200 else if (m_data.GetMode() == SmartPtrMode::Shared)
1201 {
1202 if (this_pointee != other_pointee || m_data.SharedGetOwned() != other_owned)
1203 {
1204 delete ReleaseSharedAndGetObjectToDelete();
1205 LockSharedFromWeak(x);
1206 }
1207 }
1208 else
1209 {
1210 if (this_pointee != other_pointee || m_data.WeakGetCounter() != x.m_data.WeakGetCounter())
1211 {
1212 System::Detail::SmartPtrCounter *const releaser = m_data.WeakGetCounter();
1213 LockWeakFromWeak(x);
1214 ReleaseWeak(releaser);
1215 }
1216 }
1217 }
1218 }
1220 class Data {
1222 SmartPtrMode m_mode;
1224 Pointee_ *m_pointee;
1226 Object *m_object;
1227#ifdef ENABLE_EXTERNAL_REFCOUNT
1229 Object *m_owned;
1231 System::Detail::SmartPtrCounter *m_counter;
1232#else
1233 union {
1237 System::Detail::SmartPtrCounter *m_counter;
1238 };
1239#endif
1240 public:
1243 : m_mode(mode) //No initialization for other members - they will be assigned by SmartPtr constructor
1244 {}
1245 Data(const Data&) = delete;
1246 Data(Data&&) = delete;
1247 Data& operator = (const Data&) = delete;
1248 Data& operator = (Data&&) = delete;
1252 {
1253 return m_mode;
1254 }
1258 {
1259 m_mode = mode;
1260 }
1263 T* GetPointee() const
1264 {
1265 return m_pointee;
1266 }
1269 T* GetComparable() const
1270 {
1271 if (m_mode == SmartPtrMode::Shared)
1272 return m_pointee;
1273 else if (m_pointee == nullptr)
1274 return nullptr;
1275 else if (m_counter->GetObject() == nullptr)
1276 return nullptr;
1277 else
1278 return m_pointee;
1279 }
1282 bool IsNull() const
1283 {
1284 return m_pointee == nullptr || (m_mode == SmartPtrMode::Weak && m_counter->GetObject() == nullptr);
1285 }
1287 void SetNull()
1288 {
1289 m_pointee = nullptr;
1290 }
1293 bool HoldsReference() const
1294 {
1295 return m_pointee != nullptr;
1296 }
1300 {
1301 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Weak);
1302 return m_pointee;
1303 }
1306 System::Detail::SmartPtrCounter* WeakGetCounter() const
1307 {
1308 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Weak);
1309 return m_counter;
1310 }
1313 System::Detail::SmartPtrCounter* WeakGetCounterOrNull() const
1314 {
1315 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Weak);
1316 return IsNull() ? nullptr : m_counter;
1317 }
1322 void WeakSetPointer(Pointee_ *pointee, Object *object, System::Detail::SmartPtrCounter *counter)
1323 {
1324 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Weak);
1325 m_pointee = pointee;
1326 m_object = object;
1327 m_counter = counter;
1328 }
1332 {
1333 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1334 return m_pointee;
1335 }
1339 {
1340 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1341 return m_object;
1342 }
1346 {
1347 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Weak);
1348 return m_object;
1349 }
1353 {
1354 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1355 return m_owned;
1356 }
1360 {
1361 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1362 return IsNull() ? nullptr : m_object;
1363 }
1367 {
1368 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1369 return IsNull() ? nullptr : m_owned;
1370 }
1371#ifdef ENABLE_EXTERNAL_REFCOUNT
1374 System::Detail::SmartPtrCounter* SharedGetCounter() const
1375 {
1376 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1377 return m_counter;
1378 }
1381 System::Detail::SmartPtrCounter** SharedGetCounterPointer()
1382 {
1383 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1384 return &m_counter;
1385 }
1388 System::Detail::SmartPtrCounter* SharedGetCounterOrNull() const
1389 {
1390 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1391 return IsNull() ? nullptr : m_counter;
1392 }
1398 void SharedSetPointer(Pointee_ *pointee, Object *object, Object *owned, System::Detail::SmartPtrCounter *counter)
1399 {
1400 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1401 m_pointee = pointee;
1402 m_object = object;
1403 m_owned = owned;
1404 m_counter = counter;
1405 }
1410 void SharedSetPointer(Pointee_ *pointee, Object *object, System::Detail::SmartPtrCounter *counter)
1411 {
1412 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1413 m_pointee = pointee;
1414 m_object = object;
1415 m_owned = object;
1416 m_counter = counter;
1417 }
1418#else
1423 void SharedSetPointer(Pointee_ *pointee, Object *object, Object *owned)
1424 {
1425 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1426 m_pointee = pointee;
1427 m_object = object;
1428 m_owned = owned;
1429 }
1433 void SharedSetPointer(Pointee_ *pointee, Object *object)
1434 {
1435 CODEPORTING_DEBUG_ASSERT(m_mode == SmartPtrMode::Shared);
1436 m_pointee = pointee;
1437 m_object = object;
1438 m_owned = object;
1439 }
1440#endif
1443 void swap(Data &data) noexcept
1444 {
1445 std::swap(m_mode, data.m_mode);
1446 swapPointers(data);
1447 }
1450 void swapPointers(Data &data) noexcept
1451 {
1452 std::swap(m_pointee, data.m_pointee);
1453 std::swap(m_object, data.m_object);
1454 std::swap(m_owned, data.m_owned);
1455#ifdef ENABLE_EXTERNAL_REFCOUNT
1456 std::swap(m_counter, data.m_counter);
1457#endif
1458 }
1459 }
1462};
1463
1464
1465namespace Detail {
1466#ifdef ENABLE_EXTERNAL_REFCOUNT
1468 class OwnNextObject
1469 {
1470 public:
1472 OwnNextObject()
1473 {
1474 SmartPtrCounter::NextOwnership() = SmartPtrCounter::BeingConstructed; // Not until constructors succeed
1475 }
1479 template <typename T>
1480 void CreatedSuccessfully(T *object) {
1481 object->Object::GetCounter()->CreatedSuccessfully(); // No exceptions thrown - should now manage memory by shared pointers
1482 }
1483 };
1484#else
1486 class OwnNextObject
1487 {
1488 public:
1491 template <typename T>
1492 void CreatedSuccessfully(T *) {}
1493 };
1494#endif
1495}
1498template <class T>
1499struct IsSmartPtr : System::detail::is_a<T, System::SmartPtr> {};
1505template<class T, class ...Args>
1506typename std::enable_if<!IsSmartPtr<T>::value, SmartPtr<T> >::type MakeObject(Args&&... args)
1507{
1508 System::Details::MakeObjectLeakageDetector::SignatureBacktraceSentry signature_sentry;
1509 System::Details::MakeObjectLeakageDetector::SetSignature<T>(std::forward<Args>(args)...);
1510
1511 System::Detail::OwnNextObject ownershipSentry;
1512
1513 T *const object = ::new T(std::forward<Args>(args)...);
1514
1515 ownershipSentry.CreatedSuccessfully(object);
1516 signature_sentry.CreatedSuccessfully(object);
1517
1518 return SmartPtr<T>(object);
1519}
1525template<class T, class ...Args>
1526typename std::enable_if<IsSmartPtr<T>::value, T>::type MakeObject(Args&&... args)
1527{
1528 return MakeObject<typename T::Pointee_>(std::forward<Args>(args)...);
1529}
1536template<class X, class Y>
1537bool operator == (const SmartPtr<X> &x, const SmartPtr<Y> &y)
1538{
1539 return x.GetObjectOrNull() == y.GetObjectOrNull();
1540}
1547template<class X, class Y>
1548bool operator != (const SmartPtr<X> &x, const SmartPtr<Y> &y)
1549{
1550 return x.GetObjectOrNull() != y.GetObjectOrNull();
1551}
1556template<class X>
1557bool operator == (std::nullptr_t, SmartPtr<X> const& x)
1558{
1559 return x == nullptr;
1560}
1565template<class X>
1566bool operator !=(SmartPtr<X> const& x, std::nullptr_t)
1567{
1568 return !(x == nullptr);
1569}
1574template<class X>
1575bool operator !=(std::nullptr_t, SmartPtr<X> const& x)
1576{
1577 return !(x == nullptr);
1578}
1585template<class X, class Y>
1586typename std::enable_if<std::is_base_of<Object, Y>::value && detail::has_no_operator_equal<X,Y>::value, bool>::type
1587operator == (const SmartPtr<X>& x, const Y* y)
1588{
1589 return x.GetObjectOrNull() == static_cast<const Object*>(y);
1590}
1597template<class X, class Y>
1598typename std::enable_if<std::is_base_of<Object, Y>::value && detail::has_no_operator_equal<X,Y>::value, bool>::type
1599operator != (const SmartPtr<X>& x, const Y* y)
1600{
1601 return x.GetObjectOrNull() != static_cast<const Object*>(y);
1602}
1609template<class X, class Y>
1610typename std::enable_if<std::is_base_of<Object, X>::value && detail::has_no_operator_equal<X,Y>::value, bool>::type
1611operator == (const X* x, const SmartPtr<Y>& y)
1612{
1613 return y.GetObjectOrNull() == static_cast<const Object*>(x);
1614}
1621template<class X, class Y>
1622typename std::enable_if<std::is_base_of<Object, X>::value && detail::has_no_operator_equal<X,Y>::value, bool>::type
1623operator != (const X* x, const SmartPtr<Y>& y)
1624{
1625 return y.GetObjectOrNull() != static_cast<const Object*>(x);
1626}
1631template<class T>
1632typename std::enable_if<!std::is_scalar<T>::value && !std::is_pointer<T>::value && !std::is_array<T>::value && detail::has_method_is_null<T>::value, bool>::type operator ==(T const& x, std::nullptr_t)
1633{
1634 return x.IsNull();
1635}
1640template<class T>
1641typename std::enable_if<!std::is_scalar<T>::value && !std::is_pointer<T>::value && !std::is_array<T>::value && detail::has_method_is_null<T>::value, bool>::type operator ==(std::nullptr_t, T const& x)
1642{
1643 return x.IsNull();
1644}
1649template<class X>
1651{
1653}
1658template<class X>
1660{
1661 return SmartPtr<X>(const_cast<X*>(p), SmartPtrMode::Shared);
1662}
1668template<class Y, class X>
1670{
1671 return x.template static_pointer_cast<Y>();
1672}
1678template<class Y, class X>
1680{
1681 return x.template dynamic_pointer_cast<Y>();
1682}
1683
1689template<class Y, class X>
1691{
1692 return x.template const_pointer_cast<Y>();
1693}
1697{
1698public:
1701 : m_object(nullptr)
1702 , m_owned(nullptr)
1703 , m_internal_pointer(nullptr)
1704 {}
1708 template <typename T>
1709 explicit inline SmartPtrInfo(const SmartPtr<T> &ptr)
1710 : m_object(ptr.IsShared() ? ptr.m_data.SharedGetObject() : nullptr)
1711 , m_owned(ptr.IsShared() ? ptr.m_data.SharedGetOwned() : nullptr)
1712 , m_internal_pointer(ptr.IsShared() ? ptr.m_data.GetPointee() : nullptr)
1713 {}
1716 inline operator bool() const
1717 {
1718 return m_internal_pointer != nullptr;
1719 }
1722 inline bool operator ! () const
1723 {
1724 return m_object == nullptr;
1725 }
1728 inline Object* operator -> () const
1729 {
1730 return m_object;
1731 }
1735 inline bool operator < (const SmartPtrInfo &other) const
1736 {
1737 return m_internal_pointer < other.m_internal_pointer;
1738 }
1741 inline Object* getObject() const
1742 {
1743 return m_object;
1744 }
1747 inline Object* getOwned() const
1748 {
1749 return m_owned;
1750 }
1753 const void* getInternalPtr() const
1754 {
1755 return m_internal_pointer;
1756 }
1757
1758private:
1760 Object *m_object;
1761 Object *m_owned;
1763 void *m_internal_pointer;
1764};
1769template<class T>
1771{
1772 return x.GetPointer();
1773}
1776template <typename T> using ArrayPtr = SmartPtr<Array<T>>;
1779template <typename T> using SharedPtr = SmartPtr<T>;
1784template <typename T>
1785T& Ref(T &value)
1786{
1787 return value;
1788}
1789
1793template <typename T>
1794struct BasePointerType
1795{
1797 using type = T;
1798};
1800
1802template <typename T>
1803std::enable_if_t<detail::has_print_to_function<T>::value, void> PrintTo(const SmartPtr<T>& object_ptr, std::ostream* stream)
1804{
1805 if (object_ptr == nullptr)
1806 *stream << "nullptr";
1807 else
1808 PrintTo(*object_ptr, stream);
1809}
1810
1812template <typename T>
1813std::enable_if_t<!detail::has_print_to_function<T>::value, void> PrintTo(const SmartPtr<T>& object_ptr, std::ostream* stream)
1814{
1815 if (object_ptr == nullptr)
1816 *stream << "nullptr";
1817 else
1818 PrintTo(*object_ptr.GetObjectOrNull(), stream);
1819}
1820
1825template <typename T>
1826std::ostream& operator<<(std::ostream& stream, const SharedPtr<T>& object_ptr)
1827{
1828 if (object_ptr != nullptr)
1829 stream << object_ptr->ToString();
1830 return stream;
1831}
1832
1837template <typename T>
1838std::wostream& operator<<(std::wostream& stream, const SharedPtr<T>& object_ptr)
1839{
1840 if (object_ptr != nullptr)
1841 stream << object_ptr->ToString();
1842 return stream;
1843}
1844
1845} //namespace System
1846
1847#define CODEPORTING_ARGS(...) __VA_ARGS__
1848
1849#define MEMBER_FUNCTION_MAKE_OBJECT(name, args_with_types, args) \
1850 \
1851static System::SmartPtr<name> MakeObject(args_with_types) \
1852{ \
1853 System::Details::MakeObjectLeakageDetector::SignatureBacktraceSentry signature_sentry; \
1854 System::Details::MakeObjectLeakageDetector::SetSignature<name>(args); \
1855\
1856 System::Detail::OwnNextObject ownershipSentry; \
1857\
1858 name *const object = ::new name(args); \
1859\
1860 ownershipSentry.CreatedSuccessfully(object); \
1861 signature_sentry.CreatedSuccessfully(object); \
1862\
1863 return System::SmartPtr<name>(object); \
1864}
1866#define MEMBER_FUNCTION_MAKE_OBJECT_DECLARATION(name, args_with_types) \
1867static System::SmartPtr<name> MakeObject(args_with_types);
1868
1869#define MEMBER_FUNCTION_MAKE_OBJECT_DEFINITION(name, args_with_types, args) \
1870System::SmartPtr<name> name::MakeObject(args_with_types) \
1871{ \
1872 System::Details::MakeObjectLeakageDetector::SignatureBacktraceSentry signature_sentry; \
1873 System::Details::MakeObjectLeakageDetector::SetSignature<name>(args); \
1874\
1875 System::Detail::OwnNextObject ownershipSentry; \
1876\
1877 name *const object = ::new name(args); \
1878\
1879 ownershipSentry.CreatedSuccessfully(object); \
1880 signature_sentry.CreatedSuccessfully(object); \
1881\
1882 return System::SmartPtr<name>(object); \
1883}
1884
1885namespace std
1886{
1889 template <class T>
1890 struct hash<System::SharedPtr<T> >
1891 {
1893 using argument_type = System::SharedPtr<T>;
1895 using result_type = std::size_t;
1899 std::size_t operator()(System::SharedPtr<T> const& val) const
1900 {
1901 return val.GetHashCode();
1902 }
1903 };
1904}
1905
1906#endif //_aspose_system_smart_ptr_h_
Class that represents an array data structure. Objects of this class should only be allocated using S...
Definition: array.h:259
typename System::Details::SelectType< T >::type UnderlyingType
Alias for the type used to represent each element of the array.
Definition: array.h:270
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Object * SharedRefAdded()
Increments shared reference count. Shouldn't be called directly; instead, use smart pointers or ThisP...
Definition: object.h:297
Detail::SmartPtrCounter * WeakRefAdded()
Increments weak reference count. Shouldn't be called directly; instead, use smart pointers or ThisPro...
Definition: object.h:328
Internal data storage class which hides data members and enforces neccessary asserts around them.
Definition: smart_ptr.h:1220
void SetNull()
Sets pointer to null.
Definition: smart_ptr.h:1287
Object * WeakGetObject() const
Gets referenced object which is cast to System::Object. Asserts that pointer is weak....
Definition: smart_ptr.h:1345
T * GetComparable() const
Gets pointed object (if set and not yet deleted) or nullptr.
Definition: smart_ptr.h:1269
void SharedSetPointer(Pointee_ *pointee, Object *object, Object *owned)
Sets pointer value. Asserts that pointer is shared.
Definition: smart_ptr.h:1423
Object * SharedGetObjectOrNull() const
Gets referenced object (if any) which is cast to System::Object or null. Asserts that pointer is shar...
Definition: smart_ptr.h:1359
void swapPointers(Data &data) noexcept
Swaps pointer data of two Data instances, excluding modes.
Definition: smart_ptr.h:1450
Data(const Data &)=delete
void SharedSetPointer(Pointee_ *pointee, Object *object)
Sets pointer value. Asserts that pointer is shared.
Definition: smart_ptr.h:1433
System::Detail::SmartPtrCounter * WeakGetCounterOrNull() const
Gets referenced object (if any) reference counter or null. Asserts that pointer is weak.
Definition: smart_ptr.h:1313
T * GetPointee() const
Gets pointed object (if any) or nullptr.
Definition: smart_ptr.h:1263
Data(SmartPtrMode mode)
Initializes Data structure. Doesn't set any members except for m_mode.
Definition: smart_ptr.h:1242
Object * SharedGetOwnedOrNull() const
Gets owned object. Asserts that pointer is shared.
Definition: smart_ptr.h:1366
Object * SharedGetOwned() const
Gets owned object which. Asserts that pointer is shared. Doesn't null-check.
Definition: smart_ptr.h:1352
Pointee_ * WeakGetPointee() const
Gets referenced object. Asserts that pointer is weak.
Definition: smart_ptr.h:1299
System::Detail::SmartPtrCounter * WeakGetCounter() const
Gets referenced object reference counter. Asserts that pointer is weak. Doesn't null-check.
Definition: smart_ptr.h:1306
System::Detail::SmartPtrCounter * m_counter
Reference counter structure linked to m_pointee.
Definition: smart_ptr.h:1237
Data(Data &&)=delete
Object * m_owned
Owned pointer.
Definition: smart_ptr.h:1235
bool IsNull() const
Checks if referenced object is set and not yet deleted.
Definition: smart_ptr.h:1282
void WeakSetPointer(Pointee_ *pointee, Object *object, System::Detail::SmartPtrCounter *counter)
Sets pointer value. Asserts that pointer is weak.
Definition: smart_ptr.h:1322
void SetMode(SmartPtrMode mode)
Sets pointer mode. Doesn't do any reference count or pointer changes.
Definition: smart_ptr.h:1257
bool HoldsReference() const
Checks if pointer actually holds a shared or weak reference to an object.
Definition: smart_ptr.h:1293
void swap(Data &data) noexcept
Swaps data of two Data instances, including modes and pointers.
Definition: smart_ptr.h:1443
SmartPtrMode GetMode() const
Gets current pointer mode.
Definition: smart_ptr.h:1251
Pointee_ * SharedGetPointee() const
Gets referenced object. Asserts that pointer is shared.
Definition: smart_ptr.h:1331
Object * SharedGetObject() const
Gets referenced object which is cast to System::Object. Asserts that pointer is shared....
Definition: smart_ptr.h:1338
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
int GetHashCode() const
Calls GetHashCode() on pointed object.
Definition: smart_ptr.h:877
void LockSharedFromShared(const SmartPtr< Q > &ptr)
Sets pointee object. Asserts that both current object and ptr are in shared mode.
Definition: smart_ptr.h:944
SmartPtr(const SmartPtr< P > &ptr, Pointee_ *p, SmartPtrMode mode=SmartPtrMode::Shared)
Constructs a SmartPtr which shares ownership information with the initial value of ptr,...
Definition: smart_ptr.h:327
auto end() -> decltype(std::declval< Q >().end())
Accessor for end() method of an underling collection. Only compiles if SmartPtr_ is specialization ty...
Definition: smart_ptr.h:838
SmartPtr< T > SmartPtr_
Specialized smart pointer type.
Definition: smart_ptr.h:185
SmartPtr(std::nullptr_t=nullptr, SmartPtrMode mode=SmartPtrMode::Shared)
Creates null-pointer SmartPtr object of required mode.
Definition: smart_ptr.h:199
SmartPtr(const SmartPtr< Array< Y > > &src, SmartPtrMode mode=SmartPtrMode::Shared)
Converts type of referenced array by creating a new array of different type. Useful if in C# there is...
Definition: smart_ptr.h:254
Pointee_ * GetPointer() const
Gets pointed object (if any) or nullptr. Same as get().
Definition: smart_ptr.h:797
void ReleaseWeak()
Decrements currently referenced object's weak pointer counter.
Definition: smart_ptr.h:1044
SmartPtr(SmartPtrMode mode)
Creates SmartPtr object of required mode.
Definition: smart_ptr.h:192
Object * ReleaseAndGetObjectToDelete()
Decrements currently referenced object's shared or weak pointer counter, depending on current pointer...
Definition: smart_ptr.h:999
void set_Mode(SmartPtrMode mode)
Sets pointer mode. May alter referenced object's reference counts.
Definition: smart_ptr.h:664
typename System::Details::SelectType< typename System::Details::ArrayTypeResolver< T >::value_type >::type ValueType
Storage type of pointed array. Only meaningful if T is a specialization of System::Array.
Definition: smart_ptr.h:189
Pointee_ * get() const
Gets pointed object.
Definition: smart_ptr.h:518
auto cend() const -> decltype(std::declval< const Q >().cend())
Accessor for cend() method of an underling collection. Only compiles if SmartPtr_ is specialization t...
Definition: smart_ptr.h:870
SmartPtr(const Y &)
Initializes empty array. Used to translate some C# code constructs.
Definition: smart_ptr.h:262
SmartPtr(const SmartPtr< Q > &x, SmartPtrMode mode=SmartPtrMode::Shared)
Copy constructs SmartPtr object. Both pointers point to the same object afterwards....
Definition: smart_ptr.h:225
SmartPtr< Y > dynamic_pointer_cast() const
Casts pointer to different type using dynamic_cast on pointed object.
Definition: smart_ptr.h:725
std::enable_if_t< std::is_same< Y, T >::value, SmartPtr< Y > > Cast() const
Casts pointer to its type itself.
Definition: smart_ptr.h:742
void Lock(Pointee_ *object)
Sets pointee object. Increments shared or weak reference count, depending on pointer mode.
Definition: smart_ptr.h:901
void LockSharedFromWeak(const SmartPtr< Q > &ptr)
Sets pointee object. Asserts that current object is in shared mode and ptr is in weak mode.
Definition: smart_ptr.h:959
bool IsShared() const
Checks if pointer is in shared mode.
Definition: smart_ptr.h:549
Object SharedRefReleaser
Type to use to release shared pointers. Depends on whether external refcount is on or off.
Definition: smart_ptr.h:1021
static void InitArray(SmartPtr< Array< X > > *ptr, const SmartPtr< Array< Y > > &src)
Performs actual array copying on cast constructor calls.
Definition: smart_ptr.h:1094
SmartPtr< Y > static_pointer_cast() const
Casts pointer to different type using static_cast on pointed object.
Definition: smart_ptr.h:717
auto begin() -> decltype(std::declval< Q >().begin())
Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization ...
Definition: smart_ptr.h:830
~SmartPtr()
Destroys SmartPtr object. If required, decreases pointed object's reference counter and deletes objec...
Definition: smart_ptr.h:374
bool IsWeak() const
Checks if pointer is in weak mode.
Definition: smart_ptr.h:555
SmartPtr_ RemoveAliasing() const
Removes aliasing (created by an aliasing constructor) from pointer, makes sure it manages (if shared)...
Definition: smart_ptr.h:601
SmartPtr(Pointee_ *object, SmartPtrMode mode=SmartPtrMode::Shared)
Creates SmartPtr pointing to specified object, or converts raw pointer to SmartPtr.
Definition: smart_ptr.h:207
Object * GetObjectOrNull() const
Gets pointed object (if any) or nullptr. Same as get().
Definition: smart_ptr.h:783
void MoveWeakFromShared(SmartPtr &&x)
Implements move semantics. Asserts that current object is in weak mode and x is in shared mode.
Definition: smart_ptr.h:1069
void Lock(const SmartPtr< Q > &ptr)
Sets pointee object. Increments shared or weak reference count, depending on pointer mode.
Definition: smart_ptr.h:921
SharedRefReleaser * GetSharedReleaser() const
Gets object to use to release shared pointer to.
Definition: smart_ptr.h:1024
void reset()
Makes pointer pointing to nullptr.
Definition: smart_ptr.h:536
System::Details::ArrayTypeResolver< T >::type ArrayType
Same as Pointee_, if it is a specialization of System::Array, and void otherwise.
Definition: smart_ptr.h:187
std::enable_if_t<!std::is_same< Y, T >::value &&std::is_base_of< Y, T >::value, SmartPtr< Y > > Cast() const
Casts pointer to base type using static_cast.
Definition: smart_ptr.h:751
void reset(Pointee_ *ptr)
Sets pointed object.
Definition: smart_ptr.h:530
int get_shared_count() const
Gets number of shared pointers existing to referenced object, including current one....
Definition: smart_ptr.h:803
std::enable_if_t<!Check::value &&!std::is_same< Y, T >::value &&!std::is_base_of< Y, T >::value, SmartPtr< Y > > Cast() const
Casts pointer to derived type dynamic_cast.
Definition: smart_ptr.h:773
SmartPtr(SmartPtr_ &&x, SmartPtrMode mode=SmartPtrMode::Shared) noexcept
Move constructs SmartPtr object. Effectively, swaps two pointers, if they are both of same mode....
Definition: smart_ptr.h:233
SmartPtr< Y > const_pointer_cast() const
Casts pointer to different type using const_cast on pointed object.
Definition: smart_ptr.h:733
int GetHashCodeImpl(void *) const
Calls GetHashCode() method from Object if it is not available on Pointee_ type (e....
Definition: smart_ptr.h:1112
SmartPtr(const SmartPtr_ &ptr, SmartPtrMode mode=SmartPtrMode::Shared)
Copy constructs SmartPtr object. Both pointers point to the same object afterwards.
Definition: smart_ptr.h:215
SmartPtrMode get_Mode() const
Gets pointer mode.
Definition: smart_ptr.h:543
T * GetObjectNotNull() const
Gets currently referenced object (if any) or throws.
Definition: smart_ptr.h:885
void MoveSharedFromWeak(SmartPtr &&x)
Implements move semantics. Asserts that current object is in shared mode and x is in weak mode.
Definition: smart_ptr.h:1050
void Assign(const SmartPtr< Q > &x)
Copy-assigns SmartPtr object. Does type conversions, if required.
Definition: smart_ptr.h:1125
static const System::TypeInfo & Type()
Shortcut to get System::TypeInfo object for the Pointee_ type.
Definition: smart_ptr.h:709
Pointee_ * get_shared() const
Gets pointed object, but asserts that pointer is in shared mode.
Definition: smart_ptr.h:524
void LockWeakFromShared(const SmartPtr< Q > &ptr)
Sets pointee object. Asserts that current object is in weak mode and ptr is in shared mode.
Definition: smart_ptr.h:974
class System::SmartPtr::Data m_data
An instance of Data class.
auto end() const -> decltype(std::declval< const Q >().end())
Accessor for end() method of an underling collection. Only compiles if SmartPtr_ is specialization ty...
Definition: smart_ptr.h:854
Object * GetObjectOwner() const
Gets referenced object.
Definition: smart_ptr.h:580
bool IsAliasingPtr() const
Checks if pointer is pointed to another object than owned (created by an aliasing constructor).
Definition: smart_ptr.h:561
static void ReleaseWeak(System::Detail::SmartPtrCounter *counter)
Decrements weak pointer counter.
Definition: smart_ptr.h:1039
auto begin() const -> decltype(std::declval< const Q >().begin())
Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization ...
Definition: smart_ptr.h:846
T Pointee_
Pointed type.
Definition: smart_ptr.h:183
auto cbegin() const -> decltype(std::declval< const Q >().cbegin())
Accessor for cbegin() method of an underling collection. Only compiles if SmartPtr_ is specialization...
Definition: smart_ptr.h:862
std::enable_if_t< Check::value &&!std::is_same< Y, T >::value &&!std::is_base_of< Y, T >::value, SmartPtr< Y > > Cast() const
Casts pointer to derived type dynamic_cast.
Definition: smart_ptr.h:760
void LockWeakFromWeak(const SmartPtr< Q > &ptr)
Sets pointee object. Asserts that both current object and ptr are in weak mode.
Definition: smart_ptr.h:990
R GetHashCodeImpl(Q *) const
Calls into GetHashCode() method if it is available on Pointee_ type (which is true if it is a complet...
Definition: smart_ptr.h:1104
Object * ReleaseSharedAndGetObjectToDelete()
Decrements currently referenced object's shared pointer counter.
Definition: smart_ptr.h:1033
Service class to test and alter SmartPtr's contents without knowing final type. Used for garbage coll...
Definition: smart_ptr.h:1697
Object * getObject() const
Gets object referenced pointer points to.
Definition: smart_ptr.h:1741
bool operator!() const
Checks if info object does not point to non-null pointer.
Definition: smart_ptr.h:1722
bool operator<(const SmartPtrInfo &other) const
Less-compares values of pointers referenced by two info objects.
Definition: smart_ptr.h:1735
SmartPtrInfo(const SmartPtr< T > &ptr)
Creates SmartPtrInfo object with information on specific smart pointer.
Definition: smart_ptr.h:1709
Object * operator->() const
Allows to call methods of Object pointed by the referenced pointer.
Definition: smart_ptr.h:1728
SmartPtrInfo()
Creates empty SmartPtrInfo object.
Definition: smart_ptr.h:1700
const void * getInternalPtr() const
Gets raw object referenced pointer points to.
Definition: smart_ptr.h:1753
Object * getOwned() const
Gets object owned pointer.
Definition: smart_ptr.h:1747
Represents a particular type and provides information about it.
Definition: type_info.h:109
@ TypeInfo
Specifies that the member is a type.
Definition: db_command.h:9
void PrintTo(DateTime value, std::ostream *stream)
Prints value to ostream. Mostly used for debug.
bool operator!=(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:157
SmartPtr< Y > static_pointer_cast(SmartPtr< X > const &x)
Casts smart pointers using static_cast.
Definition: smart_ptr.h:1669
SmartPtrMode
SmartPtr pointer type: weak or shared. Defines whether pointer is being counted when it is being deci...
Definition: smart_ptr.h:68
@ Weak
Weak mode: pointer does not participate in reference counting.
@ Shared
Shared mode: pointer participates in reference counting.
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650
SmartPtr< Y > const_pointer_cast(SmartPtr< X > const &x)
Casts smart pointers using const_cast.
Definition: smart_ptr.h:1690
std::enable_if<!IsSmartPtr< T >::value, SmartPtr< T > >::type MakeObject(Args &&... args)
Creates object on heap and returns shared pointer to it.
Definition: smart_ptr.h:1506
SmartPtr< T > SharedPtr
Alias for smart pointer widely used in the library.
Definition: smart_ptr.h:1779
decltype(Ref(std::declval< T & >())) Ref(const std::reference_wrapper< T > &wrapper)
Wrapper to make sure Ref(std::ref(DynamicWeakPtr)) works.
Definition: dynamic_weak_ptr.h:207
SmartPtr< Y > dynamic_pointer_cast(SmartPtr< X > const &x)
Casts smart pointers using dynamic_cast.
Definition: smart_ptr.h:1679
Decimal operator*(const T &x, const Decimal &d)
Returns a new instance of Decimal class that represents a value that is a result of multiplication of...
Definition: decimal.h:556
class ASPOSECPP_SHARED_CLASS Array
Definition: smart_ptr.h:22
T * get_pointer(System::SmartPtr< T > const &x)
Gets referenced object of smart pointer.
Definition: smart_ptr.h:1770
std::ostream & operator<<(std::ostream &stream, DateTime date_time)
Insert data into the stream using UTF-8 encoding.
Definition: date_time.h:729
constexpr bool operator<(std::nullptr_t, DateTime)
Definition: date_time.h:712
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:151
Trait class to check if a type is a specialization of SmartPtr class.
Definition: smart_ptr.h:1499
Performs cheapest cast possible from static_cast and dynamic_cast.
Definition: detail.h:34
Checks whether IsNull method exists. If so, inherits std::true_type, otherwise inherits std::false_ty...
Definition: detail.h:246
Tests if specific type is a specialization of specific template. If it is, inherits std::true_type,...
Definition: detail.h:80