Browse Source

+ add implicit assignment operators from TDateTime, TDate and TTime to TValue so that the later will have the correct type information

Sven/Sarah Barth 1 day ago
parent
commit
1f3a013f8f
2 changed files with 54 additions and 0 deletions
  1. 18 0
      packages/rtl-objpas/src/inc/rtti.pp
  2. 36 0
      packages/rtl-objpas/tests/tests.rtti.value.pas

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

@@ -283,6 +283,9 @@ type
     class operator := (AValue: Boolean): TValue; inline;
     class operator := (AValue: IUnknown): TValue; inline;
     class operator := (AValue: TVarRec): TValue; inline;
+    class operator := (AValue: TDateTime): TValue; inline;
+    class operator := (AValue: TDate): TValue; inline;
+    class operator := (AValue: TTime): TValue; inline;
     property DataSize: SizeInt read GetDataSize;
     property Kind: TTypeKind read GetTypeKind;
     property TypeData: PTypeData read GetTypeDataProp;
@@ -2779,6 +2782,21 @@ begin
   Result:=TValue.FromVarRec(aValue);
 end;
 
+class operator TValue.:=(AValue: TDateTime): TValue;
+begin
+  Make(@AValue, System.TypeInfo(TDateTime), Result);
+end;
+
+class operator TValue.:=(AValue: TDate): TValue;
+begin
+  Make(@AValue, System.TypeInfo(TDate), Result);
+end;
+
+class operator TValue.:=(AValue: TTime): TValue;
+begin
+  Make(@AValue, System.TypeInfo(TTime), Result);
+end;
+
 function TValue.AsString: string;
 begin
   if System.GetTypeKind(String) = tkUString then

+ 36 - 0
packages/rtl-objpas/tests/tests.rtti.value.pas

@@ -18,6 +18,9 @@ Type
     procedure TestIsManaged;
     procedure TestCasts; 
     procedure TestAssignPointer;
+    procedure TestAssignDateTime;
+    procedure TestAssignDate;
+    procedure TestAssignTime;
   end;
 
   TTestValueSimple = Class(TTestCase)
@@ -1969,6 +1972,39 @@ begin
   AssertSame('Correct type info', TypeInfo(Pointer),V.TypeInfo);
 end;
 
+procedure TTestValueGeneral.TestAssignDateTime;
+var
+  dt: TDateTime;
+  v: TValue;
+begin
+  dt := Now;
+  v := dt;
+  AssertSame('Incorrect type info', TypeInfo(TDateTime), V.TypeInfo);
+  CheckEquals(dt, V.AsDateTime, 'Incorrect value');
+end;
+
+procedure TTestValueGeneral.TestAssignDate;
+var
+  d: TDate;
+  v: TValue;
+begin
+  d := Date;
+  v := d;
+  AssertSame('Incorrect type info', TypeInfo(TDate), V.TypeInfo);
+  CheckEquals(d, V.AsDateTime, 'Incorrect value');
+end;
+
+procedure TTestValueGeneral.TestAssignTime;
+var
+  t: TTime;
+  v: TValue;
+begin
+  t := Time;
+  v := t;
+  AssertSame('Incorrect type info', TypeInfo(TTime), V.TypeInfo);
+  CheckEquals(t, V.AsDateTime, 'Incorrect value');
+end;
+
 procedure TTestValueGeneral.TestReferenceRawData;
 var
   value: TValue;