Solved

Example of Tpersitant Class

Posted on 2006-11-24
10
1,350 Views
Last Modified: 2012-06-27
Hi
       Iam Abu.I am using Delphi-5.I just want to learn about Tpersistant and how to use Tperstiant with small example.Everyone says it is used for streaming of data.But i dont know what is streaming in delphi with easy example
Thanks
Abu
0
Comment
Question by:abunectar
  • 5
  • 5
10 Comments
 
LVL 28

Expert Comment

by:ciuly
ID: 18007131
that means that it can be save to a stream. a stream can be anything from a memory stream to a filestream or some custom stream. (you should look up stream in you language dictionary if you do not know the meaning of the word)

TPersistent offers the means (functions) to persist a class easily (save and load to and from a stream). so any class that extends TPersistent will have it's published properties save to a stream.

quote from delphi help:

TPersistent is the ancestor for all objects that have assignment and streaming capabilities.

Unit

Classes

Description

TPersistent encapsulates the behavior common to all objects that can be assigned to other objects, and that can read and write their properties to and from a form file (.xfm or .dfm file). For this purpose TPersistent introduces methods that can be overridden to:

Define the procedure for loading and storing unpublished data to a stream.
    Provide the means to assign values to properties.
    Provide the means to assign the contents of one object to another.

Do not create instances of TPersistent. Use TPersistent as a base class when declaring objects that are not components, but that need to be saved to a stream or have their properties assigned to other objects.

all components are derived from TPersistent which allows them to be save and loaded from a stream. specifically, if you look in a dfm file, you will notice that it is a text file with the components from a form saved in there.

some examples of use:

http://www.swissdelphicenter.ch/torry/showcode.php?id=784
http://www.swissdelphicenter.ch/torry/showcode.php?id=785
http://www.delphipages.com/news/detaildocs.cfm?ID=145
0
 
LVL 1

Author Comment

by:abunectar
ID: 18083194
In DFM when we right click and view as text,we see stream.Is that created bt Tpersistant class
0
 
LVL 28

Expert Comment

by:ciuly
ID: 18083360
well, you could say that it's a stream though I never heard anyone reffering to the contents of a dfm as a stream :)
when you look in a dfm you will see something like:

object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 224
    Top = 200
    Width = 75
    Height = 25
    Caption = 'save'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 312
    Top = 200
    Width = 75
    Height = 25
    Caption = 'load'
    TabOrder = 1
    OnClick = Button2Click
  end
and continues with rest of the components from the form. But only components will be saved there. tpersistent only gives the idea how to do it. the base support. it's tcomponent that does the hard work in the case of dfm saving/loading.

the above dfm contains an object Form1 of type TForm1 which also contains a few objects, button1 and button2. all objects have their published properties saved (in the form: property name = property value).

if you look in tstream class you will notice 2 functions: ReadComponent and WriteComponent which are called when you want to save a component to a stream. those functions will iterate through the published properties of teh comonent and save them to the stream. this is basically how that dfm is betting to be a text file.

now, what TPersistent actually does in this case is just make the inherited classes to also inherit it's compilation settings, which if you look in the delphi sources (if you have the right delphi version, not personal or whatever) you will see that tpersistent is defined as:

{$M+}

  TPersistent = class(TObject)
  private
    procedure AssignError(Source: TPersistent);
  protected
    procedure AssignTo(Dest: TPersistent); virtual;
    procedure DefineProperties(Filer: TFiler); virtual;
    function  GetOwner: TPersistent; dynamic;
  public
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); virtual;
    function  GetNamePath: string; dynamic;
  end;

{$M-}

the key thing here is the {$M+} about which delphi help says:
Type      Switch
Syntax      {$M+} or {$M-}
{$TYPEINFO ON} or {$TYPEINFO OFF}
Default      {$M-}
{$TYPEINFO OFF}
Scope      Local
The $M switch directive controls generation of runtime type information (RTTI). When a class is declared in the {$M+} state, or is derived from a class that was declared in the {$M+} state, the compiler generates runtime type information for fields, methods, and properties that are declared in a published section. If a class is declared in the {$M-} state, and is not derived from a class that was declared in the {$M+} state, published sections are not allowed in the class. Note that if a class is forward declared, the first declaration of the class must be declared with the $M switch.

