Home arrow C++ arrow Page 4 - Extending the Basic Streams in C++

Extending the Basic Streams in C++

The basic stream library (iostream) is designed to resolve most of the general problems you may run into during your programming endeavors. However, sometimes it simply isn't enough, and you need to extend it to suit your application—your special situation. This article will show you how to do that.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
May 19, 2009
  1. · Extending the Basic Streams in C++
  2. · The Source of it All
  3. · The streambuff Class
  4. · Epilogue

print this article

Extending the Basic Streams in C++ - Epilogue
(Page 4 of 4 )

With this, you know enough about the class that you should already have a sharp idea in mind of how you can use it. Let me also mention that other points of interest in this class are two more functions. The get and put xsgetn() [inside it will be called a secure version the _Xsgetn_s()] and the xsputn(). The creators and further developers of the STL will put in front of the functions the character x to distinguish it from the ones you write.

This will treat every aspect of your stream and get or put char into areas accordingly. If you break down the code, you will see that in the case of an unbuffered stream you will get an uflow () for the get area and the overflow() in the case of the put area. The same is true for a buffered stream, but in addition, now we need to worry about more characters. 

For this, I will not insert the code directly here, as it is rather long and you can find it inside the streambuff class of your compiler. Now only one more unanswered question remains: how do we transform the data from the put area to the consumer?

Just like the proper print on the screen in the case of the console window, the function that is responsible for doing this even in the case of the iostream classes is the virtual function sync() inside the stream buffer.

This will handle the job, but the question is, do you need to call this function after every time you work on the buffer? This can turn out to be a quite annoying and boring job, so the developers came up with a better idea that you can observe in the iostream library.

Each time the buffer is touched, a so-called basic_ostream<>::sentry is created. When the function terminates on the sentry's destructor inside the osfx() function, the flush() is called if the unitbuf flag bit is on.

Unitbuf is one of the many format flags of the Iostream Library. This explicitly points out that the buffer should be flushed after each output. It is highly recommended to leave this one on, but you may turn it off if you are in pursuit of a great performance and you know that you do not need up to date synchronization. Be aware if you turn this off, only one output will be synchronized at a time.

cout << " Developer Shed";

This would result in only Developer output; however, in a future output operation, the Shed will also be printed, as it was put inside the buffer.

The basic_istream::sentry does the same work on the constructor, as you need to synchronize up before the reading starts. In the end, you are free to implement this in whatever way you find best; the presented solution is just an example of how it can be done following the standards. The streams used for the file handling (including the cin/cout) are not overwriting the xsgetn() and xsputn() functions as they follow the basic conventions.

Of course, the list of the existing functions is still large, and many more can be done, but you rarely use them and this will give you a basic idea as to how the streams are built a little more explicitly than my Introduction to Streams article pointed out. The basic implementation for other virtual functions will all return _Tr::eof() so it will not produce eventual unexpected errors.

Deriving from the streambuff class allows you to create an Input/output system based on your personal needs and starting from steady ground. The possibilities where this can be used are endless; all you need is to put your neurons to work.

Network communication can profit from streams the most. Making communication a piece of cake is perfectly achievable. Writing a game inside the communication can have many input endpoints and output endpoints, while all of them stay synchronized, allowing the players to maintain a conference.

With a little coding patience and commitment, much can be achieved. You may use this article as a starting point for that, or just to comprehend the buildup of the I/O feature of the C++ via the streams a little more. Feel free to ask the people around (including me) additional questions here in the blog section or over at the DevHardware Forums.

Live with passion!

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials