Skip to content

Commit

Permalink
cleanup TODO's
Browse files Browse the repository at this point in the history
  • Loading branch information
SunSerega committed Mar 28, 2024
1 parent 802913b commit aa54a10
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Packing/Template/LowLvl/BinUtils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{$savepcu false} //TODO #3057

uses System;
uses '..\..\..\Utils\TypeMagic';

type
BinReader = System.IO.BinaryReader;
Expand Down Expand Up @@ -51,8 +52,7 @@ function ReadEnum<T>(self: BinReader): T; extensionmethod;
if typeof(T).GetEnumUnderlyingType <> typeof(Int32) then
raise new NotSupportedException;
{$endif DEBUG}
//TODO #3056: as object
Result := T(self.ReadInt32() as object);
Result := ConvertTo&<T>.FromUnchecked(self.ReadInt32());
end;

end.
2 changes: 0 additions & 2 deletions Packing/Template/LowLvl/Essentials.pas
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
unit Essentials;

{$savepcu false} //TODO #3058

interface

uses System;
Expand Down
7 changes: 2 additions & 5 deletions Packing/Template/LowLvl/NamedItemHelpers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@
Otp($'WARNING: {kvp.Key} referenced itself');
end;

foreach var kvp in all_unused_reports do
kvp.Value();
//TODO #3061: Заменить предыдущий цикл
// foreach var rep in all_unused_reports.Values do
// rep.Invoke();
foreach var rep in all_unused_reports.values do
rep.Invoke();

all_unused_reports := nil;
end;
Expand Down
6 changes: 2 additions & 4 deletions Packing/Template/LowLvl/ParData.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ interface
private static inst := new ParArrSizeNotArray;
public static property Instance: ParArrSizeNotArray read inst;

//TODO #3063: use Instance
static constructor := DefineLoader(PASK_NotArray, br->inst);
static constructor := DefineLoader(PASK_NotArray, br->Instance);

end;

Expand All @@ -39,8 +38,7 @@ interface
private static inst := new ParArrSizeArbitrary;
public static property Instance: ParArrSizeArbitrary read inst;

//TODO #3063: use Instance
static constructor := DefineLoader(PASK_Arbitrary, br->inst);
static constructor := DefineLoader(PASK_Arbitrary, br->Instance);

end;

Expand Down
68 changes: 68 additions & 0 deletions Utils/TypeMagic.pas
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.

0 comments on commit aa54a10

Please sign in to comment.