When the $M switch is used to declare an interface, the compiler generates runtime type information for all properties and methods. That is, for interfaces, all members are treated as if they were published.

Note:      The TPersistent class defined in the Classes unit of the component library is declared in the {$M+} state, so any class derived from TPersistent will have RTTI generated for its published sections. The component library uses the runtime type information generated for published sections to access the values of a component's properties when saving or loading form files. Furthermore, the IDE uses a component's runtime type information to determine the list of properties to show in the Object Inspector.

Note:      The IInvokable interface defined in the System unit is declared in the {$M+} state, so any interface derived from IInvokable will have RTTI generated. The routines in the IntfInfo unit can be used to retrieved the RTTI.

There is seldom, if ever, any need for an application to directly use the $M compiler switch.

----------

so, basically, the tstream class will use RTTI to save the component to the stream. and TComponent inherits from TPersistent so it also inherits the {$M+}

that is why TPersistent is the base class for any class that needs persistance: so you don't have to remember to add the {$M+}/{$M-} switches and to have a base class that defines the base functionality (see tpersistant definition above).

this allows you to write for example:

type
  ta=class(tpersistent)
  end;

  tb=class(tpersistent)
  end;

procedure whatever;
var a:ta; b:tb;
begin
  // initialize a,b
  a.assign(b);
end;

You should not get into the deep dark ways of *how it's done* right now. try concentrating on "how to use" instead. the 3 examples I've given you a few days back shold be a starting point. you will not interface directly with tpersistant, but through the classes that extend it, so it's best to get familiar with thos for starters and tcomponent is maybe the easiest (or at least the most used :) )
0
 
LVL 1

Author Comment

by:abunectar
ID: 18083482
Thanks cuily for ur quick response.Sorry for days i delayed.Iam really struggling hard to understand the example u have given.Thats why i came into assumption that Text file in dfm is due to tPeristent class.But iam wondering about the o/p. Since there is no output iam struggling hard to find the workings of Tpersistant.And more over iam jus a beginner in delphi-5
0
 
LVL 28

Expert Comment

by:ciuly
ID: 18083500
let's look at the problem from another angle: why are fixated on tpersistent? you don't work with it directly anyway so I don't see why spend time understanding it. all you have to know is that it provides a few methods for copying object properties from one to another and that it enables RTTI (Run Time Type Information) for all classes that inherit from it.
so again: why do you want to understand how TPersistent work? it's not tpersistent itself that implements the persitency: as I said, it only enables it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:abunectar
ID: 18083558
At interview point they are asking what is tpersistant and how it is useful.Thats why iam asking this question to u
0
 
LVL 28

Accepted Solution

by:
ciuly earned 100 total points
ID: 18083593
well ... at its simplest description: tpersistent is a class that enables RTTI for all classes that extend it and provides means of copying object properties among eachother. it's usefull for cloning objects, saving them to streams or any other operation that involves knowing the object properties and their values.
you can mix the above with a few info from the help quotes I already posted.

and if they want detailed tehnical information, then the job is probably not for you since you'll have to start learning about RTTI for starters, which itself is quite a big chapter (and very tehnical).
0
 
LVL 1

Author Comment

by:abunectar
ID: 18083673
Thanks Ciuly for the excellent defnition
0
 
LVL 1

Author Comment

by:abunectar
ID: 18083712
Ciuly if u dont mind kindly tell me the usage of rtti
Thank
Abu
0
 
LVL 28

Expert Comment

by:ciuly
ID: 18083734
you can start from here: http://www.blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm
it's a pretty good and easy article in my opinion. then you can search for some more, on specific things if you will want to know more specific things. The above article however should give you a picture of what you can do with rtti
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now