Sign in to follow this  
randomZ

C++ STL - inheriting from ostream

Recommended Posts

randomZ    163
Hi, I'm trying to write a class that basically encapsulates an ostream, providing the same functions on it as are available anyway, plus adding some more (several kinds of Unicode conversions, to be specific). To sketch out my design:
class my_ostream : public ostream
{
    private:
        ostream& realOstream;
    public:
        my_ostream(ostream& strm)
        : realOstream(strm)
        { }

        my_ostream& put(char ch) { realOstream.put(ch); return *this; }
        // and so on, all the standard functions

        my_ostream& writeUtf32le(const string32& str)
        { /* string32 is my own type */ }
};


Well, anyway, the problem I'm having is, should I really inherit from ostream for this, or from basic_ios? ostream seems to have all its member functions already defined, although I don't even know where it's writing its stuff to... can anyone help me with this? Thanks!

Share this post


Link to post
Share on other sites
snk_kid    1312
ostream is a typedef for basic_ostream with char type passed in it's type parameter. The way the standard library iostreams basically works is basic_iostream has a pointer to a basic_streambuf type which is a base type.

for all the other kinds of streams such as basic_fstream when constructed creates & assigns a derived version of basic_streambuf called basic_filebuf to that pointer in it's super-type basic_iostream.

All of the public methods of basic_iostream are not virtaul because they use a template method where they delegate certain parts of the function's task to protected virtual methods that derive from basic_iostream and provide implementation it's like a skeleton of the algorithm. basic_iostream also delegates the task of character transportation to it's member, the pointer to type basic_streambuf.

When you derive from basic_iostream all you have to do is override particular set of protected virtual methods declared in basic_iostream (they provide default behaviour). Also you need to instantiate a derived type of basic_streambuf and pass it to basic_iostream constructor. Lucky for you the standard iostreams & locales book is some how online here.

I don't know much about unicode characters but there are typedefs of iostreams for wide character sets so you may not even need to create a new kind of stream.

[Edited by - snk_kid on August 5, 2004 7:23:12 PM]

Share this post


Link to post
Share on other sites
Chris Hare    462
Excellent post, snk_kid. I'm not familiar with the <iostream> heirachy, but my <ostream> source seems to suggest that the only ostream constructor useful here would be the copy constructor. Essentially, the private reference you have is redundant because of the base class.

If what you want can be done by extension of ostream (though I'm sure there would be issues), then go for it, but I still think that a few standalone functions would suffice.

Share this post


Link to post
Share on other sites
snk_kid    1312
I think i've made a slight mistake, basic streams don't have protected virtual methods it's basic_streambuf that does. The basic stream types work with the locale & facet types & use basic_streambuf to delegate character transportation.

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by lucaz
snk_kid: can you write a simple example please?!


There are lots of ways to extend the iostream library, by extending the stream types for new kind of formatting which you can do by either deriving or using iword/pword, creating new manipulator types. Extneding basic_streambuf for a new devices for character transportation. The link i provided above shows you how with examples.

Share this post


Link to post
Share on other sites
randomZ    163
Well, just for the record, this is what I did after doing some more thinking: I derived my class from ostream and linked its streambuf to that of another ostream.

I get the feeling that some of you slightly misunderstood what I wanted (I probably haven't expressed it very clearly). I wanted my stream to still write in bytes, not in wchars. Also, wchars don't allow you to select the encoding scheme to use, so they're not really useful.

Now I have my own OutStream class that can "wrap" an ostringstream, fstream etc. object and write Unicode data to it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this