Quick.Value.RTTI.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. { ***************************************************************************
  2. Copyright (c) 2016-2020 Kike Pérez
  3. Unit : Quick.Value.RTTI
  4. Description : FlexValue Helper for RTTI
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 06/05/2019
  8. Modified : 09/04/2020
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Value.RTTI;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. SysUtils,
  26. Rtti,
  27. Quick.Value;
  28. type
  29. IValueTValue = interface
  30. ['{B109F5F2-32E5-4C4B-B83C-BF00BB69B2D0}']
  31. function GetValue : TValue;
  32. procedure SetValue(const Value : TValue);
  33. property Value : TValue read GetValue write SetValue;
  34. end;
  35. TValueTValue = class(TValueData,IValueTValue)
  36. strict private
  37. fData : TValue;
  38. private
  39. function GetValue : TValue;
  40. procedure SetValue(const Value : TValue);
  41. public
  42. constructor Create(const Value : TValue);
  43. property Value : TValue read GetValue write SetValue;
  44. end;
  45. TRTTIFlexValue = record helper for TFlexValue
  46. private
  47. function CastToTValue: TValue;
  48. procedure SetAsTValue(const Value: TValue);
  49. public
  50. property AsTValue : TValue read CastToTValue write SetAsTValue;
  51. procedure FromRecord<T : record>(aRecord : T);
  52. procedure FromArray<T>(aArray: TArray<T>);
  53. function AsType<T : class> : T;
  54. function AsRecord<T : record> : T;
  55. function AsArray<T> : TArray<T>;
  56. end;
  57. implementation
  58. { TRTTIFlexValue }
  59. function TRTTIFlexValue.AsArray<T>: TArray<T>;
  60. begin
  61. if DataType <> dtArray then raise Exception.Create('DataType not supported');
  62. Result := (Self.Data as IValueTValue).Value.AsType<TArray<T>>;
  63. end;
  64. function TRTTIFlexValue.AsRecord<T>: T;
  65. begin
  66. if DataType <> dtRecord then raise Exception.Create('DataType not supported');
  67. Result := (Self.Data as IValueTValue).Value.AsType<T>;
  68. end;
  69. function TRTTIFlexValue.AsType<T>: T;
  70. begin
  71. Result := T(AsObject);
  72. end;
  73. function TRTTIFlexValue.CastToTValue: TValue;
  74. begin
  75. try
  76. case DataType of
  77. dtNull : Result := TValueExtended;
  78. dtBoolean : Result := AsBoolean;
  79. dtString : Result := AsString;
  80. {$IFDEF MSWINDOWS}
  81. dtAnsiString : Result := AsAnsiString;
  82. dtWideString : Result := AsWideString;
  83. {$ENDIF}
  84. dtInteger,
  85. dtInt64 : Result := AsInt64;
  86. {$IFNDEF FPC}
  87. dtVariant : Result := TValue.FromVariant(AsVariant);
  88. dtInterface : Result := TValue.FromVariant(AsInterface);
  89. {$ENDIF}
  90. dtObject : Result := AsObject;
  91. dtArray : Result := (Self.Data as IValueTValue).Value;
  92. else raise Exception.Create('DataType not supported');
  93. end;
  94. except
  95. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TValue error: %s',[e.message]);
  96. end;
  97. end;
  98. procedure TRTTIFlexValue.FromArray<T>(aArray: TArray<T>);
  99. var
  100. value : TValue;
  101. begin
  102. TValue.Make(@aArray,TypeInfo(T),value);
  103. Self.SetAsCustom(TValueTValue.Create(value),TValueDataType.dtArray);
  104. end;
  105. procedure TRTTIFlexValue.FromRecord<T>(aRecord : T);
  106. var
  107. value : TValue;
  108. begin
  109. TValue.Make(@aRecord,TypeInfo(T),value);
  110. Self.SetAsCustom(TValueTValue.Create(value),TValueDataType.dtRecord);
  111. end;
  112. procedure TRTTIFlexValue.SetAsTValue(const Value: TValue);
  113. begin
  114. Clear;
  115. case Value.Kind of
  116. tkInteger,
  117. tkInt64 : AsInt64 := Value.AsInt64;
  118. tkFloat : AsExtended := Value.AsExtended;
  119. tkChar,
  120. {$IFNDEF FPC}
  121. tkString,
  122. tkUstring,
  123. {$ELSE}
  124. tkAstring,
  125. {$ENDIF}
  126. tkWideString,
  127. tkWideChar : AsString := Value.AsString;
  128. tkEnumeration,
  129. tkSet : AsInteger := Value.AsInteger;
  130. tkClass : AsObject := Value.AsObject;
  131. tkInterface : AsInterface := Value.AsInterface;
  132. {$IFNDEF FPC}
  133. tkArray,
  134. tkDynArray : Self.SetAsCustom(TValueTValue.Create(Value),TValueDataType.dtArray);
  135. tkRecord : Self.SetAsCustom(TValueTValue.Create(Value),TValueDataType.dtRecord);
  136. else AsVariant := Value.AsVariant;
  137. {$ENDIF}
  138. end;
  139. end;
  140. { TValueTValue }
  141. constructor TValueTValue.Create(const Value: TValue);
  142. begin
  143. fData := Value;
  144. end;
  145. function TValueTValue.GetValue: TValue;
  146. begin
  147. Result := fData;
  148. end;
  149. procedure TValueTValue.SetValue(const Value: TValue);
  150. begin
  151. fData := Value;
  152. end;
  153. end.