From e31ae1a2bdd2512d1a70201f3ca66dd56c64df37 Mon Sep 17 00:00:00 2001 From: miket Date: Fri, 7 Sep 2018 10:21:30 +0100 Subject: [PATCH] added ifdef for generics support, the library is once again compatible with Delphi versions 6 and up. --- src/TZDBPK/TZDB.pas | 61 +++++++++++++++++++++++++++-------------- src/TZDBPK/Version.inc | 11 ++++---- src/TZTest/TestTZDB.pas | 2 +- src/TZTest/dunit.ini | 26 ++++++++++++++++++ 4 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 src/TZTest/dunit.ini diff --git a/src/TZDBPK/TZDB.pas b/src/TZDBPK/TZDB.pas index f779377..7de210d 100644 --- a/src/TZDBPK/TZDB.pas +++ b/src/TZDBPK/TZDB.pas @@ -283,10 +283,10 @@ implementation {$IFNDEF SUPPORTS_MONITOR} SyncObjs, {$ENDIF} -{$IFDEF FPC} - Contnrs, -{$ELSE} +{$IFDEF SUPPORTS_GENERICS} Generics.Collections, +{$ELSE} + Contnrs, {$ENDIF} IniFiles; @@ -540,10 +540,10 @@ TCompiledPeriod = class FRulesByYearLock: TCriticalSection; {$ENDIF} { Year -> List of Rules for that year } -{$IFDEF FPC} - FRulesByYear: TBucketList; { Word, TList } -{$ELSE} +{$IFDEF SUPPORTS_GENERICS} FRulesByYear: TDictionary; { Word, TList } +{$ELSE} + FRulesByYear: TBucketList; { Word, TList } {$ENDIF} { Obtain the last rule that is active in a given year } @@ -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; @@ -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; @@ -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.Create; +{$ELSE} + FRulesByYear := TBucketList.Create(); {$ENDIF} end; @@ -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; @@ -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} @@ -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; diff --git a/src/TZDBPK/Version.inc b/src/TZDBPK/Version.inc index e409e04..f3c96db 100644 --- a/src/TZDBPK/Version.inc +++ b/src/TZDBPK/Version.inc @@ -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} diff --git a/src/TZTest/TestTZDB.pas b/src/TZTest/TestTZDB.pas index 4c19a80..ff7af98 100644 --- a/src/TZTest/TestTZDB.pas +++ b/src/TZTest/TestTZDB.pas @@ -33,7 +33,7 @@ interface TestFramework, {$IFNDEF SUPPORTS_TARRAY}Types,{$ENDIF} {$IFDEF SUPPORTS_TTIMESPAN}TimeSpan,{$ENDIF} - Generics.Collections, +Generics.Collections, Classes, SysUtils, TypInfo, diff --git a/src/TZTest/dunit.ini b/src/TZTest/dunit.ini new file mode 100644 index 0000000..c3e26e4 --- /dev/null +++ b/src/TZTest/dunit.ini @@ -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 +