-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution for Issue #125 and reverted change made in commit e90fee5 related to {$M-} #126
Open
alydouglas
wants to merge
8
commits into
VSoftTechnologies:master
Choose a base branch
from
alydouglas:tvaluecustomcomparer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−2
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a08120f
Fixed issue #125 Access violation in TValueHelper.CompareValue for tk…
aly-sas 9d3fc34
Fixed E1234 compiler error when {$M+} is specified at project level
aly-sas 936635e
Added better error message when custom comparer is not available
aly-sas f46ace6
Avoid error in TCustomValueComparerStore.RegisterCustomComparer when …
aly-sas f84d449
Fixed typo as was checking {$IFOPT M+} rather than {$IFDEF ENABLED_M+}
aly-sas 613eccf
Changed Pointer to PTypeInfo so it is stronger typed
aly-sas ccd3c6a
Updated TCustomValueComparer so that it can be used without TValue an…
aly-sas ec5e5b6
Updated TCustomValueComparer behaviour to use generics and not rely o…
aly-sas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,20 @@ | |
interface | ||
|
||
uses | ||
Rtti; | ||
System.Generics.Collections, | ||
System.Rtti; | ||
|
||
type | ||
//Allow custom comparisons | ||
TCustomValueComparer = reference to function(const a, b: TValue): Integer; | ||
TCustomValueComparerStore = record | ||
private | ||
class var CustomComparers: TDictionary<Pointer, TCustomValueComparer>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this not be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good point, will update that. |
||
public | ||
class procedure RegisterCustomComparer<T>(const AComparer: TCustomValueComparer); static; | ||
class procedure UnRegisterCustomComparer<T>; static; | ||
end; | ||
|
||
//TValue really needs to have an Equals operator overload! | ||
TValueHelper = record helper for TValue | ||
private | ||
|
@@ -64,6 +75,7 @@ TValueHelper = record helper for TValue | |
function IsWord: Boolean; | ||
function IsGuid: Boolean; | ||
function IsInterface : Boolean; | ||
function IsRecord: Boolean; | ||
function AsDouble: Double; | ||
function AsFloat: Extended; | ||
function AsSingle: Single; | ||
|
@@ -101,11 +113,17 @@ function CompareValue(const Left, Right: TValue): Integer; | |
EmptyResults: array[Boolean, Boolean] of Integer = ((0, -1), (1, 0)); | ||
var | ||
leftIsEmpty, rightIsEmpty: Boolean; | ||
CustomComparer: TCustomValueComparer; | ||
const | ||
ErrorStr: String = 'Unable to compare %s. Use Delphi.Mocks.Helpers.TCustomValueComparerStore.RegisterCustomComparer<T> to add a ' + | ||
'method to compare records.'; | ||
begin | ||
leftIsEmpty := left.IsEmpty; | ||
rightIsEmpty := right.IsEmpty; | ||
if leftIsEmpty or rightIsEmpty then | ||
Result := EmptyResults[leftIsEmpty, rightIsEmpty] | ||
else if (Left.TypeInfo = Right.TypeInfo) and TCustomValueComparerStore.CustomComparers.TryGetValue(Left.TypeInfo, CustomComparer) then | ||
Result := CustomComparer(Left, Right) | ||
else if left.IsOrdinal and right.IsOrdinal then | ||
Result := Math.CompareValue(left.AsOrdinal, right.AsOrdinal) | ||
else if left.IsFloat and right.IsFloat then | ||
|
@@ -116,6 +134,8 @@ function CompareValue(const Left, Right: TValue): Integer; | |
Result := NativeInt(left.AsObject) - NativeInt(right.AsObject) // TODO: instance comparer | ||
else if Left.IsInterface and Right.IsInterface then | ||
Result := NativeInt(left.AsInterface) - NativeInt(right.AsInterface) // TODO: instance comparer | ||
else if Left.IsRecord and Right.IsRecord then | ||
raise Exception.Create(Format(ErrorStr ,[Left.TypeInfo.Name])) | ||
else if left.IsVariant and right.IsVariant then | ||
begin | ||
case VarCompareValue(left.AsVariant, right.AsVariant) of | ||
|
@@ -236,6 +256,11 @@ function TValueHelper.IsPointer: Boolean; | |
Result := Kind = tkPointer; | ||
end; | ||
|
||
function TValueHelper.IsRecord: Boolean; | ||
begin | ||
Result := Kind = tkRecord; | ||
end; | ||
|
||
function TValueHelper.IsShortInt: Boolean; | ||
begin | ||
Result := TypeInfo = System.TypeInfo(ShortInt); | ||
|
@@ -307,4 +332,26 @@ function TRttiTypeHelper.TryGetMethod(const AName: string; out AMethod: TRttiMet | |
Result := Assigned(AMethod); | ||
end; | ||
|
||
|
||
|
||
{ TCustomValueComparerStore } | ||
|
||
class procedure TCustomValueComparerStore.RegisterCustomComparer<T>(const AComparer: TCustomValueComparer); | ||
begin | ||
CustomComparers.AddOrSetValue(System.TypeInfo(T), AComparer); | ||
end; | ||
|
||
class procedure TCustomValueComparerStore.UnRegisterCustomComparer<T>; | ||
begin | ||
CustomComparers.Remove(System.TypeInfo(T)); | ||
end; | ||
|
||
|
||
|
||
initialization | ||
TCustomValueComparerStore.CustomComparers := TDictionary<Pointer, TCustomValueComparer>.Create; | ||
|
||
finalization | ||
TCustomValueComparerStore.CustomComparers.Free; | ||
|
||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea if it's possible, but did you try doing something like
TCustomValueComparer = reference to function<T>(const a, b: T): Integer;
that would allow us to avoid TValue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that would be possible as I think you might need to declare it as
TCustomValueComparer<T> = reference to function<T>(const a, b: T): Integer;
so it couldn't be stored in a common container. Having said that, I am sure there should be a way to do this without the need for TValue?