Quick.Value.RTTI.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 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 : 30/08/2019
  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. function AsType<T : class> : T;
  52. end;
  53. implementation
  54. { TRTTIFlexValue }
  55. function TRTTIFlexValue.AsType<T>: T;
  56. begin
  57. Result := T(AsObject);
  58. end;
  59. function TRTTIFlexValue.CastToTValue: TValue;
  60. begin
  61. try
  62. case DataType of
  63. dtNull : Result := TValueExtended;
  64. dtBoolean : Result := AsBoolean;
  65. dtString : Result := AsString;
  66. {$IFDEF MSWINDOWS}
  67. dtAnsiString : Result := AsAnsiString;
  68. dtWideString : Result := AsWideString;
  69. {$ENDIF}
  70. dtInteger,
  71. dtInt64 : Result := AsInt64;
  72. {$IFNDEF FPC}
  73. dtVariant : Result := TValue.FromVariant(AsVariant);
  74. dtInterface : Result := TValue.FromVariant(AsInterface);
  75. {$ENDIF}
  76. dtObject : Result := AsObject;
  77. dtArray : Result := (Self.Data as IValueTValue).Value;
  78. else raise Exception.Create('DataType not supported');
  79. end;
  80. except
  81. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TValue error: %s',[e.message]);
  82. end;
  83. end;
  84. procedure TRTTIFlexValue.SetAsTValue(const Value: TValue);
  85. begin
  86. Clear;
  87. case Value.Kind of
  88. tkInteger,
  89. tkInt64 : AsInt64 := Value.AsInt64;
  90. tkFloat : AsExtended := Value.AsExtended;
  91. tkChar,
  92. {$IFNDEF FPC}
  93. tkString,
  94. tkUstring,
  95. {$ELSE}
  96. tkAstring,
  97. {$ENDIF}
  98. tkWideString,
  99. tkWideChar : AsString := Value.AsString;
  100. tkEnumeration,
  101. tkSet : AsInteger := Value.AsInteger;
  102. tkClass : AsObject := Value.AsObject;
  103. tkInterface : AsInterface := Value.AsInterface;
  104. {$IFNDEF FPC}
  105. tkArray,
  106. tkDynArray : Self.SetAsCustom(TValueTValue.Create(Value),TValueDataType.dtArray);
  107. else AsVariant := Value.AsVariant;
  108. {$ENDIF}
  109. end;
  110. end;
  111. { TValueTValue }
  112. constructor TValueTValue.Create(const Value: TValue);
  113. begin
  114. fData := Value;
  115. end;
  116. function TValueTValue.GetValue: TValue;
  117. begin
  118. Result := fData;
  119. end;
  120. procedure TValueTValue.SetValue(const Value: TValue);
  121. begin
  122. fData := Value;
  123. end;
  124. end.