Realization principle of 20.2.1.2 Tfiler object
The Tfiler object is the underlying class of the Filer object, and most of the methods it defines are abstract types and are not specifically implemented, and are overridden in Treader and Twrite. But they provide a framework for the Filer object, and it is important to understand it.
1. Implementation of Tfiler object properties
The Tfiler object defines three properties: Root, ancestor, and Ignorechildren. As with the method that defines the properties of an object, you define the data field that stores the property value in the Private section, and then define the property in the public or published section and add read and write control as needed. They are defined as follows:
Tfiler = Class (TObject)
Private
...
Froot:tcomponent;
Fancestor:tpersistent;
Fignorechildren:boolean;
Public
...
Property Root:tcomponent read Froot write Froot;
Property Ancestor:tpersistent read Fancestor write fancestor;
Property Ignorechildren:boolean read Fignorechildren write Fignorechildren;
End
Both read and write control are directly read and write private data fields.
In introducing the implementation of Treader and Twriter, we will also see the principle introduction of these attributes.
2. Implementation of Tfiler object method
Many of the methods in the Tfiler object definition are abstract class methods, not specifically implemented. These methods are overridden in the Tfiler successor object Treader. In later chapters, the implementation of these methods is described.
There are two ways to create and destroy in a Tfiler object that are specifically implemented.
The realization of ⑴create method
The Create method is a Tfiler construction method that has two parameter stream and bufsize. The stream is a stream object that specifies the Tfiler object, and the Filer object completes the specific reading and writing with the stream object. The bufsize is the size of the buffer opened inside the Tfiler object. The Filer object internally opens the buffer to speed up the reading and writing of the data, which is implemented as follows:
Constructor Tfiler.create (Stream:tstream; Bufsize:integer);
Begin
FStream: = Stream;
Getmem (Fbuffer, bufsize);
Fbufsize: = bufsize;
End
FStream, Fbuffer, and fbufsize are all Tfiler data fields defined in the private section. FStream represents a stream object that is associated with a Filer object, Fbuffer points to a buffer that is opened inside the Filer object, and the fbufsize is the size of the internal buffer. The Create method assigns values to the fstream with the stream parameter value, and then uses GETMEM to allocate the bufsize size of the dynamic memory as the internal buffer.
The realization of ⑵destroy method
The Destroy method is a destructor of the Tfiler object, and its function is to release the dynamic memory.
destructor Tfiler.destroy;
Begin
If Fbuffer <> nil then Freemem (Fbuffer, fbufsize);
End
20.2.2 Twriter Object
The Twriter object is an instantiated, filer object that writes data to the stream. The Twriter object inherits directly from the Tfiler, and in addition to overriding the methods inherited from Tfiler, there is a significant increase in the number of ways to write various data types, such as integers, strings, and component. The use of Twriter objects and Treader objects will make the object read and write play a significant role.