Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-10

TFPHashList help

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I have the following class and function that retrieves a object from a internal TFPHashList. If the object isn't in the list, it is read from a database, and then also inserted into the hashlist, before it gets returned.

Does TFPHashTable also manage the objects it contains (or references) like TObjectList. If not (which I think it doesn't), that means when TModuleFlyweightFactory gets destroyed, I need to free and empty the internal TFPHashTable as well. I'm not sure how I would to that though. Can is loop through the hashtable using the Items[] property to free off the referenced object. Set that same Item to nil, and then in the end, just .Free the FPHashTable?

interface section:

  TModuleFlyweightFactory = class(TObject)
  private
    FList: TFPHashTable;
    function GetModuleCount: integer;
  public
    constructor Create;
    destructor  Destroy; override;
    function    GetModule(pOID: TOID): TModule; overload;
    function    GetModule(pOIDAsString: string): TModule; overload;
    property    ModuleCount: integer read GetModuleCount;
  end;

implementation section:

function TModuleFlyweightFactory.GetModule(pOID: TOID): TModule;
var
  lData: TModule;
begin
  lData := TModule(FList.Items[pOID.AsString]);
  if lData  nil then
    Result := lData
  else
  begin
    lData := TModule.Create;
    lData.OID.AsString := pOID.AsString;
    lData.ReadPK;
    if lData.ObjectState  posEmpty then
      FList.Add(lData.OID.AsString, lData);  // only add if we know we
have something
    Result := lData;
  end;
end;

Regards,
- Graeme -

Graeme Geldenhuys
to Graeme Geldenhuys

Sorry, I forgot to mention... TFPHashTable.Items[] doesn't take a integer, but a string. Which I believe is the 'key', so I can't loop through the list.

Graeme.

Graeme Geldenhuys wrote 57 lines
Hi, I have the following class and function that retrieves a object from a internal TFPHashList. If the object isn't in the list, it is read from a database, and then also inserted into the hashlist, before it gets returned. Does TFPHashTable also manage the objects it contains (or references) like TObjectList. If not (which I think it doesn't), that means when TModuleFlyweightFactory gets destroyed, I need to free and empty the internal TFPHashTable as well. I'm not sure how I would to that though. Can is loop through the hashtable using the Items[] property to free off the referenced object. Set that same Item to nil, and then in the end, just .Free the FPHashTable? interface section: TModuleFlyweightFactory = class(TObject) private FList: TFPHashTable; function GetModuleCount: integer; public constructor Create; destructor Destroy; override; function GetModule(pOID: TOID): TModule; overload; function GetModule(pOIDAsString: string): TModule; overload; property ModuleCount: integer read GetModuleCount; end; implementation section: function TModuleFlyweightFactory.GetModule(pOID: TOID): TModule; var lData: TModule; begin lData := TModule(FList.Items[pOID.AsString]); if lData nil then Result := lData else begin lData := TModule.Create; lData.OID.AsString := pOID.AsString; lData.ReadPK; if lData.ObjectState posEmpty then FList.Add(lData.OID.AsString, lData); // only add if we know we have something Result := lData; end; end; Regards, - Graeme -
There's no place like 127.0.0.1
Michael Van Canneyt
to Graeme Geldenhuys: I have the following class and function that retrieves a object from a internal…
Graeme Geldenhuys wrote 14 lines
Hi, I have the following class and function that retrieves a object from a internal TFPHashList. If the object isn't in the list, it is read from a database, and then also inserted into the hashlist, before it gets returned. Does TFPHashTable also manage the objects it contains (or references) like TObjectList. If not (which I think it doesn't), that means when TModuleFlyweightFactory gets destroyed, I need to free and empty the internal TFPHashTable as well. I'm not sure how I would to that though. Can is loop through the hashtable using the Items[] property to free off the referenced object. Set that same Item to nil, and then in the end, just .Free the FPHashTable?

Well, I just programmed an extension to TFPHashTable (in cooperation with Dean Zobec) which allows you to select this behaviour: to free or not to free.

It's not yet in SVN, but if you want I can send it to you.

Michael.

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 6 lines
Well, I just programmed an extension to TFPHashTable (in cooperation with Dean Zobec) which allows you to select this behaviour: to free or not to free. It's not yet in SVN, but if you want I can send it to you. Michael.

It's not urgent, so will wait until it is in SVN. In the mean time I swapped out the internal TFPHashList for a TStringList (and make a note to swap it back later).

Regards,
- Graeme -