Quick.Value.RTTI.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 : 22/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. TRTTIFlexValue = record helper for TFlexValue
  30. private
  31. function CastToTValue: TValue;
  32. procedure SetAsTValue(const Value: TValue);
  33. public
  34. property AsTValue : TValue read CastToTValue write SetAsTValue;
  35. function AsType<T> : T;
  36. end;
  37. implementation
  38. { TRTTIFlexValue }
  39. function TRTTIFlexValue.AsType<T>: T;
  40. begin
  41. Result := AsObject;
  42. end;
  43. function TRTTIFlexValue.CastToTValue: TValue;
  44. begin
  45. try
  46. case DataType of
  47. dtNull : Result := TValueExtended;
  48. dtBoolean : Result := AsBoolean;
  49. {$IFDEF MSWINDOWS}
  50. dtAnsiString : Result := AsAnsiString;
  51. dtWideString : Result := AsWideString;
  52. {$ENDIF}
  53. dtInteger,
  54. dtInt64 : Result := AsInt64;
  55. {$IFNDEF FPC}
  56. dtVariant : Result := TValue.FromVariant(AsVariant);
  57. {$ENDIF}
  58. else raise Exception.Create('DataType not supported');
  59. end;
  60. except
  61. on E : Exception do raise Exception.CreateFmt('TFlexValue conversion to TValue error: %s',[e.message]);
  62. end;
  63. end;
  64. procedure TRTTIFlexValue.SetAsTValue(const Value: TValue);
  65. begin
  66. Clear;
  67. case Value.Kind of
  68. tkInteger,
  69. tkInt64 : AsInt64 := Value.AsInt64;
  70. tkFloat : AsExtended := Value.AsExtended;
  71. tkChar,
  72. {$IFNDEF FPC}
  73. tkString,
  74. tkUstring,
  75. {$ELSE}
  76. tkAstring,
  77. {$ENDIF}
  78. tkWideString,
  79. tkWideChar : AsString := Value.AsString;
  80. tkEnumeration,
  81. tkSet : AsInteger := Value.AsInteger;
  82. {$IFNDEF FPC}
  83. else AsVariant := Value.AsVariant;
  84. {$ENDIF}
  85. end;
  86. end;
  87. end.