Streaming Nonpublished Component Data in Delphi 6
- Defining Properties
- An Example of DefineProperty()
- TddgWaveFile: An Example of DefineBinaryProperty()
Introduction
The Delphi IDE automatically knows how to stream the published properties of a component to and from a DFM file. But what happens when you have nonpublished data that you want to be persistent by keeping it in the DFM file? Fortunately, Delphi components provide a mechanism for writing and reading programmer-defined data to and from the DFM file.
Defining Properties
The first step in defining persistent nonpublished "properties" is to override a component's DefineProperties() method. This method is inherited from TPersistent, and defined as follows:
procedure DefineProperties(Filer: TFiler); virtual;
By default, this method handles reading and writing published properties to and from the DFM file. You can override this method, and, after calling inherited, you can call the TFiler method DefineProperty() or DefineBinaryProperty() once for each piece of data you want to become part of the DFM file. These methods are defined, respectively, as follows:
procedure DefineProperty(const Name: string; ReadData: TReaderProc; WriteData: TWriterProc; HasData: Boolean); virtual; procedure DefineBinaryProperty(const Name: string; ReadData, WriteData: TStreamProc; HasData: Boolean); virtual;
DefineProperty() is used to make standard data types such as strings, integers, Booleans, chars, floats, and enumerated types persistent. DefineBinaryProperty() is used to provide access to raw binary data, such as a graphic or sound, written to the DFM file.
For both of these functions, the Name parameter identifies the property name that should be written to the DFM file. This doesn't have to be the same as the internal name of the data field you're accessing. The ReadData and WriteData parameters differ in type between DefineProperty() and DefineBinaryProperty(), but they serve the same purpose: These methods are called in order to write or read data to or from the DFM file. (We'll discuss these in more detail in just a moment.) The HasData parameter indicates whether the "property" has data that it needs to store.
The ReadData and WriteData parameters of DefineProperty() are of type TReaderProc and TWriterProc, respectively. These types are defined as follows:
type TReaderProc = procedure(Reader: TReader) of object; TWriterProc = procedure(Writer: TWriter) of object;
TReader and TWriter are specialized descendants of TFiler that have additional methods for reading and writing native types. Methods of these types provide the conduit between published component data and the DFM file.
The ReadData and WriteData parameters of DefineBinaryProperty() are of type TStreamProc, which is defined as follows:
type TStreamProc = procedure(Stream: TStream) of object;
Because TStreamProc type methods receive only TStream as a parameter, this allows you to read and write binary data very easily to and from the stream. Like the other method types described earlier, methods of this type provide the conduit between nonstandard data and the DFM file.