Skip to content

Commit

Permalink
added ifdef for generics support, the library is once again compatibl…
Browse files Browse the repository at this point in the history
…e with Delphi versions 6 and up.
  • Loading branch information
miket committed Sep 7, 2018
1 parent a2d88b8 commit e31ae1a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
61 changes: 40 additions & 21 deletions src/TZDBPK/TZDB.pas
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ implementation
{$IFNDEF SUPPORTS_MONITOR}
SyncObjs,
{$ENDIF}
{$IFDEF FPC}
Contnrs,
{$ELSE}
{$IFDEF SUPPORTS_GENERICS}
Generics.Collections,
{$ELSE}
Contnrs,
{$ENDIF}
IniFiles;

Expand Down Expand Up @@ -540,10 +540,10 @@ TCompiledPeriod = class
FRulesByYearLock: TCriticalSection;
{$ENDIF}
{ Year -> List of Rules for that year }
{$IFDEF FPC}
FRulesByYear: TBucketList; { Word, TList<TCompiledRule> }
{$ELSE}
{$IFDEF SUPPORTS_GENERICS}
FRulesByYear: TDictionary<Word,TList>; { Word, TList<TCompiledRule> }
{$ELSE}
FRulesByYear: TBucketList; { Word, TList<TCompiledRule> }
{$ENDIF}

{ Obtain the last rule that is active in a given year }
Expand Down Expand Up @@ -584,9 +584,15 @@ procedure ForEachYearlyRule(AInfo, AItem, AData: Pointer; out AContinue: Boolean
var i: Integer;
begin
{ Free the value list }
for i := 0 to TList(AData).Count - 1 do
TObject(TList(AData).Items[i]).Free;
TList(AData).Free;
if AData <> nil then
begin
if (TList(AData).Count > 0) then
begin
for i := 0 to TList(AData).Count - 1 do
TObject(TList(AData).Items[i]).Free;
end;
TList(AData).Free;
end;
AContinue := True;
end;

Expand Down Expand Up @@ -664,10 +670,10 @@ function TCompiledPeriod.CompileRulesForYear(const AYear: Word): TList;

{ Register the new list into the dictionary }
{$WARNINGS OFF}
{$IFDEF FPC}
FRulesByYear.Add(Pointer(AYear), Result);
{$ELSE}
{$IFDEF SUPPORTS_GENERICS}
FRulesByYear.Add(AYear, Result);
{$ELSE}
FRulesByYear.Add(Pointer(AYear), Result);
{$ENDIF}
{$WARNINGS ON}
end;
Expand All @@ -681,10 +687,10 @@ constructor TCompiledPeriod.Create(const APeriod: PPeriod; const AFrom, AUntil:
{$IFNDEF SUPPORTS_MONITOR}
FRulesByYearLock := TCriticalSection.Create;
{$ENDIF}
{$IFDEF FPC}
FRulesByYear := TBucketList.Create();
{$ELSE}
{$IFDEF SUPPORTS_GENERICS}
FRulesByYear := TDictionary<Word,TList>.Create;
{$ELSE}
FRulesByYear := TBucketList.Create();
{$ENDIF}
end;

Expand All @@ -702,8 +708,13 @@ destructor TCompiledPeriod.Destroy;
{$IFDEF FPC}
FRulesByYear.ForEach(@ForEachYearlyRule);
{$ELSE}
{$IFDEF SUPPORTS_GENERICS}
for L in FRulesByYear.Values do
ForEachYearlyRule(nil, nil, L, c);
{$ELSE}
FRulesByYear.ForEach(ForEachYearlyRule);
{$ENDIF}

{$ENDIF}

FRulesByYear.Free;
Expand All @@ -730,10 +741,10 @@ function TCompiledPeriod.FindMatchingRule(const ADateTime: TDateTime): TCompiled
try
{$WARNINGS OFF}
{ Check if we have a cached list of matching rules for this date's year }
{$IFDEF FPC}
if not FRulesByYear.Find(Pointer(LYear), Pointer(LCompiledList)) then
{$ELSE}
{$IFDEF SUPPORTS_GENERICS}
if not FRulesByYear.TryGetValue(LYear, LCompiledList) then
{$ELSE}
if not FRulesByYear.Find(Pointer(LYear), Pointer(LCompiledList)) then
{$ENDIF}
LCompiledList := CompileRulesForYear(LYear);
{$WARNINGS ON}
Expand Down Expand Up @@ -1099,9 +1110,17 @@ function TBundledTimeZone.ToISO8601Str(const ADateTime: TDateTime): String;
destructor TBundledTimeZone.Destroy;
var i: Integer;
begin
for i := 0 to FPeriods.Count - 1 do
TObject(FPeriods[i]).Free;
FPeriods.Free;
if Assigned(FPeriods) then
begin

if (FPeriods.Count > 0) then
begin
for i := 0 to FPeriods.Count - 1 do
TObject(FPeriods[i]).Free;
end;

FPeriods.Free;
end;
inherited;
end;

Expand Down
11 changes: 6 additions & 5 deletions src/TZDBPK/Version.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@
{$IFDEF CONDITIONALEXPRESSIONS}

{$IF DECLARED(CompilerVersion)}
{$IF CompilerVersion >= 14}
{$IF CompilerVersion >= 14} //Delphi 6
{$UNDEF UNSUPPORTED_VERSION}
{$IFEND}
{$IF CompilerVersion >= 17}
{$IF CompilerVersion >= 17} //Delphi 2005
{$DEFINE SUPPORTS_INLINE}
{$IFEND}
{$IFEND}

{$IF DECLARED(RTLVersion)}
{$IF RTLVersion >= 20}
{$IF RTLVersion >= 20} //Delphi 2009
{$DEFINE SUPPORTS_MONITOR}
{$DEFINE SUPPORTS_TSTRINGS_OWNSOBJECTS}
{$DEFINE SUPPORTS_GENERICS}
{$IFEND}
{$IF RTLVersion >= 21}
{$IF RTLVersion >= 21} //Delphi 2010
{$DEFINE SUPPORTS_TARRAY}
{$DEFINE SUPPORTS_TTIMESPAN}
{$IFEND}
{$IF RTLVersion >= 22}
{$IF RTLVersion >= 22} //Delphi XE
{$DEFINE SUPPORTS_TTIMEZONE}
{$IFEND}
{$IFEND}
Expand Down
2 changes: 1 addition & 1 deletion src/TZTest/TestTZDB.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface
TestFramework,
{$IFNDEF SUPPORTS_TARRAY}Types,{$ENDIF}
{$IFDEF SUPPORTS_TTIMESPAN}TimeSpan,{$ENDIF}
Generics.Collections,
Generics.Collections,
Classes,
SysUtils,
TypInfo,
Expand Down
26 changes: 26 additions & 0 deletions src/TZTest/dunit.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[GUITestRunner Config]
AutoSave=1
Left=642
Top=159
Width=687
Height=697
Maximized=0
UseRegistry=0
ResultsPanel.Height=174
ErrorMessage.Height=75
ErrorMessage.Visible=1
FailureList.ColumnWidth[0]=120
FailureList.ColumnWidth[1]=100
FailureList.ColumnWidth[2]=200
FailureList.ColumnWidth[3]=239
HideTestNodesOnOpen=0
BreakOnFailures=0
FailOnNoChecksExecuted=0
FailOnMemoryLeaked=0
IgnoreSetUpTearDownLeaks=0
ReportMemoryLeakTypes=0
SelectTestedNode=1
WarnOnFailTestOverride=0
PopupX=350
PopupY=30

0 comments on commit e31ae1a

Please sign in to comment.