Delphi's component reading and writing mechanism (1)
1. Introduction to streaming objects (Stream) and read-write objects (Filer)
In object-oriented programming, object-based data management plays an important role. In Delphi, the support for object-based data management is a major feature.
Delphi is an integrated development environment that combines object-oriented visual design with object-oriented language. The core of Delphi is components. Component is a type of object. Delphi applications are entirely constructed from components, so developing high-performance Delphi applications will inevitably involve object-based data management technology.
Object data management includes two aspects:
● Use objects to manage data
● Management of various data objects (including objects and components)
Delphi summarizes object-based data management classes into Stream objects (Stream) and Filer objects (Filer), and applies them to all aspects of the Visual Component Class Library (VCL). They provide rich functions for managing objects in memory, external storage and Windows resources.
Stream object, also known as streaming object, is the collective name for TStream, THandleStream, TFileStream, TMemoryStream, TResourceStream and TBlobStream. They respectively represent the ability to store data on various media. They abstract the management operations of various data types (including objects and components) in memory, external memory and database fields into object methods, and make full use of object-oriented technology. The advantage is that applications can copy data in various Stream objects fairly easily.
Read-write objects (Filer) include TFiler objects, TReader objects and TWriter objects. The TFiler object is the basic object for file reading and writing. TReader and TWriter are mainly used in applications. Both TReader and TWriter objects inherit directly from TFiler objects. The TFiler object defines the basic properties and methods of the Filer object.
The Filer object mainly completes two major functions:
● Access form files and components in form files
●Provide data buffering to speed up data reading and writing operations
In order to have a perceptual understanding of streaming objects and read-write objects, let's first look at an example.
a)Write a file
PRocedure TFomr1.WriteData (Sender: TObject); r;
Var
FileStream:TFilestream;
Mywriter:TWriter;
i: integer
Begin
FileStream:=TFilestream.create('c:/Test.txt',fmopenwrite);//Create a file stream object
Mywriter:=TWriter.create(FileStream,1024); //Associate Mywriter with FileStream
Mywriter.writelistbegin; //Write list start flag
For i:=0 to Memo1.lines.count-1 do
Mywriter.writestring(memo1.lines[i]); //Save the text information in the Memo component to the file
Mywriter.writelistend; //write list end mark
FileStream.seek(0,sofrombeginning); //The file stream object pointer moves to the beginning of the stream
Mywriter.free; //Release the Mywriter object
FileStream.free; //Release the FileStream object
End;
b) Read files
procedure TForm1.ReadData(Sender: TObject);
Var
FileStream:TFilestream;
Myreader:TReader;
Begin
FileStream:=TFilestream.create('c:/Test.txt',fmopenread);
Myreader:=TRreader.create(FileStream,1024); //Associate Myreader with FileStream
Myreader.readlistbegin; //Read out the written list start mark
Memo1.lines.clear; //Clear the text content of the Memo1 component
While not myreader.endoflist do //Pay attention to a method of TReader: endoflist
Begin
Memo1.lines.add(myreader.readstring); //Add the read string to the Memo1 component
End;
Myreader.readlistend; //Read out the written list end mark
Myreader.free; //Release the Myreader object
FileStream.free; //Release the FileStream object
End;
The above two processes, one is a writing process and the other is a reading process. The writing process passes through TWriter and uses TFilestream to save the content (text information) in a Memo as a binary file saved on the disk. The reading process is just the opposite of the writing process. Through TReader, TFilestream is used to convert the contents of the binary file into text information and display it in Memo. When you run the program, you can see that the reading process faithfully restores the information saved by the writing process.
The following diagram describes the relationship between data objects (including objects and components), streaming objects, and read and write objects.
Figure (1)
It is worth noting that read-write objects such as TFiler objects, TReader objects, and TWriter objects are rarely directly called by application writers. They are usually used to read and write the state of components. They play a very important role in the read-write component mechanism. important role.
For the streaming object Stream, many reference materials have very detailed introductions, while reference materials for TFiler objects, TReader objects and TWriter objects, especially component reading and writing mechanisms, are rare. This article will trace the VCL original code. Analyze the component reading and writing mechanism.
2. Reading and writing objects (Filer) and component reading and writing mechanisms
The Filer object is mainly used to access Delphi form files and components in the form file, so to clearly understand the Filer object, you must know the structure of the Delphi form file (DFM file).
DFM files are used for Delphi storage forms. Form is the core of Delphi visual programming. The form corresponds to the window in the Delphi application, the visual components in the form correspond to the interface elements in the window, and the non-visual components such as TTimer and TOpenDialog correspond to a certain function of the Delphi application. The design of Delphi applications is actually centered on the design of the form. Therefore, DFM files also occupy a very important position in Delphi application design. All elements in the form, including the properties of the form itself, are included in the DFM file.
In the Delphi application window, the interface elements are related to each other according to the ownership relationship, so the tree structure is the most natural form of expression; accordingly, the components in the form are also organized according to the tree structure; correspondingly in the DFM file, also To express this relationship. DFM files are physically stored in text format (before Delphi2.0 version, they were stored as binary files), and logically, the relationships between components are arranged in a tree structure. The tree structure of the form can be seen clearly from this text. Below is the content of the DFM file:
object Form1: TForm1
Left = 197
Top = 124
…
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 272
…
Caption = 'Button1'
TabOrder = 0
end
object Panel1: TPanel
Left = 120
…
Caption = 'Panel1'
TabOrder = 1
object CheckBox1: TCheckBox
Left = 104
…
Caption = 'CheckBox1'
TabOrder = 0
end
end
end
This DFM file is generated by TWriter through the streaming object Stream. Of course, there is also a conversion process from a binary file to a text information file. This conversion process is not the object of this article, so such a process is ignored.
When the program starts running, TReader reads the forms and components through the streaming object Stream, because when Delphi compiles the program, it has compiled the DFM file information into the executable file using the compilation instruction {$R *.dfm} , so what TReader reads is actually information about forms and components that is compiled into the executable file.
TReader and TWriter can not only read and write most of the standard data types in Object Pascal, but also can read and write advanced types such as List and Variant, and even read and write Properties and Components. However, TReader and TWriter themselves actually provide very limited functions, and most of the actual work is done by the very powerful class TStream. In other words, TReader and TWriter are actually just tools. They are only responsible for reading and writing components. The specific reading and writing operations are completed by TStream.
Since TFiler is the common ancestor class of TReader and TWriter, to understand TReader and TWriter, you should start with TFiler first.