6#include <system/type_info.h>
7#include <system/reflection.h>
8#include <system/smart_ptr.h>
9#include <system/weak_ptr.h>
10#include <system/dynamic_weak_ptr.h>
11#include <system/refcount.h>
13#include <system/cycles_detection.h>
14#include <system/details/dbg_tools.h>
15#include <system/details/dbg_garbage_collection.h>
16#include <system/details/leakage_detector.h>
17#include <system/details/lock_context.h>
32class Details_Exception;
55#ifdef ASPOSE_GET_SHARED_MEMBERS
57 typedef System::Details::SharedMembersType shared_members_type;
70 Object& operator = (
Object const& x){ ASPOSE_UNUSED(x);
return *
this; }
76 int SharedRefRemovedSafe();
80 int RemovedSharedRefs(
int count);
83 Detail::SmartPtrCounter* WeakRefAdded();
85 void WeakRefRemoved();
88 inline Detail::SmartPtrCounter* GetCounter();
91 int SharedCount()
const;
93 ASPOSECPP_SHARED_API
void Lock();
114 static typename std::enable_if<!IsSmartPtr<T>::value,
bool>::type
ReferenceEquals(T
const& objA, T
const& objB)
116 return &objA == &objB;
123 static typename std::enable_if<!IsSmartPtr<T>::value,
bool>::type
ReferenceEquals(T
const& objA, std::nullptr_t)
133 template<
typename T1,
typename T2>
136 return ReferenceEquals(objA, objB) || (objA !=
nullptr && objB !=
nullptr && objA->Equals(objB));
144 template<
typename T1,
typename T2>
163 return *static_holder<ThisTypeInfo>();
171 virtual ASPOSECPP_SHARED_API
bool Is(
const TypeInfo& targetType)
const;
175#ifdef ASPOSE_GET_SHARED_MEMBERS
177 virtual ASPOSECPP_SHARED_API
void GetSharedMembers(shared_members_type&)
const;
183 virtual ASPOSECPP_SHARED_API
bool FastCast(
const Details::FastRttiBase& helper,
void** out_ptr)
const;
187 friend struct ::System::Details::DbgTools;
190 System::Details::DbgTools::stack_s_int_string_t m_personal_st;
192 bool m_rec_personal_st;
194 mutable int m_instanceNo;
197#if defined(ENABLE_CYCLES_DETECTION_EXT)
199 friend class System::Debug::ExternalObjectCollector;
200 unsigned m_instance_id;
203#ifdef __DBG_FOR_EACH_MEMBER
207 virtual ASPOSECPP_SHARED_API
void DBG_for_each_member(DBG::for_each_member_visitor &visitor)
const;
210 virtual const char* DBG_class_name()
const {
return "Object"; }
214 friend class ::System::Detail::SmartPtrCounter;
215 friend class ::System::Details::LeakageDetector;
217#ifdef ENABLE_EXTERNAL_REFCOUNT
219 std::unique_ptr<Detail::SmartPtrCounter> m_external_count;
222 std::atomic<Detail::SmartPtrCounter*> m_external_count;
224 Detail::RefCount m_shared_count;
236 ThisTypeInfo(
const ThisTypeInfo&) =
delete;
237 ThisTypeInfo(ThisTypeInfo&&) =
delete;
238 ThisTypeInfo& operator = (
const ThisTypeInfo&) =
delete;
239 ThisTypeInfo& operator = (ThisTypeInfo&&) =
delete;
243 typedef std::recursive_mutex mutex;
245 class GlobalMutexHolder;
247 static GlobalMutexHolder s_globalMutex;
249 mutex*
volatile m_objectMutex;
251#ifdef ASPOSECPP_DEBUG
253 mutable const std::string* m_currentConstructor;
256#ifdef __DBG_GARBAGE_COLLECTION
259 friend bool ASPOSECPP_SHARED_API System::Details::__buildIsolationIsland(Object *&last, std::map<const Object*, shared_members_type> &island,
int generation);
269inline bool Object::Equals<float, float>(
float const& objA,
float const& objB)
271 return (std::isnan(objA) && std::isnan(objB)) ? true : objA == objB;
280inline bool Object::Equals<double, double>(
double const& objA,
double const& objB)
282 return (std::isnan(objA) && std::isnan(objB)) ? true : objA == objB;
288#ifdef ENABLE_EXTERNAL_REFCOUNT
299#ifdef ENABLE_EXTERNAL_REFCOUNT
302 return --m_shared_count;
309#ifdef ENABLE_EXTERNAL_REFCOUNT
310 return m_external_count->DecreaseSharedRef(count);
312 return (m_shared_count -= count);
331#ifdef ENABLE_EXTERNAL_REFCOUNT
332 return m_external_count.get();
334 Detail::SmartPtrCounter *counter = m_external_count.load();
335 if (counter ==
nullptr)
337 std::unique_ptr<Detail::SmartPtrCounter> new_counter(
new Detail::SmartPtrCounter(
this));
338 if (m_external_count.compare_exchange_strong(counter, new_counter.get()))
339 return new_counter.release();
348#ifdef ENABLE_EXTERNAL_REFCOUNT
349 return m_external_count->SharedCount();
351 return m_shared_count;
359 [[noreturn]] ASPOSECPP_SHARED_API
void ThrowInvalidCastException();
363 template <
typename T>
364 T ThrowInvalidCastException_helper()
366 ThrowInvalidCastException();
372template <
typename TTo>
380template <
typename TTo>
389struct ExceptionWrapperType {};
394template <
typename T>
struct IsExceptionWrapper : std::is_base_of<System::ExceptionWrapperType, T> {};
401template<
typename TTo,
typename TFrom>
405 return obj.template dynamic_pointer_cast<TTo>();
414template<
typename TTo,
typename TFrom>
418 auto casted =
dynamic_cast<TTo*
>(obj.
get());
419 return obj && !casted
420 ? Details::ThrowInvalidCastException_helper<typename CastResult<TTo>::type>()
433template<
typename TTo,
typename TFrom>
434[[deprecated]]
typename std::enable_if<IsExceptionWrapper<TFrom>::value && IsExceptionWrapper<TTo>::value && (std::is_convertible<TTo, TFrom>::value || std::is_base_of<TTo, TFrom>::value), TTo>::type
438 (DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
451template<
typename TTo,
typename TFrom>
452[[deprecated]]
typename std::enable_if<IsExceptionWrapper<TFrom>::value && IsExceptionWrapper<TTo>::value && (std::is_convertible<TTo, TFrom>::value || std::is_base_of<TTo, TFrom>::value), TTo>::type
457 ? ((DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
459 : Details::ThrowInvalidCastException_helper<TTo>())
472template<
typename TTo,
typename TFrom>
473[[deprecated]]
typename std::enable_if<!IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
476 return Cast_noexcept<TTo, TFrom>(obj);
486template<
typename TTo,
typename TFrom>
487[[deprecated]]
typename std::enable_if<!IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
490 return Cast<TTo, TFrom>(obj);
499template<
typename TTo,
typename TFrom>
502 return Cast_noexcept<TTo, TFrom>(obj);
512template<
typename TTo,
typename TFrom>
522template<
typename TTo>
530template<
typename TTo,
typename TFrom>
531[[deprecated]]
typename std::enable_if<std::is_arithmetic<TFrom>::value, TTo>::type
StaticCast(TFrom value)
537template<
typename TTo>
538[[deprecated]]
typename std::enable_if<std::is_same<TTo, System::String>::value, TTo>::type
StaticCast(TTo value)
544template<
typename TTo,
typename TFrom>
545[[deprecated]]
typename std::enable_if<std::is_arithmetic<TFrom>::value, TTo>::type
StaticCast(
const TFrom* value)
557template<
typename TTo,
typename TFrom>
561 if (!obj.Is(TTo::Type()))
562 Details::ThrowInvalidCastException();
563 return static_cast<TTo&
>(obj);
574template<
typename TTo,
typename TFrom>
575[[deprecated]]
typename std::enable_if<!IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
578 return Cast_noexcept<TTo, TFrom>(obj);
587template<
typename TTo,
typename TFrom>
588[[deprecated]]
typename std::enable_if<IsExceptionWrapper<TFrom>::value && IsExceptionWrapper<TTo>::value && (std::is_convertible<TTo, TFrom>::value || std::is_base_of<TTo, TFrom>::value), TTo>::type
592 (DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
605template<
typename TTo,
typename TFrom>
606[[deprecated]]
typename std::enable_if<IsExceptionWrapper<TFrom>::value && IsExceptionWrapper<TTo>::value && (std::is_convertible<TTo, TFrom>::value || std::is_base_of<TTo, TFrom>::value), TTo>::type
611 ? ((DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
613 : Details::ThrowInvalidCastException_helper<TTo>())
626template<
typename TTo,
typename TFrom>
627[[deprecated]]
typename std::enable_if<!std::is_enum<TTo>::value && !IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
630 return Cast<TTo, TFrom>(obj);
638 ASPOSECPP_SHARED_API uint64_t GetIEnumUnsignedLongLongValue(
const SmartPtr<Object>& obj);
648template<
typename TTo,
typename TFrom>
651 return static_cast<TTo
>(Detail::GetIEnumUnsignedLongLongValue(obj));
658template<
typename TTo>
671template<
typename TTo,
typename TFrom>
672[[deprecated]]
typename std::enable_if<
673 !IsExceptionWrapper<TFrom>::value && !IsSmartPtr<TFrom>::value && std::is_convertible<TTo, TFrom>::value
676 return *
dynamic_cast<TTo*
>(&obj);
685template<
typename TTo,
typename TFrom>
686[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
689 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
690 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : TTo(
nullptr);
700template<
typename TTo,
typename TFrom>
701[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
707 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
708 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : Details::ThrowInvalidCastException_helper<TTo>();
717template<
typename TTo,
typename TFrom>
718[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
721 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
722 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : TTo(
nullptr);
732template <
typename TTo,
typename TFrom>
733[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
739 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
740 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : Details::ThrowInvalidCastException_helper<TTo>();
749template<
typename TTo,
typename TFrom>
750[[deprecated]]
typename std::enable_if<std::is_pointer<TTo>::value && std::is_same<IntPtr, TFrom>::value, TTo>::type
DynamicCast(TFrom value)
noexcept
752 return reinterpret_cast<TTo
>(value);
763template<
typename TTo,
typename TFrom>
766 return obj->
Is(TTo::Type()) ? System::const_pointer_cast<TTo>(obj) :
SmartPtr<TTo>();
774template <
typename TTo,
typename TFrom>
787 return MakeObject<T>(*ptr);
796template<
typename T,
typename A>
799 auto result = MakeSharedPtr<T>(
static_cast<T*
>(record->_Clone_()));
800 initializer(*result);
810template <
typename T,
typename A>
811T
With(
const T& record,
const A& initializer)
813 auto result = record;
819 namespace CompilerServices {
831 return obj->Object::GetHashCode();
840#ifdef ENABLE_EXTERNAL_REFCOUNT
841 return SharedRefAdded();
843 return m_object->SharedRefAdded();
858 if (
Object *
const object = GetObjectOrNull())
859 object->SetTemplateWeakPtr(argument);
869 return m_data.SharedGetObject()->Is(target);
871 return m_data.WeakGetCounter()->GetObject()->Is(target);
880 System::Details::MakeObjectLeakageDetector::ReportLeakCycle(count);
882#ifdef ENABLE_EXTERNAL_REFCOUNT
883 if (releaser->GetOwnershipState() != System::Detail::SmartPtrCounter::ObjectOwnershipState::OwnedByPointers)
886 Object *
const object = releaser->GetObject();
888 Object *
const&
object = releaser;
901 template <
unsigned int leaf,
unsigned int ...leafs>
905 WeakLeafMaker<leafs...>::Apply(ptr);
929#include <system/details/this_protector.h>
930#include <system/details/for_each_member_visitor.h>
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:51
Object * SharedRefAdded()
Increments shared reference count. Shouldn't be called directly; instead, use smart pointers or ThisP...
Definition: object.h:286
virtual int32_t GetHashCode() const
Analog of C# Object.GetHashCode() method. Enables hashing of custom objects.
void Lock()
Implements C# lock() statement locking. Call directly or use LockContext sentry object.
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals(T const &objA, std::nullptr_t)
Reference-compares value type object with nullptr.
Definition: object.h:123
virtual const TypeInfo & GetType() const
Gets actual type of object. Analog of C# System.Object.GetType() call.
virtual bool Is(const TypeInfo &targetType) const
Check if object represents an instance of type described by targetType. Analog of C# 'is' operator.
virtual bool Equals(ptr obj)
Compares objects using C# Object.Equals semantics.
void WeakRefRemoved()
Decrements weak reference count. Shouldn't be called directly; instead, use smart pointers or ThisPro...
Definition: object.h:323
Detail::SmartPtrCounter * GetCounter()
Gets reference counter data structure associated with the object.
Definition: object.h:329
static bool ReferenceEquals(ptr const &objA, ptr const &objB)
Compares objects by reference.
Definition: object.h:104
Object()
Creates object. Initializes all internal data structures.
virtual String ToString() const
Analog of C# Object.ToString() method. Enables converting custom objects to string.
virtual ptr MemberwiseClone() const
Analog of C# Object.MemberwiseClone() method. Enables cloning custom types.
Detail::SmartPtrCounter * WeakRefAdded()
Increments weak reference count. Shouldn't be called directly; instead, use smart pointers or ThisPro...
Definition: object.h:317
static std::enable_if< IsSmartPtr< T1 >::value &&IsSmartPtr< T2 >::value, bool >::type Equals(T1 const &objA, T2 const &objB)
Compares reference type objects in C# style.
Definition: object.h:134
void Unlock()
Implements C# lock() statement unlocking. Call directly or use LockContext sentry object.
static std::enable_if<!IsSmartPtr< T1 >::value &&!IsSmartPtr< T2 >::value, bool >::type Equals(T1 const &objA, T2 const &objB)
Compares value type objects in C# style.
Definition: object.h:145
int RemovedSharedRefs(int count)
Decreases shared reference count by specified value.
Definition: object.h:307
int SharedCount() const
Gets current value of shared reference counter.
Definition: object.h:346
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals(T const &objA, T const &objB)
Compares objects by reference.
Definition: object.h:114
virtual ~Object()
Destroys object. Frees all internal data structures.
static const TypeInfo & Type()
Implements C# typeof(System.Object) construct.
Definition: object.h:161
int SharedRefRemovedSafe()
Decrements and returns shared reference count. Shouldn't be called directly; instead,...
Definition: object.h:297
Object(Object const &x)
Copy constructor. Doesn't copy anything, really, just initializes new object and enables copy constru...
virtual bool FastCast(const Details::FastRttiBase &helper, void **out_ptr) const
For internal purposes only.
SmartPtr< Object > ptr
Alias for smart pointer type.
Definition: object.h:54
virtual void SetTemplateWeakPtr(uint32_t argument)
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Helper class to get some object information in runtime.
Definition: object.h:822
static int GetHashCode(SmartPtr< T > const &obj)
Gets hash code on arbitrary type. Calls Object::GetHashCode() to do so.
Definition: object.h:829
SmartPtr< Object > ToObjectPtr() const
Converts any pointer type to pointer to Object. Doesn't require Pointee_ type to be complete.
Definition: object.h:849
Pointee_ * get() const
Gets pointed object.
Definition: smart_ptr.h:518
void SetContainedTemplateWeakPtr(uint32_t argument) const
Calls SetTemplateWeakPtr() method on pointed object (if any).
Definition: object.h:856
bool Is(const System::TypeInfo &target) const
Checks if pointed object is of specific type or its child type. Follows C# 'is' semantics.
Definition: object.h:864
SmartPtrMode get_Mode() const
Gets pointer mode.
Definition: smart_ptr.h:543
Object * ReleaseSharedAndGetObjectToDelete()
Decrements currently referenced object's shared pointer counter.
Definition: smart_ptr.h:1033
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:122
Represents a particular type and provides information about it.
Definition: type_info.h:109
Subclass of System::SmartPtr which sets itself to weak mode at construction. Please note that this cl...
Definition: weak_ptr.h:18
Definition: db_command.h:9
void PrintTo(DateTime value, std::ostream *stream)
Prints value to ostream. Mostly used for debug.
@ Shared
Shared mode: pointer participates in reference counting.
CastResult< TTo >::type ConstCast(const SmartPtr< TFrom > &obj)
End of deprecated casts.
Definition: object.h:764
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type StaticCast(SmartPtr< TFrom > const &obj)
Performs static cast on SmartPtr objects.
Definition: object.h:488
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type Cast(SmartPtr< TFrom > const &obj)
Performs cast on SmartPtr objects.
Definition: object.h:416
std::enable_if< IsExceptionWrapper< TFrom >::value &&IsExceptionWrapper< TTo >::value &&(std::is_convertible< TTo, TFrom >::value||std::is_base_of< TTo, TFrom >::value), TTo >::type DynamicCast_noexcept(const TFrom &obj) noexcept
Old obsolete casts. Will be removed in future versions.
Definition: object.h:435
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type StaticCast_noexcept(SmartPtr< TFrom > const &obj)
Performs static cast on SmartPtr objects.
Definition: object.h:474
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type Cast_noexcept(SmartPtr< TFrom > const &obj)
Performs cast on SmartPtr objects.
Definition: object.h:403
CastResult< TTo >::type ForceStaticCast(SmartPtr< TFrom > const &obj)
Performs real static cast on SmartPtr objects.
Definition: object.h:775
SharedPtr< T > With(const SharedPtr< T > &record, const A &initializer)
Clones reference record and applies initializer functor to it.
Definition: object.h:797
SmartPtr< Object > MemberwiseClone(T *ptr)
Performs memberwise cloning using copy constructor.
Definition: object.h:785
std::ostream & operator<<(std::ostream &stream, DateTime date_time)
Insert data into the stream using UTF-8 encoding.
Definition: date_time.h:729
std::enable_if< IsExceptionWrapper< TFrom >::value &&IsExceptionWrapper< TTo >::value &&(std::is_convertible< TTo, TFrom >::value||std::is_base_of< TTo, TFrom >::value), TTo >::type DynamicCast(const TFrom &obj)
Performs dynamic cast on Exception objects.
Definition: object.h:453
Template magic to deduce cast results.
Definition: object.h:374
SmartPtr< TTo > type
An alias for a pointer to an instance of TTo.
Definition: object.h:376
A template predicate that determines if the specified type is a Exception class or its descendant.
Definition: object.h:394
Trait class to check if a type is a specialization of SmartPtr class.
Definition: smart_ptr.h:1499
Wrapper for a pointer to an instance of TypeInfo class. This type should be allocated on stack and pa...
Definition: type_info.h:72