Browse Source

+ add TValue.DataSize property

git-svn-id: trunk@36965 -
svenbarth 8 years ago
parent
commit
cd73a46313
1 changed files with 85 additions and 0 deletions
  1. 85 0
      packages/rtl-objpas/src/inc/rtti.pp

+ 85 - 0
packages/rtl-objpas/src/inc/rtti.pp

@@ -65,6 +65,7 @@ type
   TValue = record
   private
     FData: TValueData;
+    function GetDataSize: SizeInt;
     function GetTypeDataProp: PTypeData; inline;
     function GetTypeInfo: PTypeInfo; inline;
     function GetTypeKind: TTypeKind; inline;
@@ -87,6 +88,7 @@ type
     function ToString: String;
     function IsType(ATypeInfo: PTypeInfo): boolean; inline;
     function TryAsOrdinal(out AResult: int64): boolean;
+    property DataSize: SizeInt read GetDataSize;
     property Kind: TTypeKind read GetTypeKind;
     property TypeData: PTypeData read GetTypeDataProp;
     property TypeInfo: PTypeInfo read GetTypeInfo;
@@ -573,6 +575,89 @@ begin
   result := GetTypeData(FData.FTypeInfo);
 end;
 
+function TValue.GetDataSize: SizeInt;
+begin
+  if IsEmpty then
+    Result := 0
+  else if Assigned(FData.FValueData) then
+    Result := FData.FValueData.GetDataSize
+  else begin
+    Result := 0;
+    case Kind of
+      tkEnumeration,
+      tkBool,
+      tkInt64,
+      tkQWord,
+      tkInteger:
+        case TypeData^.OrdType of
+          otSByte,
+          otUByte:
+            Result := SizeOf(Byte);
+          otSWord,
+          otUWord:
+            Result := SizeOf(Word);
+          otSLong,
+          otULong:
+            Result := SizeOf(LongWord);
+          otSQWord,
+          otUQWord:
+            Result := SizeOf(QWord);
+        end;
+      tkChar:
+        Result := SizeOf(AnsiChar);
+      tkFloat:
+        case TypeData^.FloatType of
+          ftSingle:
+            Result := SizeOf(Single);
+          ftDouble:
+            Result := SizeOf(Double);
+          ftExtended:
+            Result := SizeOf(Extended);
+          ftComp:
+            Result := SizeOf(Comp);
+          ftCurr:
+            Result := SizeOf(Currency);
+        end;
+      tkSet:
+        { ToDo }
+        Result := 0;
+      tkMethod:
+        { ? }
+        Result := SizeOf(TMethod);
+      tkSString:
+        Result := SizeOf(ShortString);
+      tkVariant:
+        Result := SizeOf(Variant);
+      tkProcVar:
+        Result := SizeOf(CodePointer);
+      tkWChar:
+        Result := SizeOf(WideChar);
+      tkUChar:
+        Result := SizeOf(UnicodeChar);
+      tkFile:
+        { ToDo }
+        Result := SizeOf(TTextRec);
+      tkClass,
+      tkHelper,
+      tkClassRef,
+      tkPointer:
+        Result := SizeOf(Pointer);
+      tkUnknown,
+      tkLString,
+      tkAString,
+      tkWString,
+      tkUString,
+      tkObject,
+      tkArray,
+      tkRecord,
+      tkInterface,
+      tkDynArray,
+      tkInterfaceRaw:
+        Assert(False);
+    end;
+  end;
+end;
+
 function TValue.GetTypeInfo: PTypeInfo;
 begin
   result := FData.FTypeInfo;