unit CommonDataStore;
interface
uses
System.Generics.Collections;
type
TDataStoreEntry = class;
TDataStore<T: TDataStoreEntry> = class(TObjectDictionary<Integer, T>)
private
protected
function GetEntry(ID: Integer): T; virtual;
public
constructor Create; reintroduce;
destructor Destroy; override;
function AddEntry(NewEntry: T): Boolean; virtual;
procedure DeleteEntry(const ID: Integer); overload; virtual;
procedure DeleteEntry(Entry: TDataStoreEntry); overload; virtual;
property Entry[ID: Integer]: T read GetEntry; default;
end;
TDataStoreEntry = class
private
FID: Integer;
FOwnerDataStore: TDataStore<TDataStoreEntry>;
protected
function GetDataStore: TDataStore<TDataStoreEntry>; virtual;
function GetIsNull: Boolean; virtual;
function Delete: Boolean; virtual;
public
property ID: Integer read FID write FID;
property IsNull: Boolean read GetIsNull;
end;
var NULL_DATASTORE_ENTRY: TDataStoreEntry;
implementation
uses
System.Generics.Defaults;
{ TDataStore<T> }
function TDataStore<T>.AddEntry(NewEntry: T): Boolean;
begin
Result := Self.TryAdd(NewEntry.ID, NewEntry);
end;
procedure TDataStore<T>.DeleteEntry(const ID: Integer);
begin
Remove(ID);
end;
constructor TDataStore<T>.Create;
begin
inherited Create([doOwnsValues]);
end;
procedure TDataStore<T>.DeleteEntry(Entry: TDataStoreEntry);
begin
Remove(Entry.ID);
end;
destructor TDataStore<T>.Destroy;
begin
inherited;
end;
function TDataStore<T>.GetEntry(ID: Integer): T;
begin
if ContainsKey(ID) then
Result := Items[ID]
else
Result := NULL_DATASTORE_ENTRY; // <---- [dcc32 Error] CommonDataStore.pas(77): E2010 Incompatible types: 'T' and 'TDataStoreEntry'
end;
{ TDataStoreEntry }
function TDataStoreEntry.Delete: Boolean;
begin
end;
function TDataStoreEntry.GetDataStore: TDataStore<TDataStoreEntry>;
begin
end;
function TDataStoreEntry.GetIsNull: Boolean;
begin
Result := False;
end;
type
TNullDataStoreEntry = class(TDataStoreEntry)
protected
function GetIsNull: Boolean; override;
end;
{ TNullDataStoreEntry }
function TNullDataStoreEntry.GetIsNull: Boolean;
begin
Result := True;
end;
initialization
NULL_DATASTORE_ENTRY := TNullDataStoreEntry.Create;
finalization
NULL_DATASTORE_ENTRY.Free;
end.