-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
85 additions
and
37 deletions.
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
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
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
unit Essentials; | ||
|
||
{$savepcu false} //TODO #3058 | ||
|
||
interface | ||
|
||
uses System; | ||
|
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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,6 @@ | ||
unit Patterns; | ||
{$zerobasedstrings} | ||
|
||
//TODO #2739 | ||
{$savepcu false} | ||
|
||
interface | ||
|
||
uses System; | ||
|
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
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
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 |
---|---|---|
|
@@ -7,8 +7,6 @@ | |
uses PathUtils; | ||
uses Timers; | ||
|
||
uses AQueue; //TODO #2543 | ||
|
||
{$region Helpers} | ||
|
||
type | ||
|
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
unit TypeMagic; | ||
|
||
interface | ||
|
||
type | ||
|
||
{$region Convert} | ||
|
||
ConvertTo<T> = sealed class | ||
|
||
private constructor := raise new System.InvalidOperationException; | ||
|
||
public static function FromChecked<T2>(o: T2): T; | ||
public static function FromUnchecked<T2>(o: T2): T; | ||
public static function From<T2>(o: T2; checked: boolean := true) := | ||
if checked then FromChecked(o) else FromUnchecked(o); | ||
|
||
end; | ||
|
||
{$endregion Convert} | ||
|
||
implementation | ||
|
||
uses System.Linq.Expressions; | ||
|
||
{$region Convert} | ||
|
||
type ConvertCache<T1,T2> = sealed class | ||
|
||
private constructor := raise new System.InvalidOperationException; | ||
|
||
public static checked, unchecked: T2->T1; | ||
private static function MakeFunc(conv: (Expression,System.Type)->Expression): T2->T1; | ||
begin | ||
try | ||
var p := Expression.Parameter(typeof(T2)); | ||
var c := conv(p, typeof(T1)); | ||
var l := Expression.Lambda&<Func<T2, T1>>(c, p); | ||
Result := l.Compile(); | ||
except | ||
on e: Exception do | ||
begin | ||
Result := o-> | ||
begin | ||
Result := default(T1); | ||
raise new System.InvalidCastException($'Failed to make [{TypeToTypeName(typeof(T2))}]=>[{TypeToTypeName(typeof(T1))}] conversion', e); | ||
end; | ||
exit; | ||
end; | ||
end; | ||
|
||
end; | ||
static constructor; | ||
begin | ||
checked := MakeFunc(Expression.ConvertChecked); | ||
unchecked := MakeFunc(Expression.Convert); | ||
end; | ||
|
||
end; | ||
|
||
static function ConvertTo<T>.FromChecked<T2>(o: T2) := | ||
ConvertCache&<T,T2>.checked(o); | ||
static function ConvertTo<T>.FromUnchecked<T2>(o: T2) := | ||
ConvertCache&<T,T2>.unchecked(o); | ||
|
||
{$endregion Convert} | ||
|
||
end. |