1
0
mirror of synced 2024-12-22 17:23:07 +01:00

Added: value caching for defined settings

This commit is contained in:
Mark van Renswoude 2005-03-12 16:54:34 +00:00
parent cc9f8a792e
commit ba8347180b
3 changed files with 180 additions and 26 deletions

View File

@ -42,18 +42,22 @@ type
} }
TX2SettingsDefine = class(TObject) TX2SettingsDefine = class(TObject)
private private
FCallback: TX2SettingsCallback; FCached: Boolean;
FValue: Variant; FCallback: TX2SettingsCallback;
FDefaultValue: Variant;
FValue: Variant;
public public
constructor Create(const AValue: Variant; constructor Create(const AValue: Variant;
const ACallback: TX2SettingsCallback); const ACallback: TX2SettingsCallback);
procedure Action(const AAction: TX2SettingsAction; procedure Action(const AAction: TX2SettingsAction;
const ASection, AName: String; const ASection, AName: String;
var AValue: Variant); var AValue: Variant);
property Callback: TX2SettingsCallback read FCallback; property Cached: Boolean read FCached write FCached;
property Value: Variant read FValue; property Callback: TX2SettingsCallback read FCallback;
property DefaultValue: Variant read FDefaultValue;
property Value: Variant read FValue write FValue;
end; end;
@ -69,16 +73,20 @@ type
private private
FFactory: TX2SettingsFactory; FFactory: TX2SettingsFactory;
FSection: String; FSection: String;
protected
function InternalReadBool(const AName: String; out AValue: Boolean): Boolean; virtual; abstract;
function InternalReadFloat(const AName: String; out AValue: Double): Boolean; virtual; abstract;
function InternalReadInteger(const AName: String; out AValue: Integer): Boolean; virtual; abstract;
function InternalReadString(const AName: String; out AValue: String): Boolean; virtual; abstract;
procedure InternalWriteBool(const AName: String; AValue: Boolean); virtual; abstract; function ReadCache(const AName: String; out AValue: Variant): Boolean;
procedure InternalWriteFloat(const AName: String; AValue: Double); virtual; abstract; procedure WriteCache(const AName: String; const AValue: Variant); overload;
procedure InternalWriteInteger(const AName: String; AValue: Integer); virtual; abstract; procedure WriteCache(const ADefine: TX2SettingsDefine; const AValue: Variant); overload;
procedure InternalWriteString(const AName, AValue: String); virtual; abstract; protected
function InternalReadBool(const AName: String; out AValue: Boolean): Boolean; virtual;
function InternalReadFloat(const AName: String; out AValue: Double): Boolean; virtual;
function InternalReadInteger(const AName: String; out AValue: Integer): Boolean; virtual;
function InternalReadString(const AName: String; out AValue: String): Boolean; virtual;
procedure InternalWriteBool(const AName: String; AValue: Boolean); virtual;
procedure InternalWriteFloat(const AName: String; AValue: Double); virtual;
procedure InternalWriteInteger(const AName: String; AValue: Integer); virtual;
procedure InternalWriteString(const AName, AValue: String); virtual;
property Factory: TX2SettingsFactory read FFactory; property Factory: TX2SettingsFactory read FFactory;
property Section: String read FSection; property Section: String read FSection;
@ -202,6 +210,51 @@ end;
{============================ TX2Settings {============================ TX2Settings
Reading Reading
========================================} ========================================}
function TX2Settings.InternalReadBool(const AName: String;
out AValue: Boolean): Boolean;
var
vValue: Variant;
begin
Result := ReadCache(AName, vValue);
if Result then
AValue := vValue;
end;
function TX2Settings.InternalReadFloat(const AName: String;
out AValue: Double): Boolean;
var
vValue: Variant;
begin
Result := ReadCache(AName, vValue);
if Result then
AValue := vValue;
end;
function TX2Settings.InternalReadInteger(const AName: String;
out AValue: Integer): Boolean;
var
vValue: Variant;
begin
Result := ReadCache(AName, vValue);
if Result then
AValue := vValue;
end;
function TX2Settings.InternalReadString(const AName: String;
out AValue: String): Boolean;
var
vValue: Variant;
begin
Result := ReadCache(AName, vValue);
if Result then
AValue := vValue;
end;
function TX2Settings.ReadBool(const AName: String): Boolean; function TX2Settings.ReadBool(const AName: String): Boolean;
var var
pDefine: TX2SettingsDefine; pDefine: TX2SettingsDefine;
@ -212,7 +265,7 @@ begin
if not InternalReadBool(AName, Result) then if not InternalReadBool(AName, Result) then
if Assigned(pDefine) then if Assigned(pDefine) then
Result := pDefine.Value Result := pDefine.DefaultValue
else else
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]); raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
@ -221,6 +274,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -240,6 +294,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -253,7 +308,7 @@ begin
if not InternalReadFloat(AName, Result) then if not InternalReadFloat(AName, Result) then
if Assigned(pDefine) then if Assigned(pDefine) then
Result := pDefine.Value Result := pDefine.DefaultValue
else else
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]); raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
@ -262,6 +317,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -281,6 +337,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -294,7 +351,7 @@ begin
if not InternalReadInteger(AName, Result) then if not InternalReadInteger(AName, Result) then
if Assigned(pDefine) then if Assigned(pDefine) then
Result := pDefine.Value Result := pDefine.DefaultValue
else else
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]); raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
@ -303,6 +360,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -322,6 +380,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -335,7 +394,7 @@ begin
if not InternalReadString(AName, Result) then if not InternalReadString(AName, Result) then
if Assigned(pDefine) then if Assigned(pDefine) then
Result := pDefine.Value Result := pDefine.DefaultValue
else else
raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]); raise EX2SettingsUndefined.CreateFmt(RSUndefined, [AName]);
@ -344,6 +403,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -362,6 +422,7 @@ begin
vValue := Result; vValue := Result;
pDefine.Action(saRead, FSection, AName, vValue); pDefine.Action(saRead, FSection, AName, vValue);
Result := vValue; Result := vValue;
WriteCache(pDefine, Result);
end; end;
end; end;
@ -369,6 +430,28 @@ end;
{============================ TX2Settings {============================ TX2Settings
Writing Writing
========================================} ========================================}
procedure TX2Settings.InternalWriteBool(const AName: String; AValue: Boolean);
begin
WriteCache(AName, AValue);
end;
procedure TX2Settings.InternalWriteFloat(const AName: String; AValue: Double);
begin
WriteCache(AName, AValue);
end;
procedure TX2Settings.InternalWriteInteger(const AName: String;
AValue: Integer);
begin
WriteCache(AName, AValue);
end;
procedure TX2Settings.InternalWriteString(const AName, AValue: String);
begin
WriteCache(AName, AValue);
end;
procedure TX2Settings.WriteBool; procedure TX2Settings.WriteBool;
var var
pDefine: TX2SettingsDefine; pDefine: TX2SettingsDefine;
@ -442,6 +525,41 @@ begin
end; end;
function TX2Settings.ReadCache(const AName: String;
out AValue: Variant): Boolean;
var
pDefine: TX2SettingsDefine;
begin
pDefine := FFactory.GetDefine(FSection, AName);
Result := Assigned(pDefine) and pDefine.Cached;
if Result then
AValue := pDefine.Value;
end;
procedure TX2Settings.WriteCache(const AName: String;
const AValue: Variant);
var
pDefine: TX2SettingsDefine;
begin
pDefine := FFactory.GetDefine(FSection, AName);
if Assigned(pDefine) then
begin
pDefine.Cached := True;
pDefine.Value := AValue;
end;
end;
procedure TX2Settings.WriteCache(const ADefine: TX2SettingsDefine;
const AValue: Variant);
begin
ADefine.Cached := True;
ADefine.Value := AValue;
end;
{===================== TX2SettingsFactory {===================== TX2SettingsFactory
Defines Defines
========================================} ========================================}
@ -506,13 +624,17 @@ end;
{====================== TX2SettingsDefine {====================== TX2SettingsDefine
Initialization Initialization
========================================} ========================================}
constructor TX2SettingsDefine.Create; constructor TX2SettingsDefine.Create(const AValue: Variant;
const ACallback: TX2SettingsCallback);
begin begin
FValue := AValue; FCached := False;
FCallback := ACallback; FCallback := ACallback;
FDefaultValue := AValue;
end; end;
procedure TX2SettingsDefine.Action; procedure TX2SettingsDefine.Action(const AAction: TX2SettingsAction;
const ASection, AName: String;
var AValue: Variant);
begin begin
if Assigned(FCallback) then if Assigned(FCallback) then
FCallback(AAction, ASection, AName, AValue); FCallback(AAction, ASection, AName, AValue);

