Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {***************************************************************************}
- { }
- { Delphi.Mocks }
- { }
- { Copyright (C) 2011 Vincent Parrett }
- { }
- { http://www.finalbuilder.com }
- { }
- { }
- {***************************************************************************}
- { }
- { Licensed under the Apache License, Version 2.0 (the "License"); }
- { you may not use this file except in compliance with the License. }
- { You may obtain a copy of the License at }
- { }
- { http://www.apache.org/licenses/LICENSE-2.0 }
- { }
- { Unless required by applicable law or agreed to in writing, software }
- { distributed under the License is distributed on an "AS IS" BASIS, }
- { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
- { See the License for the specific language governing permissions and }
- { limitations under the License. }
- { }
- {***************************************************************************}
- (*
- SameValue/CompareValue Copyright (c) 2011, Stefan Glienke
- Used with permission.
- *)
- unit LeopardGerenciador.Helpers.RttiHelpers.RttiValuesHelper;
- interface
- uses
- Rtti;
- type
- //TValue really needs to have an Equals operator overload!
- TValueHelper = record helper for TValue
- private
- function GetRttiType: TRttiType;
- public
- function Equals(const value : TValue) : boolean;
- function IsFloat: Boolean;
- function IsNumeric: Boolean;
- function IsPointer: Boolean;
- function IsString: Boolean;
- function IsBoolean: Boolean;
- function IsByte: Boolean;
- function IsCardinal: Boolean;
- function IsCurrency: Boolean;
- function IsDate: Boolean;
- function IsDateTime: Boolean;
- function IsDouble: Boolean;
- function IsInteger: Boolean;
- function IsInt64: Boolean;
- function IsShortInt: Boolean;
- function IsSingle: Boolean;
- function IsSmallInt: Boolean;
- function IsTime: Boolean;
- function IsUInt64: Boolean;
- function IsVariant: Boolean;
- function IsWord: Boolean;
- function IsGuid: Boolean;
- function AsDouble: Double;
- function AsFloat: Extended;
- function AsSingle: Single;
- function AsPointer: Pointer;
- property RttiType: TRttiType read GetRttiType;
- end;
- TRttiTypeHelper = class helper for TRttiType
- function TryGetMethod(const AName: string; out AMethod: TRttiMethod): Boolean;
- function FindConstructor : TRttiMethod;
- end;
- function CompareValue(const Left, Right: TValue): Integer;
- function SameValue(const Left, Right: TValue): Boolean;
- implementation
- uses
- SysUtils,
- Math,
- TypInfo;
- var
- Context : TRttiContext;
- function CompareValue(const Left, Right: TValue): Integer;
- begin
- if Left.IsOrdinal and Right.IsOrdinal then
- begin
- Result := Math.CompareValue(Left.AsOrdinal, Right.AsOrdinal);
- end else
- if Left.IsFloat and Right.IsFloat then
- begin
- Result := Math.CompareValue(Left.AsFloat, Right.AsFloat);
- end else
- if Left.IsString and Right.IsString then
- begin
- Result := SysUtils.CompareStr(Left.AsString, Right.AsString);
- end else
- begin
- Result := 0;
- end;
- end;
- function SameValue(const Left, Right: TValue): Boolean;
- begin
- if Left.IsNumeric and Right.IsNumeric then
- begin
- if Left.IsOrdinal then
- begin
- if Right.IsOrdinal then
- begin
- Result := Left.AsOrdinal = Right.AsOrdinal;
- end else
- if Right.IsSingle then
- begin
- Result := Math.SameValue(Left.AsOrdinal, Right.AsSingle);
- end else
- if Right.IsDouble then
- begin
- Result := Math.SameValue(Left.AsOrdinal, Right.AsDouble);
- end
- else
- begin
- Result := Math.SameValue(Left.AsOrdinal, Right.AsExtended);
- end;
- end else
- if Left.IsSingle then
- begin
- if Right.IsOrdinal then
- begin
- Result := Math.SameValue(Left.AsSingle, Right.AsOrdinal);
- end else
- if Right.IsSingle then
- begin
- Result := Math.SameValue(Left.AsSingle, Right.AsSingle);
- end else
- if Right.IsDouble then
- begin
- Result := Math.SameValue(Left.AsSingle, Right.AsDouble);
- end
- else
- begin
- Result := Math.SameValue(Left.AsSingle, Right.AsExtended);
- end;
- end else
- if Left.IsDouble then
- begin
- if Right.IsOrdinal then
- begin
- Result := Math.SameValue(Left.AsDouble, Right.AsOrdinal);
- end else
- if Right.IsSingle then
- begin
- Result := Math.SameValue(Left.AsDouble, Right.AsSingle);
- end else
- if Right.IsDouble then
- begin
- Result := Math.SameValue(Left.AsDouble, Right.AsDouble);
- end
- else
- begin
- Result := Math.SameValue(Left.AsDouble, Right.AsExtended);
- end;
- end
- else
- begin
- if Right.IsOrdinal then
- begin
- Result := Math.SameValue(Left.AsExtended, Right.AsOrdinal);
- end else
- if Right.IsSingle then
- begin
- Result := Math.SameValue(Left.AsExtended, Right.AsSingle);
- end else
- if Right.IsDouble then
- begin
- Result := Math.SameValue(Left.AsExtended, Right.AsDouble);
- end
- else
- begin
- Result := Math.SameValue(Left.AsExtended, Right.AsExtended);
- end;
- end;
- end else
- if Left.IsString and Right.IsString then
- begin
- Result := Left.AsString = Right.AsString;
- end else
- if Left.IsClass and Right.IsClass then
- begin
- Result := Left.AsClass = Right.AsClass;
- end else
- if Left.IsObject and Right.IsObject then
- begin
- Result := Left.AsObject = Right.AsObject;
- end else
- if Left.IsPointer and Right.IsPointer then
- begin
- Result := Left.AsPointer = Right.AsPointer;
- end else
- if Left.IsVariant and Right.IsVariant then
- begin
- Result := Left.AsVariant = Right.AsVariant;
- end else
- if Left.IsGuid and Right.IsGuid then
- begin
- Result := IsEqualGuid( Left.AsType<TGUID>, Right.AsType<TGUID> );
- end else
- if Left.TypeInfo = Right.TypeInfo then
- begin
- Result := Left.AsPointer = Right.AsPointer;
- end else
- begin
- Result := False;
- end;
- end;
- function TValueHelper.AsDouble: Double;
- begin
- Result := AsType<Double>;
- end;
- function TValueHelper.AsFloat: Extended;
- begin
- Result := AsType<Extended>;
- end;
- function TValueHelper.AsPointer: Pointer;
- begin
- ExtractRawDataNoCopy(@Result);
- end;
- function TValueHelper.AsSingle: Single;
- begin
- Result := AsType<Single>;
- end;
- function TValueHelper.Equals(const value : TValue) : boolean;
- begin
- result := SameValue(Self, value);
- end;
- function TValueHelper.GetRttiType: TRttiType;
- begin
- Result := Context.GetType(TypeInfo);
- end;
- function TValueHelper.IsBoolean: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Boolean);
- end;
- function TValueHelper.IsByte: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Byte);
- end;
- function TValueHelper.IsCardinal: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Cardinal);
- end;
- function TValueHelper.IsCurrency: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Currency);
- end;
- function TValueHelper.IsDate: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(TDate);
- end;
- function TValueHelper.IsDateTime: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(TDateTime);
- end;
- function TValueHelper.IsDouble: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Double);
- end;
- function TValueHelper.IsFloat: Boolean;
- begin
- Result := Kind = tkFloat;
- end;
- function TValueHelper.IsInt64: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Int64);
- end;
- function TValueHelper.IsInteger: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Integer);
- end;
- function TValueHelper.IsNumeric: Boolean;
- begin
- Result := Kind in [tkInteger, tkChar, tkEnumeration, tkFloat, tkWChar, tkInt64];
- end;
- function TValueHelper.IsPointer: Boolean;
- begin
- Result := Kind = tkPointer;
- end;
- function TValueHelper.IsShortInt: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(ShortInt);
- end;
- function TValueHelper.IsSingle: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Single);
- end;
- function TValueHelper.IsSmallInt: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(SmallInt);
- end;
- function TValueHelper.IsString: Boolean;
- begin
- Result := Kind in [tkChar, tkString, tkWChar, tkLString, tkWString, tkUString];
- end;
- function TValueHelper.IsTime: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(TTime);
- end;
- function TValueHelper.IsUInt64: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(UInt64);
- end;
- function TValueHelper.IsVariant: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Variant);
- end;
- function TValueHelper.IsWord: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(Word);
- end;
- function TValueHelper.IsGuid: Boolean;
- begin
- Result := TypeInfo = System.TypeInfo(TGUID);
- end;
- { TRttiTypeHelper }
- function TRttiTypeHelper.FindConstructor: TRttiMethod;
- var
- candidateCtor: TRttiMethod;
- begin
- Result := nil;
- for candidateCtor in GetMethods('Create') do
- begin
- if Length(candidateCtor.GetParameters) = 0 then
- begin
- Result := candidateCtor;
- Break;
- end;
- end;
- end;
- function TRttiTypeHelper.TryGetMethod(const AName: string; out AMethod: TRttiMethod): Boolean;
- begin
- AMethod := GetMethod(AName);
- Result := Assigned(AMethod);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement