Thread: Inheriting from basic_iostream and streambuf

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    Inheriting from basic_iostream and streambuf

    Hello!

    I am writing a library for an embedded system where I want to provide the user with an std::basic_iostream interface to some peripheral (let's say an UART).

    The straightforward way to do this is to derive from std::streambuf, and wrap it with a std::basic_iostream.

    The problem is I also want to provide an asynchronous IO interface, which basic_iostream doesn't support, and the user can no longer (easily) get to my class with the basic_iostream wrapper.

    How would you design this?

    I can have my class inherit from both iostream and streambuf, and pass "this" as the streambuf to iostream ctor, but that feels dirty...

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Can you show some example code?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    It is not normally advised to inherit from the iostream class. The istream and ostream classes are really not designed to be a base class. See this link for a little more information.

    By the way your example (an UART) does not usually require an asynchronous input stream. Realize that UART stands for Universal Asynchronous Receiver-Transmitter.
    Last edited by jimblumberg; 08-27-2018 at 12:30 PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by jimblumberg View Post
    It is not normally advised to inherit from the iostream class.
    He's saying he wants to inherit from basic_streambuf and then "wrap" that in a basic_iostream by passing his streambuf object to basic_iostream's constructor. I'm assuming a little, though, but that's all that makes sense.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Can you show some example code?
    I am still in the early planning stages so unfortunately not.

    Quote Originally Posted by jimblumberg View Post
    It is not normally advised to inherit from the iostream class. The istream and ostream classes are really not designed to be a base class. See this link for a little more information.

    By the way your example (an UART) does not usually require an asynchronous input stream. Realize that UART stands for Universal Asynchronous Receiver-Transmitter.
    Yeah, it's looking like providing a streambuf for the user to wrap in her own iostream is a better way to go.

    By async IO I mean the user needs to be able to check that some data is available in either the iostream buffer or streambuf buffer (in this case, UART receive FIFO) before reading, so reads don't block. Though I just found readsome() in istream, which, although very basic, should support my use case. I should probably just implement streambuf then.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    As it turned out, I still had to derive from both streambuf and iostream, because readsome() is the only non-blocking call supported by the iostream interface, and it's seriously inadequate. There really needs to be something like readsome() but doesn't extract data. streambuf even provides in_avail(). Not sure why iostream doesn't expose that.

    The class ended up being something like this:
    Code:
    class MySerial : public std::iostream, public std::streambuf {
     public:
      MySerial() : std::iostream(this) {}
    
     protected:
      // overriding functions for streambuf
    }
    It seems to be working perfectly fine, and I can provide my own non-blocking interface alongside.

  7. #7
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by cyberfish View Post
    As it turned out, I still had to derive from both streambuf and iostream, because readsome() is the only non-blocking call supported by the iostream interface, and it's seriously inadequate. There really needs to be something like readsome() but doesn't extract data. streambuf even provides in_avail(). Not sure why iostream doesn't expose that.

    The class ended up being something like this:
    Code:
    class MySerial : public std::iostream, public std::streambuf {
     public:
      MySerial() : std::iostream(this) {}
    
     protected:
      // overriding functions for streambuf
    }
    It seems to be working perfectly fine, and I can provide my own non-blocking interface alongside.
    Yeah C++ streams can be a real headache. (Badly designed, IMHO). Good to know that a reasonable inheritance-based approach is actually feasible.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by Sir Galahad View Post
    Yeah C++ streams can be a real headache. (Badly designed, IMHO). Good to know that a reasonable inheritance-based approach is actually feasible.
    Yeah definitely. I was reading an article on tips on how to use iostreams, before realizing it was actually written in the early 1990s before standardization! And they have stayed more or less the same since then!

    It was a truly innovative design for its time (abstract and generic data sources and sinks), but really starting to show its age.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Streambuf overloading with sputbackc
    By cppanon42569 in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2017, 03:23 AM
  2. streambuf
    By Dae in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2009, 09:44 AM
  3. basic_iostream parameters
    By DL1 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2009, 04:21 AM
  4. Trying to override streambuf to use with cout
    By rob_l_f in forum C++ Programming
    Replies: 6
    Last Post: 01-12-2009, 01:31 AM
  5. problem with streambuf
    By vin_pll in forum C++ Programming
    Replies: 18
    Last Post: 06-22-2008, 04:13 AM

Tags for this Thread