View File

@ -101,6 +101,10 @@ end;
========================================} ========================================}
function TX2INISettings.InternalReadBool; function TX2INISettings.InternalReadBool;
begin begin
Result := inherited InternalReadBool(AName, AValue);
if Result then
exit;
Result := ValueExists(AName); Result := ValueExists(AName);
if Result then if Result then
@ -109,6 +113,10 @@ end;
function TX2INISettings.InternalReadFloat; function TX2INISettings.InternalReadFloat;
begin begin
Result := inherited InternalReadFloat(AName, AValue);
if Result then
exit;
Result := ValueExists(AName); Result := ValueExists(AName);
if Result then if Result then
@ -117,6 +125,10 @@ end;
function TX2INISettings.InternalReadInteger; function TX2INISettings.InternalReadInteger;
begin begin
Result := inherited InternalReadInteger(AName, AValue);
if Result then
exit;
Result := ValueExists(AName); Result := ValueExists(AName);
if Result then if Result then
@ -125,6 +137,10 @@ end;
function TX2INISettings.InternalReadString; function TX2INISettings.InternalReadString;
begin begin
Result := inherited InternalReadString(AName, AValue);
if Result then
exit;
Result := ValueExists(AName); Result := ValueExists(AName);
if Result then if Result then
@ -137,21 +153,25 @@ end;
========================================} ========================================}
procedure TX2INISettings.InternalWriteBool; procedure TX2INISettings.InternalWriteBool;
begin begin
inherited;
FData.WriteBool(FSection, AName, AValue); FData.WriteBool(FSection, AName, AValue);
end; end;
procedure TX2INISettings.InternalWriteFloat; procedure TX2INISettings.InternalWriteFloat;
begin begin
inherited;
FData.WriteFloat(FSection, AName, AValue); FData.WriteFloat(FSection, AName, AValue);
end; end;
procedure TX2INISettings.InternalWriteInteger; procedure TX2INISettings.InternalWriteInteger;
begin begin
inherited;
FData.WriteInteger(FSection, AName, AValue); FData.WriteInteger(FSection, AName, AValue);
end; end;
procedure TX2INISettings.InternalWriteString; procedure TX2INISettings.InternalWriteString;
begin begin
inherited;
FData.WriteString(FSection, AName, AValue); FData.WriteString(FSection, AName, AValue);
end; end;

