Quick.Value.RTTI.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 : 26/06/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. function AsInterfaceEx<T : IInterface> : T; overload;
  57. end;
  58. implementation
  59. { TRTTIFlexValue }
  60. function TRTTIFlexValue.AsArray<T>: TArray<T>;
  61. begin
  62. if DataType <> dtArray then raise Exception.Create('DataType not supported');
  63. Result := (Self.Data as IValueTValue).Value.AsType<TArray<T>>;
  64. end;
  65. function TRTTIFlexValue.AsInterfaceEx<T>: T;
  66. begin
  67. Result := T(Self.Data);
  68. end;
  69. function TRTTIFlexValue.AsRecord<T>: T;
  70. begin
  71. if DataType <> dtRecord then raise Exception.Create('DataType not supported');
  72. Result := (Self.Data as IValueTValue).Value.AsType<T>;
  73. end;
  74. function TRTTIFlexValue.AsType<T>: T;
  75. begin
  76. Result := T(AsObject);
  77. end;
  78. function TRTTIFlexValue.CastToTValue: TValue;
  79. begin
  80. try
  81. case DataType of
  82. dtNull : Result := TValueExtended;
  83. dtBoolean : Result := AsBoolean;
  84. dtString : Result := AsString;
  85. {$IFDEF MSWINDOWS}
  86. dtAnsiString : Result := string(AsAnsiString);
  87. dtWideString : Result := AsWideString;
  88. {$ENDIF}
  89. dtInteger,
  90. dtInt64 : Result := AsInt64;
  91. {$IFNDEF FPC}
  92. dtVariant : Result := TValue.FromVariant(AsVariant);
  93. dtInterface : Result := TValue.FromVariant(AsInterface);
  94. {$ENDIF}
  95. dtObject : Result := AsObject;
  96. dtArray : Result := (Self.Data as IValueTValue).Value;
  97. else raise Exception.Create('DataType not supported');
  98. end;
  99. except
  100. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TValue error: %s',[e.message]);
  101. end;
  102. end;
  103. procedure TRTTIFlexValue.FromArray<T>(aArray: TArray<T>);
  104. var
  105. value : TValue;
  106. begin
  107. TValue.Make(@aArray,TypeInfo(T),value);
  108. Self.SetAsCustom(TValueTValue.Create(value),TValueDataType.dtArray);
  109. end;
  110. procedure TRTTIFlexValue.FromRecord<T>(aRecord : T);
  111. var
  112. value : TValue;
  113. begin
  114. TValue.Make(@aRecord,TypeInfo(T),value);
  115. Self.SetAsCustom(TValueTValue.Create(value),TValueDataType.dtRecord);
  116. end;
  117. procedure TRTTIFlexValue.SetAsTValue(const Value: TValue);
  118. begin
  119. Clear;
  120. case Value.Kind of
  121. tkInteger,
  122. tkInt64 : AsInt64 := Value.AsInt64;
  123. tkFloat : AsExtended := Value.AsExtended;
  124. tkChar,
  125. {$IFNDEF FPC}
  126. tkString,
  127. tkUstring,
  128. {$ELSE}
  129. tkAstring,
  130. {$ENDIF}
  131. tkWideString,
  132. tkWideChar : AsString := Value.AsString;
  133. tkEnumeration,
  134. tkSet : AsInteger := Value.AsInteger;
  135. tkClass : AsObject := Value.AsObject;
  136. tkInterface : AsInterface := Value.AsInterface;
  137. {$IFNDEF FPC}
  138. tkArray,
  139. tkDynArray : Self.SetAsCustom(TValueTValue.Create(Value),TValueDataType.dtArray);
  140. tkRecord : Self.SetAsCustom(TValueTValue.Create(Value),TValueDataType.dtRecord);
  141. else AsVariant := Value.AsVariant;
  142. {$ENDIF}
  143. end;
  144. end;
  145. { TValueTValue }
  146. constructor TValueTValue.Create(const Value: TValue);
  147. begin
  148. fData := Value;
  149. end;
  150. function TValueTValue.GetValue: TValue;
  151. begin
  152. Result := fData;
  153. end;
  154. procedure TValueTValue.SetValue(const Value: TValue);
  155. begin
  156. fData := Value;
  157. end;
  158. end.