CodeSnip Old Code (SVN)
A repository for source code snippets, designed with Pascal in mind.
Brought to you by:
delphidabbler
--- a/trunk/Src/USnipData.pas +++ b/trunk/Src/USnipData.pas @@ -1,9 +1,8 @@ { * USnipData.pas * - * Defines interface implemented by classes that access back end Code Snippets - * database. Also implememts class that that registers factories that create - * back end database access objects. + * Defines interface implemented by classes that read and write back end Code + * Snippets database. Also provides an exception class for data I/O errors. * * $Rev$ * $Date$ @@ -113,14 +112,11 @@ uses - // Delphi - SysUtils, Classes, // Project - UExceptions, UBaseObjects, USnippets, UIStringList; + UExceptions, USnippets, UIStringList; type - { IDataReader: Interface that defines operations that must be implemented by objects that @@ -228,102 +224,6 @@ end; { - TDatabaseType: - Type of database. Used by readers so they know whether to access the - codesnip or user database. - } - TDatabaseType = ( - dtMain, // main CodeSnip database - dtUser // user's database - ); - - { - TDataIOFactoryClass: - Class reference to TDataIOFactory classes. - } - TDataIOFactoryClass = class of TDataIOFactory; - - { - TDataIOFactory: - Pure abstract base class for classes that create objects that implement - IDataReader or IDataWriter. Should not be directly overridden. Provided so - that TDataReadFactory and TDataWriterFactory can be used polymorphically. - } - TDataIOFactory = class(TNoConstructObject) - public - class function Instance(const DBType: TDatabaseType): IInterface; - virtual; abstract; - end; - - { - TDataReaderFactory: - Pure abstract base class for classes that create objects that implement - IDataReader and can access either the CodeSnip of user database. Each data - reader object must provide a factory class that derives from this class and - that creates an instance of the IDataReader object in its Instance method. - } - TDataReaderFactory = class(TDataIOFactory) - public - class function Instance(const DBType: TDatabaseType): IInterface; - override; abstract; - end; - - { - TDataWriterFactory: - Pure abstract base class for classes that create objects that implement - IDataWriter and can write the user database. Each data writer object must - provide a factory class that derives from this class and that creates an - instance of the IDataWriter object in its Instance method. - } - TDataWriterFactory = class(TDataIOFactory) - public - class function Instance(const DBType: TDatabaseType): IInterface; - override; abstract; - end; - - { - TDataIOFactories: - Static class that enables factory classes for various database reader and - writer objects to be registered and instantiated. Factories are registered - with unique IDs. The class also permits the registered IDs to be listed and - a description to be returned. - } - TDataIOFactories = class(TNoConstructObject) - strict private - class function FindEntry(const ID: string): TObject; - {Finds an internal object that describes a specified factory. - @param ID [in] Unique string that identifies a factory class. - @return Reference to internal object that describes the factory or nil - if no factory is registered with ID. - } - public - class procedure RegisterFactory(const ID: string; - const FactoryClass: TDataIOFactoryClass); - {Registers a data reader or writer factory. - @param ID [in] Unique string identifying the factory. - @param FactoryClass [in] Factory's class reference. - @except EBug raised if factory with given ID is already registered. - } - class function CreateDataReader(const ID: string; - const DBType: TDatabaseType): IDataReader; - {Creates an instance of a registered data reader object. - @param ID [in] Identifies the factory class. - @param DBType [in] Type of database to read. - @return New data reader object instance. - @except EBug raised if no factory with given ID is registered. - } - class function CreateDataWriter(const ID: string; - const DBType: TDatabaseType): IDataWriter; - {Creates an instance of a registered data writer object. - @param ID [in] Identifies the factory class. - @param DBType [in] Type of database to write. - @return New data writer object instance. - @except EBug raised if no factory with given ID is registered. - @except EBug raised if dtMain is specified in DBType. - } - end; - - { EDataIO: Class of exception raised by IDataReader and IDataWriter objects. } @@ -332,131 +232,5 @@ implementation - -uses - // Delphi - Contnrs, Windows {for inlining}; - - -var - // Private global object storing registered data reader factory classes - PvtFactoryClasses: TObjectList; - - -type - - { - TDataFactoryEntry: - Class that records information about a registered data reader factory. - } - TDataFactoryEntry = class(TObject) - public - ID: string; - {ID of factory} - FactoryClass: TDataIOFactoryClass; - {Class reference to static factory class} - end; - - -{ TDataIOFactories } - -class function TDataIOFactories.CreateDataReader(const ID: string; - const DBType: TDatabaseType): IDataReader; - {Creates an instance of a registered data reader object. - @param ID [in] Identifies the factory class. - @param DBType [in] Type of database to read. - @return New data reader object instance. - @except EBug raised if no factory with given ID is registered. - } -var - Entry: TObject; // entry representing the the required factory -begin - Entry := FindEntry(ID); - if not Assigned(Entry) then // ** do not localise - raise EBug.Create(ClassName + '.CreateDataReader: ID not found'); - // Create instance of data reader object using required factory - Result := (Entry as TDataFactoryEntry).FactoryClass.Instance(DBType) - as IDataReader; -end; - -class function TDataIOFactories.CreateDataWriter(const ID: string; - const DBType: TDatabaseType): IDataWriter; - {Creates an instance of a registered data writer object. - @param ID [in] Identifies the factory class. - @param DBType [in] Type of database to write. - @return New data writer object instance. - @except EBug raised if no factory with given ID is registered. - @except EBug raised if dtMain is specified in DBType. - } -var - Entry: TObject; // entry representing the the required factory -begin - Entry := FindEntry(ID); - if not Assigned(Entry) then // ** do not localise - raise EBug.Create(ClassName + '.CreateDataWriter: ID not found'); - if DBType = dtMain then // ** do not localise - raise EBug.Create(ClassName + '.CreateDataWriter: ID can''t be mtMain'); - // Create instance of data writer object using required factory - Result := (Entry as TDataFactoryEntry).FactoryClass.Instance(DBType) - as IDataWriter; -end; - -class function TDataIOFactories.FindEntry(const ID: string): TObject; - {Finds an internal object that describes a specified factory. - @param ID [in] Unique string that identifies a factory class. - @return Reference to internal object that describes the factory or nil if no - factory is registered with ID. - } -var - Idx: Integer; // loops thru list of registered factories -begin - Result := nil; - for Idx := 0 to Pred(PvtFactoryClasses.Count) do - begin - if AnsiSameText( - (PvtFactoryClasses[Idx] as TDataFactoryEntry).ID, ID - ) then - begin - Result := PvtFactoryClasses[Idx]; - Break; - end; - end; -end; - -class procedure TDataIOFactories.RegisterFactory(const ID: string; - const FactoryClass: TDataIOFactoryClass); - {Registers a data reader or writer factory. - @param ID [in] Unique string identifying the factory. - @param FactoryClass [in] Factory's class reference. - @except EBug raised if factory with given ID is already registered. - } -var - Entry: TDataFactoryEntry; // entry representing the the required factory -begin - // Check if factory with given ID registered already - if Assigned(FindEntry(ID)) then - raise EBug.Create( // ** do not localise - ClassName + '.RegisterFactory: factory already registered' - ); - // Create entry for new factory - Entry := TDataFactoryEntry.Create; - Entry.ID := ID; - Entry.FactoryClass := FactoryClass; - // Add entry to list - PvtFactoryClasses.Add(Entry); -end; - - -initialization - -// Create factory class registration list -PvtFactoryClasses := TObjectList.Create(True); - - -finalization - -// Free registration list -FreeAndNil(PvtFactoryClasses); - end.