BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Streaming objects to and from disk

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Marc Wetzel
Guest





PostPosted: Mon May 02, 2005 6:15 pm    Post subject: Streaming objects to and from disk Reply with quote



Hi all,

after now creating some nice base and inherited message classes,
they finally were streamed to disk using a TFilestream.

While the TBasemessage writes just a "base" header, with some kind of
basic stuff like creation-timestamp, each object adds its own data after
that.

e.g. Baseclass
TYPE
TLO_BASE = CLASS;

{$ALIGN off}
TReceiverTimestamp = TDatetime;

TLO_BASE_Header = RECORD
Magic: Cardinal; //
MsgSize: Integer; // including this
MsgClass: TLO_CLASS; // enum with distinct ids for each class
MsgType: TLO_TYPE; // eventually we need a sub-class id
MsgSource: Word;
MsgDestination: Word;
Creationtime: TReceiverTimestamp;
QueueArrival: TReceiverTimestamp;
END;
{$ALIGN on}

TLO_BASECLASSTYPE = CLASS OF TLO_BASE;



TLO_BASE = CLASS(TPersistent)
private
FUNCTION MessageSize: Integer;
FUNCTION HeaderSize: Integer;
PROCEDURE WriteHeader(CONST F: TStream);
PROCEDURE ReadHeader(CONST F: TStream);
protected
FUNCTION Payloadsize: Integer; virtual;
PROCEDURE WritePayload(CONST F: TStream); virtual; abstract;
FUNCTION GetMessageClass: TLO_CLASS; virtual;
FUNCTION GetMessageType: TLO_TYPE; virtual;
PROCEDURE Clear;
public
Header: TLO_BASE_Header;
PROCEDURE WriteToStream(CONST F: TStream); virtual;
FUNCTION asString: STRING; virtual;
FUNCTION asXML: STRING; virtual;

CONSTRUCTOR Create; overload; virtual;
PROCEDURE Assign(Source: TPersistent); override;
PROCEDURE AssignTo(Dest: TPersistent); override;
END;
....

PROCEDURE TLO_BASE.WriteToStream(CONST F: TStream);
BEGIN
Header.MsgSize := Messagesize;
WriteHeader(F);
WritePayload(F);
END;
****************************************************************

inherited String class overrides WritePayload()

PROCEDURE TLO_String.WritePayload(CONST F: TStream);
BEGIN
F.WriteBuffer(Msg[1], Length(Msg));
END;

Finally this works pretty well for all the inherited classes,
as they all override the needed functions, and all have their own id
(set in the header) that each data-representation could be loaded
back and assigned to an object. (right now only checked with a hex-editor)

My problem is the following:

I want to have a Generator class (some kind of a factory?) which can
produce the different object types with the help of a filestream.

Is it possible for a constructor to return a class-type of a different
class-type?

How do I do this?

Should I overload a pseude-constructor which creates a class for each
repeated call to it? ( Maybe something like Application.CreateForm ?)

Is there a better way to load an object from disk as I have done with
the distinct ids?

Better than, e.g. (att! not real code)

function TLO_Reader.StreamGetObject(const F:Filestream);
var X: TLO_BASE;
Begin
ReadHeader(F);
If Header.MsgClass = LO_BASE Then X :=
TLO_BASECLASSTYPE(TLO_BASE).Create;
If Header.MsgClass = LO_STRING Then X :=
TLO_BASECLASSTYPE(TLO_STRING).Create;
....
If Assigned(X) Then
X.ReadPayload(F);
End


I have the feeling that there is a nicer OO way to accomplish this :)


TIA
/Marc

Back to top
krisztian pinter
Guest





PostPosted: Tue May 03, 2005 8:25 am    Post subject: Re: Streaming objects to and from disk Reply with quote




TPersistent descendants can automatically stream themselves.

if you want to do it yourself, stream the class name
(Obj.ClassName) as a part of the base info, and then use

function FindClass(const ClassName: string): TPersistentClass;

to get a class type from class name. like:

Obj := FindClass(ObjClsName).Create;



Marc Wetzel <mwse_n_o_s_p_a_m (AT) gmx (DOT) de> wrote:
Quote:
Hi all,

after now creating some nice base and inherited message classes,
they finally were streamed to disk using a TFilestream.

While the TBasemessage writes just a "base" header, with some kind of
basic stuff like creation-timestamp, each object adds its own data after
that.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.