1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-18 16:10:26 +02:00
Beef/BeefLibs/corlib/src/Object.bf

180 lines
3.9 KiB
Beef
Raw Normal View History

2019-08-23 11:56:54 -07:00
using System;
using System.Reflection;
using System.Collections;
2019-08-23 11:56:54 -07:00
using System.Diagnostics;
namespace System
{
class Object : IHashable
{
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
int mClassVData;
int mDbgAllocInfo;
#else
2019-08-23 11:56:54 -07:00
ClassVData* mClassVData;
#endif
[AlwaysInclude]
2019-08-23 11:56:54 -07:00
public virtual ~this()
{
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
mClassVData = ((mClassVData & ~0x08) | 0x80);
#endif
}
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
[NoShow]
int32 GetFlags()
2019-08-23 11:56:54 -07:00
{
return (int32)mClassVData & 0xFF;
}
[DisableObjectAccessChecks, NoShow]
public bool IsDeleted()
{
return (int32)mClassVData & 0x80 != 0;
}
#else
[SkipCall]
public bool IsDeleted()
{
return false;
}
#endif
extern Type Comptime_GetType();
2019-08-23 11:56:54 -07:00
public Type GetType()
{
if (Compiler.IsComptime)
return Comptime_GetType();
2020-12-19 14:19:33 -08:00
ClassVData* classVData;
2019-08-23 11:56:54 -07:00
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
classVData = (ClassVData*)(void*)(mClassVData & ~(int)0xFF);
#else
classVData = mClassVData;
#endif
#if BF_32_BIT
Type type = Type.[Friend]GetType_(classVData.mType2);
2019-08-23 11:56:54 -07:00
#else
Type type = Type.[Friend]GetType_((.)(classVData.mType >> 32));
2019-08-23 11:56:54 -07:00
#endif
return type;
}
TypeId GetTypeId()
{
ClassVData* classVData;
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
classVData = (ClassVData*)(void*)(mClassVData & ~(int)0xFF);
#else
classVData = mClassVData;
#endif
#if BF_32_BIT
return (.)classVData.mType2;
#else
return (.)(classVData.mType >> 32);
#endif
}
2019-08-23 11:56:54 -07:00
[NoShow]
Type RawGetType()
2019-08-23 11:56:54 -07:00
{
if (Compiler.IsComptime)
return Comptime_GetType();
2020-12-19 14:19:33 -08:00
ClassVData* classVData;
2019-08-23 11:56:54 -07:00
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
classVData = (ClassVData*)(void*)(mClassVData & ~(int)0xFF);
#else
classVData = mClassVData;
#endif
#if BF_32_BIT
Type type = Type.[Friend]GetType_(classVData.mType);
#else
Type type = Type.[Friend]GetType_((.)(classVData.mType & 0xFFFFFFFF));
#endif
return type;
2019-08-23 11:56:54 -07:00
}
TypeId RawGetTypeId()
{
ClassVData* classVData;
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
classVData = (ClassVData*)(void*)(mClassVData & ~(int)0xFF);
#else
classVData = mClassVData;
#endif
#if BF_32_BIT
return (.)classVData.mType;
#else
return (.)(classVData.mType & 0xFFFFFFFF);
#endif
}
2019-12-11 16:56:09 -08:00
#if BF_DYNAMIC_CAST_CHECK || BF_ENABLE_REALTIME_LEAK_CHECK
2019-08-23 11:56:54 -07:00
[NoShow]
public virtual Object DynamicCastToTypeId(int32 typeId)
{
if (typeId == (.)RawGetTypeId())
2019-08-23 11:56:54 -07:00
return this;
return null;
}
[NoShow]
public virtual Object DynamicCastToInterface(int32 typeId)
{
return null;
}
#endif
int IHashable.GetHashCode()
{
return (int)Internal.UnsafeCastToPtr(this);
2019-08-23 11:56:54 -07:00
}
2019-08-23 11:56:54 -07:00
public virtual void ToString(String strBuffer)
{
#if BF_REFLECT_MINIMAL
strBuffer.AppendF($"Type#{(Int.Simple)GetTypeId()}@0x");
NumberFormatter.AddrToString((uint)Internal.UnsafeCastToPtr(this), strBuffer);
#else
let t = RawGetType();
if (t.IsBoxedStructPtr)
{
let ti = (TypeInstance)t;
let innerPtr = *(void**)((uint8*)Internal.UnsafeCastToPtr(this) + ti.[Friend]mMemberDataOffset);
strBuffer.Append("(");
ti.UnderlyingType.GetFullName(strBuffer);
//strBuffer.AppendF("*)0x{0:A}", (UInt.Simple)(uint)(void*)innerPtr);
strBuffer.Append("*)0x");
NumberFormatter.AddrToString((uint)(void*)innerPtr, strBuffer);
return;
}
t.GetFullName(strBuffer);
strBuffer.Append("@0x");
NumberFormatter.AddrToString((uint)Internal.UnsafeCastToPtr(this), strBuffer);
#endif
2019-08-23 11:56:54 -07:00
}
2020-06-25 05:52:42 -07:00
private static void ToString(Object obj, String strBuffer)
{
if (obj == null)
strBuffer.Append("null");
else
obj.ToString(strBuffer);
}
2019-08-23 11:56:54 -07:00
[SkipCall, NoShow]
protected virtual void GCMarkMembers()
{
//PrintF("Object.GCMarkMembers %08X\n", this);
}
}
}