The 20th Chapter-Development Delphi Object Type Data management function (four) (3)

Source: Internet
Author: User
Tags count

2. Implementation of DefineProperty and Definebinaryproperty methods

These two methods are virtual methods, which are abstract methods in Tfiler and have specific implementations in Treader and Twriter objects.

They are implemented in Treader as follows:

Procedure Treader.defineproperty (const name:string; Readdata:treaderproc;

Writedata:twriterproc; Hasdata:boolean);

Begin

If Comparetext (Name, fpropname) = 0 Then

Begin

ReadData (Self);

Fpropname: = ';

End

End

Procedure Treader.definebinaryproperty (const name:string;

ReadData, Writedata:tstreamproc; Hasdata:boolean);

Var

Stream:tmemorystream;

Count:longint;

Begin

If Comparetext (Name, fpropname) = 0 Then

Begin

If ReadValue <> Vabinary Then

Begin

Dec (Fbufpos);

Skipvalue;

Fcanhandleexcepts: = True;

Propvalueerror;

End

Stream: = tmemorystream.create;

Try

Read (Count, SizeOf (count));

Stream.setsize (Count);

Read (stream.memory^, Count);

Fcanhandleexcepts: = True;

ReadData (Stream);

Finally

Stream.free;

End

Fpropname: = ';

End

End

In all two methods, the name parameter value is compared to the current property name, and if the same is read. In Definebinaryproperty, a memory stream was created. Read the data into the memory stream and then call ReadData to read the data.

3. Implementation of Flushbuffer

The Flushbuffer method is used to clear the contents of the reader object's internal buffer, keeping the reader object synchronized with the stream at position (Position), which is implemented as follows:

Procedure Treader.flushbuffer;

Begin

Fstream.position: = fstream.position-(fbufend-fbufpos);

Fbufpos: = 0;

Fbufend: = 0;

End

4. Readlistbegin, Readlistend and Endoflist methods

These three methods are used to read a series of items from the stream of the reader object, and these items are writelistbegin written by the mark of the start and Writelistend write flags, calibration end, in the reading loop with Endoflist to judge. They are often used when the reader object reads the data in the stream. They are implemented as follows:

Procedure Treader.readlistbegin;

Begin

CheckValue (valist);

End

Procedure Treader.readlistend;

Begin

CheckValue (Vanull);

End

function TReader.EndOfList:Boolean;

Begin

Result: = ReadValue = Vanull;

Dec (Fbufpos);

End

The project table start flag is valist, and the project table end flag is vanull,valist and vanull are constants defined by enumerated type Tvaluetype.

The CheckValue called in their implementation is the Treader private method, which is implemented as follows:

Procedure Treader.checkvalue (Value:tvaluetype);

Begin

If ReadValue <> Value Then

Begin

Dec (Fbufpos);

Skipvalue;

Propvalueerror;

End

End

The function of the CheckValue method is to detect whether the value that is to be read immediately is the type specified by value. If not, skip the item and trigger a sinvalidpropertyvalue error.

The Endoflist function simply determines whether the next byte is vanull returns the result of the judgment and moves the byte back to its original position.

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.