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>
29#ifdef ASPOSECPPLIB_BEING_BUILT
30#include <boost/thread/recursive_mutex.hpp>
35 ASPOSECPP_3RD_PARTY_CLASS(recursive_mutex);
43class Details_Exception;
66#ifdef ASPOSE_GET_SHARED_MEMBERS
68 typedef System::Details::SharedMembersType shared_members_type;
81 Object& operator = (
Object const& x){ ASPOSE_UNUSED(x);
return *
this; }
87 int SharedRefRemovedSafe();
91 int RemovedSharedRefs(
int count);
94 Detail::SmartPtrCounter* WeakRefAdded();
96 void WeakRefRemoved();
99 inline Detail::SmartPtrCounter* GetCounter();
102 int SharedCount()
const;
104 ASPOSECPP_SHARED_API
void Lock();
125 static typename std::enable_if<!IsSmartPtr<T>::value,
bool>::type
ReferenceEquals(T
const& objA, T
const& objB)
127 return &objA == &objB;
134 static typename std::enable_if<!IsSmartPtr<T>::value,
bool>::type
ReferenceEquals(T
const& objA, std::nullptr_t)
144 template<
typename T1,
typename T2>
147 return ReferenceEquals(objA, objB) || (objA !=
nullptr && objB !=
nullptr && objA->Equals(objB));
155 template<
typename T1,
typename T2>
174 return *static_holder<ThisTypeInfo>();
182 virtual ASPOSECPP_SHARED_API
bool Is(
const TypeInfo& targetType)
const;
186#ifdef ASPOSE_GET_SHARED_MEMBERS
188 virtual ASPOSECPP_SHARED_API
void GetSharedMembers(shared_members_type&)
const;
194 virtual ASPOSECPP_SHARED_API
bool FastCast(
const Details::FastRttiBase& helper,
void** out_ptr)
const;
198 friend struct ::System::Details::DbgTools;
201 System::Details::DbgTools::stack_s_int_string_t m_personal_st;
203 bool m_rec_personal_st;
205 mutable int m_instanceNo;
208#if defined(ENABLE_CYCLES_DETECTION_EXT)
210 friend class System::Debug::ExternalObjectCollector;
211 unsigned m_instance_id;
214#ifdef __DBG_FOR_EACH_MEMBER
218 virtual ASPOSECPP_SHARED_API
void DBG_for_each_member(DBG::for_each_member_visitor &visitor)
const;
221 virtual const char* DBG_class_name()
const {
return "Object"; }
225 friend class ::System::Detail::SmartPtrCounter;
226 friend class ::System::Details::LeakageDetector;
228#ifdef ENABLE_EXTERNAL_REFCOUNT
230 std::unique_ptr<Detail::SmartPtrCounter> m_external_count;
233 std::atomic<Detail::SmartPtrCounter*> m_external_count;
235 Detail::RefCount m_shared_count;
247 ThisTypeInfo(
const ThisTypeInfo&) =
delete;
248 ThisTypeInfo(ThisTypeInfo&&) =
delete;
249 ThisTypeInfo& operator = (
const ThisTypeInfo&) =
delete;
250 ThisTypeInfo& operator = (ThisTypeInfo&&) =
delete;
254 typedef boost::recursive_mutex mutex;
256 class GlobalMutexHolder;
258 static GlobalMutexHolder s_globalMutex;
260 mutex*
volatile m_objectMutex;
262#ifdef ASPOSECPP_DEBUG
264 mutable const std::string* m_currentConstructor;
267#ifdef __DBG_GARBAGE_COLLECTION
270 friend bool ASPOSECPP_SHARED_API System::Details::__buildIsolationIsland(Object *&last, std::map<const Object*, shared_members_type> &island,
int generation);
280inline bool Object::Equals<float, float>(
float const& objA,
float const& objB)
282 return (std::isnan(objA) && std::isnan(objB)) ? true : objA == objB;
291inline bool Object::Equals<double, double>(
double const& objA,
double const& objB)
293 return (std::isnan(objA) && std::isnan(objB)) ? true : objA == objB;
299#ifdef ENABLE_EXTERNAL_REFCOUNT
310#ifdef ENABLE_EXTERNAL_REFCOUNT
313 return --m_shared_count;
320#ifdef ENABLE_EXTERNAL_REFCOUNT
321 return m_external_count->DecreaseSharedRef(count);
323 return (m_shared_count -= count);
342#ifdef ENABLE_EXTERNAL_REFCOUNT
343 return m_external_count.get();
345 Detail::SmartPtrCounter *counter = m_external_count.load();
346 if (counter ==
nullptr)
348 std::unique_ptr<Detail::SmartPtrCounter> new_counter(
new Detail::SmartPtrCounter(
this));
349 if (m_external_count.compare_exchange_strong(counter, new_counter.get()))
350 return new_counter.release();
359#ifdef ENABLE_EXTERNAL_REFCOUNT
360 return m_external_count->SharedCount();
362 return m_shared_count;
370 [[noreturn]] ASPOSECPP_SHARED_API
void ThrowInvalidCastException();
374 template <
typename T>
375 T ThrowInvalidCastException_helper()
377 ThrowInvalidCastException();
383template <
typename TTo>
391template <
typename TTo>
400struct ExceptionWrapperType {};
405template <
typename T>
struct IsExceptionWrapper : std::is_base_of<System::ExceptionWrapperType, T> {};
412template<
typename TTo,
typename TFrom>
416 return obj.template dynamic_pointer_cast<TTo>();
425template<
typename TTo,
typename TFrom>
429 auto casted =
dynamic_cast<TTo*
>(obj.
get());
430 return obj && !casted
431 ? Details::ThrowInvalidCastException_helper<typename CastResult<TTo>::type>()
444template<
typename TTo,
typename TFrom>
445[[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
449 (DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
462template<
typename TTo,
typename TFrom>
463[[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
468 ? ((DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
470 : Details::ThrowInvalidCastException_helper<TTo>())
483template<
typename TTo,
typename TFrom>
484[[deprecated]]
typename std::enable_if<!IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
487 return Cast_noexcept<TTo, TFrom>(obj);
497template<
typename TTo,
typename TFrom>
498[[deprecated]]
typename std::enable_if<!IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
501 return Cast<TTo, TFrom>(obj);
510template<
typename TTo,
typename TFrom>
513 return Cast_noexcept<TTo, TFrom>(obj);
523template<
typename TTo,
typename TFrom>
533template<
typename TTo>
541template<
typename TTo,
typename TFrom>
542[[deprecated]]
typename std::enable_if<std::is_arithmetic<TFrom>::value, TTo>::type
StaticCast(TFrom value)
548template<
typename TTo>
549[[deprecated]]
typename std::enable_if<std::is_same<TTo, System::String>::value, TTo>::type
StaticCast(TTo value)
555template<
typename TTo,
typename TFrom>
556[[deprecated]]
typename std::enable_if<std::is_arithmetic<TFrom>::value, TTo>::type
StaticCast(
const TFrom* value)
568template<
typename TTo,
typename TFrom>
572 if (!obj.Is(TTo::Type()))
573 Details::ThrowInvalidCastException();
574 return static_cast<TTo&
>(obj);
585template<
typename TTo,
typename TFrom>
586[[deprecated]]
typename std::enable_if<!IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
589 return Cast_noexcept<TTo, TFrom>(obj);
598template<
typename TTo,
typename TFrom>
599[[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
603 (DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
616template<
typename TTo,
typename TFrom>
617[[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
622 ? ((DynamicCast_noexcept<typename TTo::ExceptionType>(obj.Get()) !=
nullptr)
624 : Details::ThrowInvalidCastException_helper<TTo>())
637template<
typename TTo,
typename TFrom>
638[[deprecated]]
typename std::enable_if<!std::is_enum<TTo>::value && !IsExceptionWrapper<TTo>::value,
typename CastResult<TTo>::type>::type
641 return Cast<TTo, TFrom>(obj);
649 ASPOSECPP_SHARED_API uint64_t GetIEnumUnsignedLongLongValue(
const SmartPtr<Object>& obj);
659template<
typename TTo,
typename TFrom>
662 return static_cast<TTo
>(Detail::GetIEnumUnsignedLongLongValue(obj));
669template<
typename TTo>
682template<
typename TTo,
typename TFrom>
683[[deprecated]]
typename std::enable_if<
684 !IsExceptionWrapper<TFrom>::value && !IsSmartPtr<TFrom>::value && std::is_convertible<TTo, TFrom>::value
687 return *
dynamic_cast<TTo*
>(&obj);
696template<
typename TTo,
typename TFrom>
697[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
700 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
701 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : TTo(
nullptr);
711template<
typename TTo,
typename TFrom>
712[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
718 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
719 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : Details::ThrowInvalidCastException_helper<TTo>();
728template<
typename TTo,
typename TFrom>
729[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
732 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
733 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : TTo(
nullptr);
743template <
typename TTo,
typename TFrom>
744[[deprecated]]
typename std::enable_if<std::is_same<System::Object, TFrom>::value && IsExceptionWrapper<TTo>::value, TTo>::type
750 auto exception_ptr = DynamicCast_noexcept<typename TTo::ExceptionType>(obj);
751 return exception_ptr !=
nullptr ? TTo(StaticCast_noexcept<System::Details_Exception>(exception_ptr)) : Details::ThrowInvalidCastException_helper<TTo>();
760template<
typename TTo,
typename TFrom>
761[[deprecated]]
typename std::enable_if<std::is_pointer<TTo>::value && std::is_same<IntPtr, TFrom>::value, TTo>::type
DynamicCast(TFrom value)
noexcept
763 return reinterpret_cast<TTo
>(value);
774template<
typename TTo,
typename TFrom>
777 return obj->
Is(TTo::Type()) ? System::const_pointer_cast<TTo>(obj) :
SmartPtr<TTo>();
785template <
typename TTo,
typename TFrom>
798 return MakeObject<T>(*ptr);
802 namespace CompilerServices {
814 return obj->Object::GetHashCode();
823#ifdef ENABLE_EXTERNAL_REFCOUNT
824 return SharedRefAdded();
826 return m_object->SharedRefAdded();
841 if (
Object *
const object = GetObjectOrNull())
842 object->SetTemplateWeakPtr(argument);
852 return m_data.SharedGetObject()->Is(target);
854 return m_data.WeakGetCounter()->GetObject()->Is(target);
863 System::Details::MakeObjectLeakageDetector::ReportLeakCycle(count);
865#ifdef ENABLE_EXTERNAL_REFCOUNT
866 if (releaser->GetOwnershipState() != System::Detail::SmartPtrCounter::ObjectOwnershipState::OwnedByPointers)
869 Object *
const object = releaser->GetObject();
871 Object *
const&
object = releaser;
884 template <
unsigned int leaf,
unsigned int ...leafs>
888 WeakLeafMaker<leafs...>::Apply(ptr);
912#include <system/details/this_protector.h>
913#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:62
Object * SharedRefAdded()
Increments shared reference count. Shouldn't be called directly; instead, use smart pointers or ThisP...
Definition: object.h:297
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:134
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:334
Detail::SmartPtrCounter * GetCounter()
Gets reference counter data structure associated with the object.
Definition: object.h:340
static bool ReferenceEquals(ptr const &objA, ptr const &objB)
Compares objects by reference.
Definition: object.h:115
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:328
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:145
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:156
int RemovedSharedRefs(int count)
Decreases shared reference count by specified value.
Definition: object.h:318
int SharedCount() const
Gets current value of shared reference counter.
Definition: object.h:357
static std::enable_if<!IsSmartPtr< T >::value, bool >::type ReferenceEquals(T const &objA, T const &objB)
Compares objects by reference.
Definition: object.h:125
virtual ~Object()
Destroys object. Frees all internal data structures.
static const TypeInfo & Type()
Implements C# typeof(System.Object) construct.
Definition: object.h:172
int SharedRefRemovedSafe()
Decrements and returns shared reference count. Shouldn't be called directly; instead,...
Definition: object.h:308
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:65
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:805
static int GetHashCode(SmartPtr< T > const &obj)
Gets hash code on arbitrary type. Calls Object::GetHashCode() to do so.
Definition: object.h:812
SmartPtr< Object > ToObjectPtr() const
Converts any pointer type to pointer to Object. Doesn't require Pointee_ type to be complete.
Definition: object.h:832
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:839
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:847
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:775
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type StaticCast(SmartPtr< TFrom > const &obj)
Performs static cast on SmartPtr objects.
Definition: object.h:499
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type Cast(SmartPtr< TFrom > const &obj)
Performs cast on SmartPtr objects.
Definition: object.h:427
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:446
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:485
std::enable_if<!IsExceptionWrapper< TTo >::value, typenameCastResult< TTo >::type >::type Cast_noexcept(SmartPtr< TFrom > const &obj)
Performs cast on SmartPtr objects.
Definition: object.h:414
CastResult< TTo >::type ForceStaticCast(SmartPtr< TFrom > const &obj)
Performs real static cast on SmartPtr objects.
Definition: object.h:786
SmartPtr< Object > MemberwiseClone(T *ptr)
Performs memberwise cloning using copy constructor.
Definition: object.h:796
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:464
Template magic to deduce cast results.
Definition: object.h:385
SmartPtr< TTo > type
An alias for a pointer to an instance of TTo.
Definition: object.h:387
A template predicate that determines if the specified type is a Exception class or its descendant.
Definition: object.h:405
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