2016-06-22 17:49:16 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2018-01-29 17:30:53 +01:00
|
|
|
|
// Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
2017-03-02 17:39:41 +01:00
|
|
|
|
// Collaborators on this file: Ezequiel Juliano M<>ller (ezequieljuliano@gmail.com)
|
|
|
|
|
//
|
2016-06-22 17:49:16 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2016-09-30 11:44:11 +02:00
|
|
|
|
unit MVCFramework.DuckTyping;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
2017-01-18 21:53:53 +01:00
|
|
|
|
{$LEGACYIFEND ON}
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2017-03-01 21:40:57 +01:00
|
|
|
|
System.Rtti,
|
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.TypInfo,
|
|
|
|
|
System.Math;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
|
|
type
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
EMVCDuckTypingException = class(Exception);
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
TSortingType = (soAscending, soDescending);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
TDuckTypedList = class;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
TDuckListEnumerator = class(TEnumerator<TObject>)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
private
|
2014-03-07 23:16:33 +01:00
|
|
|
|
FPosition: Int64;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
FDuckTypedList: TDuckTypedList;
|
|
|
|
|
protected
|
|
|
|
|
function DoGetCurrent: TObject; override;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function DoMoveNext: Boolean; override;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
public
|
2017-03-01 21:40:57 +01:00
|
|
|
|
constructor Create(const ADuckTypedList: TDuckTypedList);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-02-07 16:06:07 +01:00
|
|
|
|
IMVCList = interface
|
2017-03-02 17:39:41 +01:00
|
|
|
|
['{D5958EC5-60FF-4C7B-81AF-96312174E719}']
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function GetItem(const AIndex: Integer): TObject;
|
|
|
|
|
function GetEnumerator: TDuckListEnumerator;
|
|
|
|
|
function GetOwnsObjects: Boolean;
|
|
|
|
|
procedure SetOwnsObjects(const AValue: Boolean);
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
procedure Add(const AObject: TObject);
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function Count: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
procedure Clear;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
|
|
|
|
|
function IsWrappedList: Boolean; overload;
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
function WrappedObject: TObject;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
procedure Sort(const APropertyName: string; const AOrder: TSortingType = soAscending);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
property OwnsObjects: Boolean read GetOwnsObjects write SetOwnsObjects;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-02-07 16:06:07 +01:00
|
|
|
|
IWrappedList = IMVCList;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
TDuckTypedList = class(TInterfacedObject, IMVCList)
|
|
|
|
|
private
|
|
|
|
|
FOwnsObject: Boolean;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
FObjectAsDuck: TObject;
|
|
|
|
|
FContext: TRttiContext;
|
|
|
|
|
FObjType: TRttiType;
|
|
|
|
|
FAddMethod: TRttiMethod;
|
|
|
|
|
FClearMethod: TRttiMethod;
|
|
|
|
|
FCountProperty: TRttiProperty;
|
|
|
|
|
FGetItemMethod: TRttiMethod;
|
|
|
|
|
FGetCountMethod: TRttiMethod;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
protected
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function GetItem(const AIndex: Integer): TObject;
|
|
|
|
|
function GetEnumerator: TDuckListEnumerator;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function GetOwnsObjects: Boolean;
|
|
|
|
|
procedure SetOwnsObjects(const AValue: Boolean);
|
2017-03-02 17:39:41 +01:00
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
procedure Add(const AObject: TObject);
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function Count: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
procedure Clear;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
function WrappedObject: TObject;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
procedure Sort(const APropertyName: string; const AOrder: TSortingType = soAscending);
|
|
|
|
|
|
|
|
|
|
procedure QuickSort(const AList: IMVCList; ALeft, ARigth: Integer; ACompare: TFunc<TObject, TObject, Integer>); overload;
|
|
|
|
|
procedure QuickSort(const AList: IMVCList; ACompare: TFunc<TObject, TObject, Integer>); overload;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
public
|
2017-03-02 17:39:41 +01:00
|
|
|
|
constructor Create(const AObjectAsDuck: TObject; const AOwnsObject: Boolean = False); overload;
|
|
|
|
|
constructor Create(const AInterfaceAsDuck: IInterface; const AOwnsObject: Boolean = False); overload;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
destructor Destroy; override;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function IsWrappedList: Boolean; overload;
|
|
|
|
|
|
|
|
|
|
class function CanBeWrappedAsList(const AObjectAsDuck: TObject): Boolean; overload; static;
|
|
|
|
|
class function CanBeWrappedAsList(const AInterfaceAsDuck: IInterface): Boolean; overload; static;
|
|
|
|
|
class function Wrap(const AObjectAsDuck: TObject; const AOwnsObject: Boolean = False): IMVCList; static;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function WrapAsList(const AObject: TObject; AOwnsObject: Boolean = False): IMVCList;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function WrapAsList(const AObject: TObject; AOwnsObject: Boolean = False): IMVCList;
|
|
|
|
|
begin
|
|
|
|
|
Result := TDuckTypedList.Wrap(AObject, AOwnsObject);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function CompareValue(const ALeft, ARight: TValue): Integer;
|
|
|
|
|
begin
|
|
|
|
|
if ALeft.IsOrdinal then
|
|
|
|
|
begin
|
|
|
|
|
Result := System.Math.CompareValue(ALeft.AsOrdinal, ARight.AsOrdinal);
|
|
|
|
|
end
|
|
|
|
|
else if ALeft.Kind = tkFloat then
|
|
|
|
|
begin
|
|
|
|
|
Result := System.Math.CompareValue(ALeft.AsExtended, ARight.AsExtended);
|
|
|
|
|
end
|
|
|
|
|
else if ALeft.Kind in [tkString, tkUString, tkWString, tkLString] then
|
|
|
|
|
begin
|
|
|
|
|
Result := CompareText(ALeft.AsString, ARight.AsString);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
Result := 0;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
{ TDuckListEnumerator }
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
constructor TDuckListEnumerator.Create(const ADuckTypedList: TDuckTypedList);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
|
|
|
|
FDuckTypedList := ADuckTypedList;
|
2014-03-07 23:16:33 +01:00
|
|
|
|
FPosition := -1;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
function TDuckListEnumerator.DoGetCurrent: TObject;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-01 21:40:57 +01:00
|
|
|
|
if (FPosition > -1) then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Result := FDuckTypedList.GetItem(FPosition)
|
|
|
|
|
else
|
2017-03-02 17:39:41 +01:00
|
|
|
|
raise EMVCDuckTypingException.Create('TDuckListEnumerator exception: call MoveNext first.');
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function TDuckListEnumerator.DoMoveNext: Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Result := False;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
if (FPosition < FDuckTypedList.Count - 1) then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
Inc(FPosition);
|
|
|
|
|
Result := True;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
{ TDuckTypedList }
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
procedure TDuckTypedList.Add(const AObject: TObject);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if not Assigned(FAddMethod) then
|
|
|
|
|
raise EMVCDuckTypingException.Create('Cannot find method "Add" in the Duck Object.');
|
|
|
|
|
FAddMethod.Invoke(FObjectAsDuck, [AObject]);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
class function TDuckTypedList.CanBeWrappedAsList(const AInterfaceAsDuck: IInterface): Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Result := CanBeWrappedAsList(TObject(AInterfaceAsDuck));
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
class function TDuckTypedList.CanBeWrappedAsList(const AObjectAsDuck: TObject): Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2017-03-02 17:39:41 +01:00
|
|
|
|
List: IMVCList;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
List := TDuckTypedList.Create(AObjectAsDuck);
|
|
|
|
|
Result := List.IsWrappedList;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TDuckTypedList.Clear;
|
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if not Assigned(FClearMethod) then
|
|
|
|
|
raise EMVCDuckTypingException.Create('Cannot find method "Clear" in the Duck Object.');
|
|
|
|
|
FClearMethod.Invoke(FObjectAsDuck, []);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TDuckTypedList.Count: Integer;
|
|
|
|
|
begin
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Result := 0;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
|
|
|
|
|
if (not Assigned(FGetCountMethod)) and (not Assigned(FCountProperty)) then
|
|
|
|
|
raise EMVCDuckTypingException.Create('Cannot find property/method "Count" in the Duck Object.');
|
|
|
|
|
|
|
|
|
|
if Assigned(FCountProperty) then
|
|
|
|
|
Result := FCountProperty.GetValue(FObjectAsDuck).AsInteger
|
|
|
|
|
else if Assigned(FGetCountMethod) then
|
|
|
|
|
Result := FGetCountMethod.Invoke(FObjectAsDuck, []).AsInteger;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
constructor TDuckTypedList.Create(const AInterfaceAsDuck: IInterface; const AOwnsObject: Boolean);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Create(TObject(AInterfaceAsDuck), AOwnsObject);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
constructor TDuckTypedList.Create(const AObjectAsDuck: TObject; const AOwnsObject: Boolean);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
FOwnsObject := AOwnsObject;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
FObjectAsDuck := AObjectAsDuck;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if not Assigned(FObjectAsDuck) then
|
|
|
|
|
raise EMVCDuckTypingException.Create('Duck Object can not be null.');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
FContext := TRttiContext.Create;
|
|
|
|
|
FObjType := FContext.GetType(FObjectAsDuck.ClassInfo);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
FAddMethod := nil;
|
|
|
|
|
FClearMethod := nil;
|
|
|
|
|
FGetItemMethod := nil;
|
|
|
|
|
FGetCountMethod := nil;
|
|
|
|
|
FCountProperty := nil;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if IsWrappedList then
|
|
|
|
|
begin
|
|
|
|
|
FAddMethod := FObjType.GetMethod('Add');
|
|
|
|
|
FClearMethod := FObjType.GetMethod('Clear');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
{$IF CompilerVersion >= 23}
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if Assigned(FObjType.GetIndexedProperty('Items')) then
|
|
|
|
|
FGetItemMethod := FObjType.GetIndexedProperty('Items').ReadMethod;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
{$IFEND}
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if not Assigned(FGetItemMethod) then
|
|
|
|
|
FGetItemMethod := FObjType.GetMethod('GetItem');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if not Assigned(FGetItemMethod) then
|
|
|
|
|
FGetItemMethod := FObjType.GetMethod('GetElement');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
FGetCountMethod := nil;
|
|
|
|
|
FCountProperty := FObjType.GetProperty('Count');
|
|
|
|
|
if not Assigned(FCountProperty) then
|
|
|
|
|
FGetCountMethod := FObjType.GetMethod('Count');
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
destructor TDuckTypedList.Destroy;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if FOwnsObject and Assigned(FObjectAsDuck) then
|
|
|
|
|
FObjectAsDuck.Free;
|
|
|
|
|
FContext.Free;
|
|
|
|
|
inherited Destroy;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function TDuckTypedList.GetEnumerator: TDuckListEnumerator;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Result := TDuckListEnumerator.Create(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function TDuckTypedList.GetItem(const AIndex: Integer): TObject;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if not Assigned(FGetItemMethod) then
|
|
|
|
|
raise EMVCDuckTypingException.Create('Cannot find method Indexed property "Items" or method "GetItem" or method "GetElement" in the Duck Object.');
|
|
|
|
|
Result := FGetItemMethod.Invoke(FObjectAsDuck, [AIndex]).AsObject;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function TDuckTypedList.GetOwnsObjects: Boolean;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
var
|
|
|
|
|
Prop: TRttiProperty;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Result := False;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Prop := FObjType.GetProperty('OwnsObjects');
|
|
|
|
|
if Assigned(Prop) then
|
|
|
|
|
if Prop.IsReadable then
|
|
|
|
|
Result := Prop.GetValue(FObjectAsDuck).AsBoolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
function TDuckTypedList.IsWrappedList: Boolean;
|
2017-02-07 16:06:07 +01:00
|
|
|
|
var
|
2017-03-02 17:39:41 +01:00
|
|
|
|
ObjectType: TRttiType;
|
2017-02-07 16:06:07 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
ObjectType := FContext.GetType(FObjectAsDuck.ClassInfo);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Result := (ObjectType.GetMethod('Add') <> nil) and (ObjectType.GetMethod('Clear') <> nil)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
{$IF CompilerVersion >= 23}
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
and (ObjectType.GetIndexedProperty('Items') <> nil) and (ObjectType.GetIndexedProperty('Items').ReadMethod <> nil)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
{$IFEND}
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
and (ObjectType.GetMethod('GetItem') <> nil) or (ObjectType.GetMethod('GetElement') <> nil) and (ObjectType.GetProperty('Count') <> nil);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
procedure TDuckTypedList.QuickSort(const AList: IMVCList; ALeft,
|
|
|
|
|
ARigth: Integer; ACompare: TFunc<TObject, TObject, Integer>);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
|
|
|
|
I, J: Integer;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
P: TObject;
|
|
|
|
|
M: TRttiMethod;
|
|
|
|
|
T: TRttiType;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
{ 07/08/2013: This method is based on QuickSort procedure from
|
|
|
|
|
Classes.pas, (c) Borland Software Corp.
|
|
|
|
|
but modified to be part of TDuckListU unit. It implements the
|
|
|
|
|
standard quicksort algorithm,
|
|
|
|
|
delegating comparison operation to an anonimous.
|
|
|
|
|
The Borland version delegates to a pure function
|
|
|
|
|
pointer, which is problematic in some cases. }
|
|
|
|
|
repeat
|
2017-03-02 17:39:41 +01:00
|
|
|
|
I := ALeft;
|
|
|
|
|
J := ARigth;
|
|
|
|
|
P := AList.GetItem((ALeft + ARigth) shr 1);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
repeat
|
2017-03-02 17:39:41 +01:00
|
|
|
|
while ACompare(TObject(AList.GetItem(I)), P) < 0 do
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Inc(I);
|
2017-03-02 17:39:41 +01:00
|
|
|
|
while ACompare(TObject(AList.GetItem(J)), P) > 0 do
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Dec(J);
|
2016-09-29 18:17:12 +02:00
|
|
|
|
if I <= J then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
T := FContext.GetType(AList.WrappedObject.ClassInfo);
|
|
|
|
|
M := T.GetMethod('Exchange');
|
|
|
|
|
if Assigned(M) then
|
|
|
|
|
M.Invoke(AList.WrappedObject, [I, J])
|
|
|
|
|
else
|
|
|
|
|
raise EMVCDuckTypingException.CreateFmt('Cannot find compatible method "%s" in the object', ['Exchange']);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Inc(I);
|
|
|
|
|
Dec(J);
|
|
|
|
|
end;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
until I > J;
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if ALeft < J then
|
|
|
|
|
QuickSort(AList, ALeft, J, ACompare);
|
|
|
|
|
ALeft := I;
|
|
|
|
|
until I >= ARigth;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
procedure TDuckTypedList.QuickSort(const AList: IMVCList;
|
|
|
|
|
ACompare: TFunc<TObject, TObject, Integer>);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-01 21:40:57 +01:00
|
|
|
|
QuickSort(AList, 0, AList.Count - 1, ACompare);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TDuckTypedList.SetOwnsObjects(const AValue: Boolean);
|
2017-03-02 17:39:41 +01:00
|
|
|
|
var
|
|
|
|
|
Prop: TRttiProperty;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Prop := FObjType.GetProperty('OwnsObjects');
|
|
|
|
|
if Assigned(Prop) then
|
|
|
|
|
if Prop.IsWritable then
|
|
|
|
|
Prop.SetValue(FObjectAsDuck, AValue)
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
procedure TDuckTypedList.Sort(const APropertyName: string; const AOrder: TSortingType);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
if (AOrder = soAscending) then
|
|
|
|
|
QuickSort(Self,
|
|
|
|
|
function(ALeft, ARight: TObject): Integer
|
|
|
|
|
var
|
|
|
|
|
PropLeft, PropRight: TRttiProperty;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
PropLeft := FContext.GetType(ALeft).GetProperty(APropertyName);
|
|
|
|
|
PropRight := FContext.GetType(ARight).GetProperty(APropertyName);
|
|
|
|
|
Result := CompareValue(PropLeft, PropRight);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end)
|
|
|
|
|
else
|
2017-03-02 17:39:41 +01:00
|
|
|
|
QuickSort(Self,
|
|
|
|
|
function(ALeft, ARight: TObject): Integer
|
|
|
|
|
var
|
|
|
|
|
PropLeft, PropRight: TRttiProperty;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-02 17:39:41 +01:00
|
|
|
|
PropLeft := FContext.GetType(ALeft).GetProperty(APropertyName);
|
|
|
|
|
PropRight := FContext.GetType(ARight).GetProperty(APropertyName);
|
|
|
|
|
Result := -1 * CompareValue(PropLeft, PropRight);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 17:39:41 +01:00
|
|
|
|
class function TDuckTypedList.Wrap(const AObjectAsDuck: TObject; const AOwnsObject: Boolean): IMVCList;
|
|
|
|
|
var
|
|
|
|
|
List: IMVCList;
|
|
|
|
|
begin
|
2017-03-20 21:42:28 +01:00
|
|
|
|
if AObjectAsDuck is TDuckTypedList then
|
|
|
|
|
Exit(AObjectAsDuck as TDuckTypedList);
|
2017-03-02 17:39:41 +01:00
|
|
|
|
Result := nil;
|
|
|
|
|
List := TDuckTypedList.Create(AObjectAsDuck, AOwnsObject);
|
|
|
|
|
if List.IsWrappedList then
|
|
|
|
|
Result := List;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TDuckTypedList.WrappedObject: TObject;
|
|
|
|
|
begin
|
|
|
|
|
Result := FObjectAsDuck;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end.
|