Difference between revisions of "Streaming components"

From Free Pascal wiki
Jump to navigationJump to search
Line 3: Line 3:
 
Normally, when you want to store data on disk or to network streams, you must write code for loading and saving each property.
 
Normally, when you want to store data on disk or to network streams, you must write code for loading and saving each property.
 
This tutorial describes how to write classes, that can be loaded from and saved to streams without writing extra load/save code by using the RTTI.
 
This tutorial describes how to write classes, that can be loaded from and saved to streams without writing extra load/save code by using the RTTI.
 +
 +
There is an example in the lazarus sources, demonstrating how to save a TGroupBox with a TCheckBox child to a stream and read the stream back to create a copy of both components.
 +
  See <lazaruspath>/examples/componentstreaming/
  
 
== TComponent / TPersistent ==
 
== TComponent / TPersistent ==
Line 9: Line 12:
  
 
'''TComponent''' extends TPersistent by the ability to have child components. This is important for streaming, where one component is the '''root component''' also called '''lookup root''' with a list of child components.
 
'''TComponent''' extends TPersistent by the ability to have child components. This is important for streaming, where one component is the '''root component''' also called '''lookup root''' with a list of child components.
 +
 +
== Streamable properties ==
 +
 +
There are some limitations, what TReader/TWriter can stream:
 +
 +
* Base types can be streamed: string, integer, char, single, double, extended, byte, word, cardinal, shortint, etc. .
 +
* TPersistent and descendants can be streamed
 +
 +
* records, objects and classes not descending from TPersistent can not be streamed. To stream them you need to tell TReader/TWriter how. See below [[#Streaming custom Data - DefineProperties]].
 +
 +
== Streaming custom Data - DefineProperties ==

Revision as of 16:46, 23 February 2006

Introduction

Normally, when you want to store data on disk or to network streams, you must write code for loading and saving each property. This tutorial describes how to write classes, that can be loaded from and saved to streams without writing extra load/save code by using the RTTI.

There is an example in the lazarus sources, demonstrating how to save a TGroupBox with a TCheckBox child to a stream and read the stream back to create a copy of both components.

 See <lazaruspath>/examples/componentstreaming/

TComponent / TPersistent

The class TPersistent is defined in the unit Classes and is uses the {$M+} compiler switch. This switch tells the compiler to create Run Time Type Information (RTTI). This means it and all its descendants get a new class section published. 'Published' properties are visible as 'public', but additonally their structure is accessible at run time. That means all published properties can be read and written at run time. The IDE for instance uses this to work with components it never heard of.

TComponent extends TPersistent by the ability to have child components. This is important for streaming, where one component is the root component also called lookup root with a list of child components.

Streamable properties

There are some limitations, what TReader/TWriter can stream:

  • Base types can be streamed: string, integer, char, single, double, extended, byte, word, cardinal, shortint, etc. .
  • TPersistent and descendants can be streamed

Streaming custom Data - DefineProperties