View File

@ -143,7 +143,9 @@ end;
========================================} ========================================}
function TX2RegistrySettings.InternalReadBool; function TX2RegistrySettings.InternalReadBool;
begin begin
Result := False; Result := inherited InternalReadBool(AName, AValue);
if Result then
exit;
if OpenRead() then if OpenRead() then
begin begin
@ -156,7 +158,9 @@ end;
function TX2RegistrySettings.InternalReadFloat; function TX2RegistrySettings.InternalReadFloat;
begin begin
Result := False; Result := inherited InternalReadFloat(AName, AValue);
if Result then
exit;
if OpenRead() then if OpenRead() then
begin begin
@ -169,7 +173,9 @@ end;
function TX2RegistrySettings.InternalReadInteger; function TX2RegistrySettings.InternalReadInteger;
begin begin
Result := False; Result := inherited InternalReadInteger(AName, AValue);
if Result then
exit;
if OpenRead() then if OpenRead() then
begin begin
@ -182,7 +188,9 @@ end;
function TX2RegistrySettings.InternalReadString; function TX2RegistrySettings.InternalReadString;
begin begin
Result := False; Result := inherited InternalReadString(AName, AValue);
if Result then
exit;
if OpenRead() then if OpenRead() then
begin begin
@ -199,24 +207,28 @@ end;
========================================} ========================================}
procedure TX2RegistrySettings.InternalWriteBool; procedure TX2RegistrySettings.InternalWriteBool;
begin begin
inherited;
if OpenWrite() then if OpenWrite() then
FData.WriteBool(AName, AValue); FData.WriteBool(AName, AValue);
end; end;
procedure TX2RegistrySettings.InternalWriteFloat; procedure TX2RegistrySettings.InternalWriteFloat;
begin begin
inherited;
if OpenWrite() then if OpenWrite() then
FData.WriteFloat(AName, AValue); FData.WriteFloat(AName, AValue);
end; end;
procedure TX2RegistrySettings.InternalWriteInteger; procedure TX2RegistrySettings.InternalWriteInteger;
begin begin
inherited;
if OpenWrite() then if OpenWrite() then
FData.WriteInteger(AName, AValue); FData.WriteInteger(AName, AValue);
end; end;
procedure TX2RegistrySettings.InternalWriteString; procedure TX2RegistrySettings.InternalWriteString;
begin begin
inherited;
if OpenWrite() then if OpenWrite() then
FData.WriteString(AName, AValue); FData.WriteString(AName, AValue);
end; end;