11/25/2009

Extend C++ Standard IOStream Library

Why extend C++ standard IoStream library?
1. To support new data types
2. To support new devices.

The first extend method is easy - just overload the following two global operators:
     istream& operator>>(istream&, Complex&);
    ostream& operator<<(ostream&, Complex);
But the second is not so easy.

The stream library actually performs two unrelated tasks: formatting and buffering.

Formatting is the act of translating between binary data and their character representations. It is done by the class ios, the base class for both istream and ostream. The ios class keeps a format state that governs formatting. The format state specifies
  • the field width
  • the fill character
  • field alignment
  • integer base (decimal, hex or octal)
  • floating point format (fixed, scientific or general)
  • whether to show a + sign, trailing decimal point and zeros thereafter, or the integer base
  • whether to use upper- or lowercase letters for E, 0X, and the hex digits A ... F
The ios class concerns itself with formatting, the conversion between binary data and their ASCII characters representations. Transporting these characters from and to devices is the responsibility of the streambuf class and its descendants.

How to extend C++ standard stream library?

Here is a very good example: http://www.horstmann.com/cpp/iostreams.html

[Reference]
1. The Architecture of Iostream
2. Extending the iostream library
3. IOStream Online Documentation
4. Standard C++ IOStreams and Locales (Google Book Version)
5. Using Iostreams and Library Headers
6. The C++ IOStreams Library

No comments: