Advertisement
filhotecmail

Untitled

Mar 26th, 2020
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.30 KB | None | 0 0
  1. {***************************************************************************}
  2. {                                                                           }
  3. {           Delphi.Mocks                                                    }
  4. {                                                                           }
  5. {           Copyright (C) 2011 Vincent Parrett                              }
  6. {                                                                           }
  7. {           http://www.finalbuilder.com                                     }
  8. {                                                                           }
  9. {                                                                           }
  10. {***************************************************************************}
  11. {                                                                           }
  12. {  Licensed under the Apache License, Version 2.0 (the "License");          }
  13. {  you may not use this file except in compliance with the License.         }
  14. {  You may obtain a copy of the License at                                  }
  15. {                                                                           }
  16. {      http://www.apache.org/licenses/LICENSE-2.0                           }
  17. {                                                                           }
  18. {  Unless required by applicable law or agreed to in writing, software      }
  19. {  distributed under the License is distributed on an "AS IS" BASIS,        }
  20. {  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
  21. {  See the License for the specific language governing permissions and      }
  22. {  limitations under the License.                                           }
  23. {                                                                           }
  24. {***************************************************************************}
  25.  
  26. (*
  27.   SameValue/CompareValue Copyright (c) 2011, Stefan Glienke
  28.   Used with permission.
  29. *)
  30.  
  31. unit LeopardGerenciador.Helpers.RttiHelpers.RttiValuesHelper;
  32.  
  33. interface
  34.  
  35. uses
  36.   Rtti;
  37.  
  38. type
  39.   //TValue really needs to have an Equals operator overload!
  40.   TValueHelper = record helper for TValue
  41.     private
  42.     function GetRttiType: TRttiType;
  43.   public
  44.     function Equals(const value : TValue) : boolean;
  45.     function IsFloat: Boolean;
  46.     function IsNumeric: Boolean;
  47.     function IsPointer: Boolean;
  48.     function IsString: Boolean;
  49.     function IsBoolean: Boolean;
  50.     function IsByte: Boolean;
  51.     function IsCardinal: Boolean;
  52.     function IsCurrency: Boolean;
  53.     function IsDate: Boolean;
  54.     function IsDateTime: Boolean;
  55.     function IsDouble: Boolean;
  56.     function IsInteger: Boolean;
  57.     function IsInt64: Boolean;
  58.     function IsShortInt: Boolean;
  59.     function IsSingle: Boolean;
  60.     function IsSmallInt: Boolean;
  61.     function IsTime: Boolean;
  62.     function IsUInt64: Boolean;
  63.     function IsVariant: Boolean;
  64.     function IsWord: Boolean;
  65.     function IsGuid: Boolean;
  66.     function AsDouble: Double;
  67.     function AsFloat: Extended;
  68.     function AsSingle: Single;
  69.     function AsPointer: Pointer;
  70.     property RttiType: TRttiType read GetRttiType;
  71.  
  72.   end;
  73.  
  74.  
  75.   TRttiTypeHelper = class helper for TRttiType
  76.     function TryGetMethod(const AName: string; out AMethod: TRttiMethod): Boolean;
  77.     function FindConstructor : TRttiMethod;
  78.   end;
  79.  
  80.  
  81. function CompareValue(const Left, Right: TValue): Integer;
  82. function SameValue(const Left, Right: TValue): Boolean;
  83.  
  84.  
  85. implementation
  86.  
  87. uses
  88.   SysUtils,
  89.   Math,
  90.   TypInfo;
  91.  
  92. var
  93.   Context : TRttiContext;
  94.  
  95. function CompareValue(const Left, Right: TValue): Integer;
  96. begin
  97.   if Left.IsOrdinal and Right.IsOrdinal then
  98.   begin
  99.     Result := Math.CompareValue(Left.AsOrdinal, Right.AsOrdinal);
  100.   end else
  101.   if Left.IsFloat and Right.IsFloat then
  102.   begin
  103.     Result := Math.CompareValue(Left.AsFloat, Right.AsFloat);
  104.   end else
  105.   if Left.IsString and Right.IsString then
  106.   begin
  107.     Result := SysUtils.CompareStr(Left.AsString, Right.AsString);
  108.   end else
  109.   begin
  110.     Result := 0;
  111.   end;
  112. end;
  113.  
  114. function SameValue(const Left, Right: TValue): Boolean;
  115. begin
  116.   if Left.IsNumeric and Right.IsNumeric then
  117.   begin
  118.     if Left.IsOrdinal then
  119.     begin
  120.       if Right.IsOrdinal then
  121.       begin
  122.         Result := Left.AsOrdinal = Right.AsOrdinal;
  123.       end else
  124.       if Right.IsSingle then
  125.       begin
  126.         Result := Math.SameValue(Left.AsOrdinal, Right.AsSingle);
  127.       end else
  128.       if Right.IsDouble then
  129.       begin
  130.         Result := Math.SameValue(Left.AsOrdinal, Right.AsDouble);
  131.       end
  132.       else
  133.       begin
  134.         Result := Math.SameValue(Left.AsOrdinal, Right.AsExtended);
  135.       end;
  136.     end else
  137.     if Left.IsSingle then
  138.     begin
  139.       if Right.IsOrdinal then
  140.       begin
  141.         Result := Math.SameValue(Left.AsSingle, Right.AsOrdinal);
  142.       end else
  143.       if Right.IsSingle then
  144.       begin
  145.         Result := Math.SameValue(Left.AsSingle, Right.AsSingle);
  146.       end else
  147.       if Right.IsDouble then
  148.       begin
  149.         Result := Math.SameValue(Left.AsSingle, Right.AsDouble);
  150.       end
  151.       else
  152.       begin
  153.         Result := Math.SameValue(Left.AsSingle, Right.AsExtended);
  154.       end;
  155.     end else
  156.     if Left.IsDouble then
  157.     begin
  158.       if Right.IsOrdinal then
  159.       begin
  160.         Result := Math.SameValue(Left.AsDouble, Right.AsOrdinal);
  161.       end else
  162.       if Right.IsSingle then
  163.       begin
  164.         Result := Math.SameValue(Left.AsDouble, Right.AsSingle);
  165.       end else
  166.       if Right.IsDouble then
  167.       begin
  168.         Result := Math.SameValue(Left.AsDouble, Right.AsDouble);
  169.       end
  170.       else
  171.       begin
  172.         Result := Math.SameValue(Left.AsDouble, Right.AsExtended);
  173.       end;
  174.     end
  175.     else
  176.     begin
  177.       if Right.IsOrdinal then
  178.       begin
  179.         Result := Math.SameValue(Left.AsExtended, Right.AsOrdinal);
  180.       end else
  181.       if Right.IsSingle then
  182.       begin
  183.         Result := Math.SameValue(Left.AsExtended, Right.AsSingle);
  184.       end else
  185.       if Right.IsDouble then
  186.       begin
  187.         Result := Math.SameValue(Left.AsExtended, Right.AsDouble);
  188.       end
  189.       else
  190.       begin
  191.         Result := Math.SameValue(Left.AsExtended, Right.AsExtended);
  192.       end;
  193.     end;
  194.   end else
  195.   if Left.IsString and Right.IsString then
  196.   begin
  197.     Result := Left.AsString = Right.AsString;
  198.   end else
  199.   if Left.IsClass and Right.IsClass then
  200.   begin
  201.     Result := Left.AsClass = Right.AsClass;
  202.   end else
  203.   if Left.IsObject and Right.IsObject then
  204.   begin
  205.     Result := Left.AsObject = Right.AsObject;
  206.   end else
  207.   if Left.IsPointer and Right.IsPointer then
  208.   begin
  209.     Result := Left.AsPointer = Right.AsPointer;
  210.   end else
  211.   if Left.IsVariant and Right.IsVariant then
  212.   begin
  213.     Result := Left.AsVariant = Right.AsVariant;
  214.   end else
  215.   if Left.IsGuid and Right.IsGuid then
  216.   begin
  217.     Result := IsEqualGuid( Left.AsType<TGUID>, Right.AsType<TGUID> );
  218.   end else
  219.   if Left.TypeInfo = Right.TypeInfo then
  220.   begin
  221.     Result := Left.AsPointer = Right.AsPointer;
  222.   end else
  223.   begin
  224.     Result := False;
  225.   end;
  226. end;
  227.  
  228.  
  229. function TValueHelper.AsDouble: Double;
  230. begin
  231.   Result := AsType<Double>;
  232. end;
  233.  
  234. function TValueHelper.AsFloat: Extended;
  235. begin
  236.   Result := AsType<Extended>;
  237. end;
  238.  
  239. function TValueHelper.AsPointer: Pointer;
  240. begin
  241.   ExtractRawDataNoCopy(@Result);
  242. end;
  243.  
  244. function TValueHelper.AsSingle: Single;
  245. begin
  246.   Result := AsType<Single>;
  247. end;
  248.  
  249. function TValueHelper.Equals(const value : TValue) : boolean;
  250. begin
  251.   result := SameValue(Self, value);
  252. end;
  253.  
  254. function TValueHelper.GetRttiType: TRttiType;
  255. begin
  256.    Result := Context.GetType(TypeInfo);
  257.  
  258. end;
  259.  
  260. function TValueHelper.IsBoolean: Boolean;
  261. begin
  262.   Result := TypeInfo = System.TypeInfo(Boolean);
  263. end;
  264.  
  265. function TValueHelper.IsByte: Boolean;
  266. begin
  267.   Result := TypeInfo = System.TypeInfo(Byte);
  268. end;
  269.  
  270. function TValueHelper.IsCardinal: Boolean;
  271. begin
  272.   Result := TypeInfo = System.TypeInfo(Cardinal);
  273. end;
  274.  
  275. function TValueHelper.IsCurrency: Boolean;
  276. begin
  277.   Result := TypeInfo = System.TypeInfo(Currency);
  278. end;
  279.  
  280. function TValueHelper.IsDate: Boolean;
  281. begin
  282.   Result := TypeInfo = System.TypeInfo(TDate);
  283. end;
  284.  
  285. function TValueHelper.IsDateTime: Boolean;
  286. begin
  287.   Result := TypeInfo = System.TypeInfo(TDateTime);
  288. end;
  289.  
  290. function TValueHelper.IsDouble: Boolean;
  291. begin
  292.   Result := TypeInfo = System.TypeInfo(Double);
  293. end;
  294.  
  295. function TValueHelper.IsFloat: Boolean;
  296. begin
  297.   Result := Kind = tkFloat;
  298. end;
  299.  
  300. function TValueHelper.IsInt64: Boolean;
  301. begin
  302.   Result := TypeInfo = System.TypeInfo(Int64);
  303. end;
  304.  
  305. function TValueHelper.IsInteger: Boolean;
  306. begin
  307.   Result := TypeInfo = System.TypeInfo(Integer);
  308. end;
  309.  
  310. function TValueHelper.IsNumeric: Boolean;
  311. begin
  312.   Result := Kind in [tkInteger, tkChar, tkEnumeration, tkFloat, tkWChar, tkInt64];
  313. end;
  314.  
  315. function TValueHelper.IsPointer: Boolean;
  316. begin
  317.   Result := Kind = tkPointer;
  318. end;
  319.  
  320. function TValueHelper.IsShortInt: Boolean;
  321. begin
  322.   Result := TypeInfo = System.TypeInfo(ShortInt);
  323. end;
  324.  
  325. function TValueHelper.IsSingle: Boolean;
  326. begin
  327.   Result := TypeInfo = System.TypeInfo(Single);
  328. end;
  329.  
  330. function TValueHelper.IsSmallInt: Boolean;
  331. begin
  332.   Result := TypeInfo = System.TypeInfo(SmallInt);
  333. end;
  334.  
  335. function TValueHelper.IsString: Boolean;
  336. begin
  337.   Result := Kind in [tkChar, tkString, tkWChar, tkLString, tkWString, tkUString];
  338. end;
  339.  
  340. function TValueHelper.IsTime: Boolean;
  341. begin
  342.   Result := TypeInfo = System.TypeInfo(TTime);
  343. end;
  344.  
  345. function TValueHelper.IsUInt64: Boolean;
  346. begin
  347.   Result := TypeInfo = System.TypeInfo(UInt64);
  348. end;
  349.  
  350. function TValueHelper.IsVariant: Boolean;
  351. begin
  352.   Result := TypeInfo = System.TypeInfo(Variant);
  353. end;
  354.  
  355. function TValueHelper.IsWord: Boolean;
  356. begin
  357.   Result := TypeInfo = System.TypeInfo(Word);
  358. end;
  359.  
  360.  
  361. function TValueHelper.IsGuid: Boolean;
  362. begin
  363.   Result := TypeInfo = System.TypeInfo(TGUID);
  364. end;
  365.  
  366.  
  367.  
  368. { TRttiTypeHelper }
  369.  
  370. function TRttiTypeHelper.FindConstructor: TRttiMethod;
  371. var
  372.   candidateCtor: TRttiMethod;
  373. begin
  374.   Result := nil;
  375.   for candidateCtor in GetMethods('Create') do
  376.   begin
  377.     if Length(candidateCtor.GetParameters) = 0 then
  378.     begin
  379.       Result := candidateCtor;
  380.       Break;
  381.     end;
  382.   end;
  383. end;
  384.  
  385. function TRttiTypeHelper.TryGetMethod(const AName: string; out AMethod: TRttiMethod): Boolean;
  386. begin
  387.   AMethod := GetMethod(AName);
  388.   Result := Assigned(AMethod);
  389. end;
  390.  
  391. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement