20th Chapter-Development Delphi Object Type Data management function (i)-(1)

Source: Internet
Author: User
Tags abstract resource

Object-oriented technology is the mainstream technology in the 90 's, all kinds of application software can make the software have higher quality if it constructs and penetrates the object-oriented style with object-oriented method. Object-oriented data management plays an important role in object-oriented programming. In Delphi, the support mode of object-type data management is one of the main features.

Delphi is an object-oriented visual design and object-oriented language integration of the integrated development environment. The core of Delphi is the component. A part is one of the objects. The Delphi application is entirely constructed of parts, so developing high-performance Delphi applications involves object-type data management technology.

Object-type data management consists of two things:

Using objects to manage data

Management of various types of data objects (including objects and components)

Delphi has done quite well in both of these areas. In the early versions of Delphi, there was a class of stream (stream), group (Collection) and resource (Resource) specifically for object data management. In Delphi, these functions have been greatly enhanced. Delphi attributes Object data management classes to stream objects (stream) and Filer objects (Filer) and applies them to all aspects of the visual Part class library (VCL). They provide not only the ability to manage objects in memory, external storage, and Windows resources, but also the functionality of objects in a database Blob field.

The implementation principles, application methods, and applications in hypermedia systems for stream objects and filer objects are described in this chapter. This is important for using Delphi to develop advanced applications.

Implementation principle and application of 20.1 flow object

Stream objects, also known as streaming objects, are Tstream, Thandlestream, TFileStream, Tmemorystream, Tresourcestream and Tblobstream, and so on collectively. They represent, respectively, the ability to store data on a variety of media, they abstract the management operations of various data types (including objects and parts) in memory, external storage, and database fields into object methods, and take advantage of the benefits of object-oriented technology, which allows applications to copy data fairly easily from a variety of stream objects.

The following describes the various objects of data and methods and how to use.

20.1.1 Tstream Object

The Tstream object is an abstract object that can store binary data in a variety of media. Objects inherited from the Tstream object are used to store data in media such as memory, Windows resource files, disk files, and database fields.

Two attributes are defined in Tstream: size and position. The size of the stream and the current pointer position, respectively, in bytes. The method defined in Tstream is used to read, write, and copy binary data among various streams. Because all stream objects are inherited from Tstream, the domains and methods defined in Tstream can be invoked and accessed by Stream objects. In addition, because of the dynamic interlock function of object-oriented technology, Tstream provides a unified interface for the application of various streams, which simplifies the use of streams. Different stream objects are abstract operations on data from different storage mediums, so The Tstream method provides the simplest means for copying data between different mediums.

Properties and methods of Tstream

1. Position Properties

Statement: Property position:longint;

The Position property indicates the current offset to read and write in the stream.

2. Size Property

Statement: Property size:longint;

The Size property indicates the amount of the stream in bytes, which is read-only.

3. CopyFrom method

Statement: function CopyFrom (source:tstream; Count:longint): Longint;

CopyFrom copies count bytes from the stream specified by source into the current stream, and moves the pointer from the current position to count bytes, and the function return value is the actual number of bytes copied.

4. Read method

Statement: function Read (var Buffer; Count:longint): Longint; Virtual Abstract

The Read method copies the contents of count bytes into buffer from the current position in the current stream and moves the current pointer back to the count number of bytes, and the function return value is the actual number of bytes read. If the return value is less than count, this means that the read operation has reached the end of the stream before it reads the desired number of bytes.

The Read method is an abstract method. Each successive stream object overwrites the method according to its own specific read operation on a particular storage medium. And all other methods of reading data for the stream (e.g. readbuffer,readcomponent, etc.) call the Read method when the actual read operation is completed. The advantages of an object-oriented dynamic binder are shown here. Because subsequent stream objects simply overwrite the Read method, other read operations (such as Readbuffer, readcomponent, etc.) do not need to be redefined, and Tstream also provides a unified interface.

5. Readbuffer method

Statement: Procedure Readbuffer (Var Buffer; Count:longint);

The Readbuffer method copies the count bytes from the stream into the buffer and moves the current pointer of the stream back to count bytes. If the read operation exceeds the tail of the stream, the Readbuffer method causes the Ereaderror exception event.

6. Readcomponent method

Statement: function Readcomponent (instance:tcomponent): tcomponent;

