📄 statickeymaptemplate.inc
字号:
{ JADD - Just Another DelphiDoc: Documentation from Delphi Source Code
Copyright (C) 2005-2008 Gerold Veith
This file is part of JADD - Just Another DelphiDoc.
DelphiDoc is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.
DelphiDoc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
{This file serves as a template for creating classes, like it is used in the
C++ programming language. Simply include the file three time, twice for the
declaration of the class, for instance in the interface of the unit, and
as third for the implementation in the implementation part.~[br]
This concept has a big contraint, a template can only be used once per pascal
file. But you could create small dummy-units to create classes from the
template and then use all those units from a central unit, re-defining the
types to a type in the central unit, so only it has to be used by other
pascal files.~[p]
This file contains a template for a class acting like a map of items, mapping
a list of "StaticKeys" (or another pointer-like type) to a value of
any type. As the name implies the list of the keys should not change while an
object of this class is using it.
}
{
TStaticKeyMapTemplateKey has to be defined to the key type!
TStaticKeyMapTemplateKeys has to be defined to the key list type!
Has to define a "Count" and an "IndexOf" method!
TStaticKeyMapTemplateValue has to be defined to the base/item type!
TStaticKeyMapTemplate then has to be defined as the class type.
}
//{$DEFINE StaticKeyMapTemplateDoNotEndType}
//{$DEFINE StaticKeyMapTemplateFreeKeyList}
//{$DEFINE StaticKeyMapTemplateValueIsObject}
//{$DEFINE StaticKeyMapTemplateInitializeItems}
//{$DEFINE StaticKeyMapTemplateValueFree}
{$IFNDEF StaticKeyMapTemplateItemIsObject}
{$UNDEF StaticKeyMapTemplateItemFree}
{$ENDIF}
{$IFNDEF StaticKeyMapTemplateItemIsObject}
{$DEFINE StaticKeyMapTemplateInitializeItems}
{$ENDIF}
{$UNDEF StaticKeyMapTemplateFreeSomething}
{$IFDEF StaticKeyMapTemplateFreeKeyList}
{$DEFINE StaticKeyMapTemplateFreeSomething}
{$ENDIF}
{$IFDEF StaticKeyMapTemplateItemFree}
{$DEFINE StaticKeyMapTemplateFreeSomething}
{$ENDIF}
{$IFNDEF StaticKeyMapTemplateSecondRun}
{$DEFINE StaticKeyMapTemplateSecondRun}
{$ELSE}
{$IFNDEF StaticKeyMapTemplateThirdRun}
{A map from keys to values. }
// TStaticKeyMapTemplate
= class
private
//the static list of keys (must not change!)
FKeys: TStaticKeyMapTemplateKeys;
//the items in the list
FValues: array of TStaticKeyMapTemplateValue;
public
//Creates a new map.
constructor Create(KeyList: TStaticKeyMapTemplateKeys);
{$IFDEF StaticKeyMapTemplateFreeSomething}
//Free all items and if applicable the keys list.
destructor Destroy; override;
{$ENDIF}
//Returns an item of the map.
function Get(const Key: TStaticKeyMapTemplateKey):
TStaticKeyMapTemplateValue;
//Sets an item in the map.
procedure Put(const Key: TStaticKeyMapTemplateKey;
const Value: TStaticKeyMapTemplateValue);
//Returns whether the map is empty.
function IsEmpty: Boolean;
//Can be used to access the contents of the map.
property Items[const Key: TStaticKeyMapTemplateKey]:
TStaticKeyMapTemplateValue
read Get write Put; default;
{$IFNDEF StaticKeyMapTemplateDoNotEndType}
end;
{$ENDIF}
{$DEFINE StaticKeyMapTemplateThirdRun}
// implementation
{$ELSE NOT StaticKeyMapTemplateSecondRun }
{Creates a new map.
~param KeyList the static list of keys to map to values }
constructor TStaticKeyMapTemplate.Create(KeyList: TStaticKeyMapTemplateKeys);
var Count :Integer; //number of values in the list
begin
inherited Create; //create the StaticKey
FKeys := KeyList; //save keys
Count := KeyList.Count; //get size of the list
if Count > 0 then //list not empty?
begin
SetLength(FValues, Count); //get memory for the values
{$IFDEF StaticKeyMapTemplateInitializeItems}
FillChar(FValues[0], Count * SizeOf(FValues[0]), Byte(nil));
{$ENDIF}
end;
end;
{$IFDEF StaticKeyMapTemplateFreeSomething}
{Free all items and if applicable the keys list. }
destructor TStaticKeyMapTemplate.Destroy;
{$IFDEF StaticKeyMapTemplateItemFree}
var i :Integer; //counter through all items
{$ENDIF}
begin
{$IFDEF StaticKeyMapTemplateFreeKeyList}
FKeys.Free;
{$ENDIF}
{$IFDEF StaticKeyMapTemplateItemFree}
for i := 0 to Length(FValues) - 1 do //free each item
FValues[i].Free;
{$ENDIF}
inherited Destroy; //free the map
end;
{$ENDIF}
{Returns an value of the key in the map.
~param Key the key whose value should be returned
~result the value of the key
~exception EListError if the key can't be fouond in the list }
function TStaticKeyMapTemplate.Get(const Key: TStaticKeyMapTemplateKey):
TStaticKeyMapTemplateValue;
var Index :Integer; //index of the key
begin
Index := FKeys.IndexOf(Key); //get the index of the key
if Index = -1 then
raise EListError.Create('Invalid key; not found in Map!');
Result := FValues[Index]; //return the value of the key
end;
{Sets an item in the map to another value.
~param Key the key whose value should be set
~param Value the new value for the key
~exception EListError if the key can't be fouond in the list }
procedure TStaticKeyMapTemplate.Put(const Key: TStaticKeyMapTemplateKey;
const Value: TStaticKeyMapTemplateValue);
var Index :Integer; //index of the key
begin
Index := FKeys.IndexOf(Key); //get the index of the key
if Index = -1 then
raise EListError.Create('Invalid key; not found in Map!');
FValues[Index] := Value; //set the value of the key
end;
{Returns whether the map is empty.
~result whether the map is empty }
function TStaticKeyMapTemplate.IsEmpty: Boolean;
begin
Result := Length(FValues) = 0;
end;
{$ENDIF NOT StaticKeyMapTemplateThirdRun }
{$ENDIF NOT StaticKeyMapTemplateSecondRun }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -