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 -
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to