The Readcomponent method reads the part specified by instance from the current stream, and the function returns the part that is read. Readcomponent creates a reader object and invokes its Readrootcomponent method when reading the instance and all the objects it owns.

If the method instance for Nil,readcomponent creates a part based on the part type information described in the stream, and returns the newly created part.

7. Readcomponentres method

Statement: function Readcomponentres (instance:tcomponent): tcomponent;

The Readcomponentres method reads a instance-specified part from the stream, but the current position of the stream must be the location of the part written by the Writecomponentres method.

Readcomponentres first invokes the Readresheader method to read the resource header from the stream, and then calls the Readcomponent method to read the instance. If the current position of the stream does not contain a resource header. Readresheader will raise a Einvalidimage exception event. The Classes Library unit also contains a function called Readcomponentres that performs the same operation, except that it builds its own stream based on the resources that the application contains.

8. Readresheader method

Statement: procedure Readresheader;

The Readresheader method reads the Windows resource file header from the current position of the stream and moves the current position pointer of the stream to the end of the file header. If the stream does not contain a valid resource file header, Readresheader throws a Einvalidimage exception event.

The Readcomponentres method of the stream automatically invokes the Readresheader method before reading the part from the resource file, so it is usually not necessary for programmers to call it themselves.

9. Seek method

Statement: function Seek (offset:longint; Origin:word): Longint; Virtual Abstract

The Seek method moves the current pointer of the stream to offset bytes, and the beginning of the byte movement is specified by origin. If offset is a negative number, the Seek method moves from the beginning of the description to the head of the stream. The different values of origin and their meanings are listed in the following table:

Table 20.1 The value of the parameter of the function seek


Value of offset for the start of constant value seek


Sofrombeginning 0 The beginning positive number of the stream

The current position of the Sofromcurrent 1 stream is positive or negative

Sofromend 2 end of stream negative


Write method

There are two types of objects in the Delphi object-managed object that have a method called write: Stream object and filer object. The Write method of the Stream object writes the data into the stream. The Filer object passes data through the associated stream, which is described later in this article.

The Write method for the stream object is declared as follows:

function Write (const Buffer; Count:longint): Longint; Virtual Abstract

The Write method writes count bytes in the buffer to the stream, and moves the current position pointer to the end of the stream count bytes, and the function returns the number of bytes written.

The Write method for Tstream is abstract, and each inherited stream object provides a specific way to write data to a specific storage medium (memory, disk file, and so on) by overriding the method. All other methods of writing data for the stream, such as WriteBuffer, writecomponent, call write to perform the actual write operation.

WriteBuffer method

Statement: Procedure WriteBuffer (const Buffer; Count:longint);

The WriteBuffer function is similar to write. The WriteBuffer method calls write to perform the actual write operation, and WriteBuffer triggers a Ewriteerror exception event if the stream does not write all the bytes.

Writecomponent method

Both the stream object and the Filer object have methods called Writecomponent. The Writecomponent method of the Stream object writes the part specified by instance and all the parts it contains to the stream, and the writecomponent of the writer object writes the property value of the specified part to the stream of the writer object.

The Writecomponent method declaration for a Stream object is this:

Procedure Writecomponent (instance:tcomponent);

Writecomponent creates a writer object and invokes the writer Writerootcomponent method to write the instance and the objects it owns to the stream.

Writecomponentres method

Statement: writecomponentres (const resname:string; Instance:tcomponent);

The Writecomponentres method first writes the standard Windows resource file header to the stream, and then writes the part specified by instance to the stream. To read a part written by Writecomponentres, you must call the Readcomponentres method.

Writecomponentres uses the Resname passed-in string as the resource name for the resource file header, and then calls the Writecomponent method to write the instance and the parts it owns to the stream.

Writedescendant method

Statement: Procedure Writedescendant (Instance ancestor:tcomponent);

The Writedescendant method of the Stream object creates a writer object, and then the Writedescendant method of the object is transferred to the instance part to the stream. Instance can be a form that inherits from a ancestor part, or a part that corresponds to a ancestor part in an ancestor form in a form that inherits from an ancestor form.

Writedescendantres method

Statement: Procedure writedescendantres (const resname:string;

Instance, ancestor:tcomponent);

The Writedescendantres method writes the Windows resource file header to the stream, uses the Resname resource name, and then calls the Writedescendant method to write the instance to the stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.