File Server 2: stream and stream operators

Source: Internet
Author: User

Streaming
* The stream abstracts the external storage of data (usually objects ).
* Streaming APIs are abstract
-It provides interfaces for reading data from the storage and writing data to the storage, but does not care what the storage is
-Defined in s32std. h, and the file stream defined in s32file. h
· This header file is associated with Estor. Lib.
* Based on two key concepts
-Stream
-Stream operators

 

 

Stream
* External representation of data structures (such as objects) in the form of binary data sequences
* Access through read/write streams
* Design the C ++ class for stream implementation
-Externalizel ()
· External stream status
-Internalizel ()
· Internalized stream status
* Advantages
-More suitable for operations on raw files
-Easier to use

 

 

Externalization
* The External Object process is to write the data of this object to the stream.
* For a class instance, this process includes externalizing its data members and components to the stream.
* This process is encapsulated in the member function externalizel () of this class.
* Use the rwritestream-like write stream interface
-Allow externalizel () to write object data to the stream, regardless of the specific implementation method of the stream.
* The class generally defines the externalizel () function in the following way:
Void externalizel (rwritestream & astream) const;

Write stream
* This operation is represented by the abstract class rwritestream.
-Provide necessary interfaces for external operations to flow
* Rfilewritestream inherits from rwritestream and provides interfaces for associating to files.
-Other specific implementations, such as rstorewritestream
* Rwritestream provides external support for the following objects:
-Tint, tuint, treal, and treal64 types
-Descriptor content
-Data from the enabled Stream object reading, type: rreadstream
* The stream write operation is not executed until rwritestream: commitl () is called to buffer the stream into the stream.

 
Write stream-Example
* Use rfilewritestream: Create () to create a file
* Use rfilewritestream: open () to open an existing file

RFS & FS = ccoeenv: static ()-> fssession ();

Rfilewritestream writestream;
User: leaveiferror (writestream. Create (FS, ktxtfilename, efilewrite ));

Writestream. pushl ();/press in to clear the stack

Iobject. externalizel (writestream );

/Cleanup
Writestream. commitl ();
Writestream. Pop ();
Writestream. Release ();

Internalization
* The process of internalizing an object is to read the object data from the stream.
* For an instance of a class, this process includes reading its data members and components from the stream.
-The order of internalization is the same as that of externalization.
* This process is encapsulated by the internalizel () member function of this class.
* Use the stream reading Interface Class rreadstream
-The interanlizel () function is allowed to read object data from the stream without considering the specific implementation of the stream.
* Classes generally define internalizel () as follows ()
Void internalizel (rreadstream & astream );

 

Read stream
* Represented by the abstract class rreadstream
-Provides necessary interfaces for internalization from the stream
* Rfilereadstream inherits from rreadstream and provides interfaces associated with files.
-There are other forms of specific implementation, such as rstorereadstream.
* Rreadstream provides internal support for the following data:
-Tint, tuint, treal, and treal64 types
-Descriptor content

Stream reading-Example
* Use rfilereadstream: open () to open a file
RFS & FS = ccoeenv: static ()-> fssession ();

Rfilereadstream readstream;
User: leaveiferror (readstream. Open (FS, ktxtfilename, efileread ));

Readstream. pushl ();/press in to clear the stack

Iobject. internalizel (readstream );

/Cleanup
Readstream. Pop ();
Readstream. Release ();

Stream Operator
* There are two templated stream operators: Operator <and operator>
* Used for externalization and internalization of various types of data
* Use a syntax similar to the C ++ input/output stream
* The implementation of this operator depends on the type of the called object.

/You can use the write stream to externalize an object to a stream.
Writestream <object;
...
/Afterwards, the data can also be internalized by reading the stream
Readsteam> object;

 

 

Standard Type and class
* The storage framework provides necessary implementations for external and internal operators, including
-Basic types, such as tint8, tuint8, and treal32
-Graphic API class, such as tpoint, tsize, and trect
-UID operation API class, such as tuid
-Dynamic Buffer API class, such as cbufflat and cbufseg
* When Using stream operators for built-in types and classes
-<Operator resolved to the rwritestream function associated with the built-in type or class
· For example, rwritestream: writeint8l (tint avalue)
-> Operators are resolved to rreadstream functions associated with built-in types or classes.
· For example, rreadstream: readint8l ()

 

 

Serializable class
* A serializable class is a class that defines and implements externalizel () and internalizel ().
* For this type, the storage framework
-The operator is implemented by calling the externalizel () member function of the class. <
-You can call the internalizel () member function of this class to implement the operator.>
 
For example, iobj is a serializable object.
Iobj. externalizel (writestream)
Equivalent
Writestream <iobj

Iobj. internalizel (readstream)
Equivalent
Readstrem> iobj

 

 

Serialize tint
* Tint is a special case
-Define a minimum size (occupied space)
-The actual size is determined by the platform.
* The operator <or> cannot be used to serialize tint.
* The function must be used to specify the external size.

 

 

 

Non-class type
* For example, enumeration type
* For non-class types, specific operators must be defined and implemented.
-The operator definition must be consistent with the template definition.
* For example, for the enumerated TXXX type, operators should be implemented in the following way:
Rwritestream & operator <(rwritestream & astream, const TXXX & anxxx)
{
Astream writeint8l (anxxx );
Return astream;
}
Rreadstream & operator> (rreadstream & astream, TXXX & anxxx)
{
Anxxx = TXXX (astream. readint8l ());
}

 

 

Non-serializable class
* This class is not deterministic and implements externalizel () and internalizel () functions.
* By defining and implementing some additional global functions, you can still serialize the class.
* In fact, it is easier to define and implement externalizel () and internalizel () for a class.
* However, in a few cases, you do not need to define this function. For example, when porting a class
* All newly created classes should contain this function.

 

 

External Descriptor
* Use the writel () function of the rwritestream class
-Only write the descriptor content into the stream
-They do not write any length information.
* Use templated stream operators <
-Implemented by the storage framework, the content and length of the descriptor are written into the stream.
-It is more suitable for descriptor, because it can be easily internalized.
· Corresponding operators>
· Reload hbufc: newl () or hbufcl: newlc () to take the read stream as a parameter

 

 

Internalization Descriptor
* Use the real () member function of the rreadstream class
-Assume that the stream contains only the descriptor content.
* Use templated stream operators>
-Implemented by the storage framework, the description content and length are read from the stream.
-Reload hbufc: newl () or hbufc: newlc () to take the read stream as a parameter.
-Heap descriptors need to be allocated before being internalized from the stream.
* The premise of the last two items is that the descriptor is externalized using the templated operator <

 

Internalization of unmodifiable Descriptors
* Data cannot be read directly into unmodifiable Descriptors (tbufc, tptrc, hbufc ):
-APIs that cannot be modified, so there is no reloaded> Operator
-Use: des () to create modifiable Descriptors
-Use modifiable descriptors to access memory owned by unmodifiable Descriptors
Tbufc idataitem1;
Tptr modifiabledataitem1 = idataitem1.des ();
Astream> modifiabledataitem1;

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.