C++ STL - inheriting from ostream

Started by
6 comments, last by randomZ 19 years, 8 months ago
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!
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Advertisement
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]
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.
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.
snk_kid: can you write a simple example please?!
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.
My suspicion is that the kind of transformations you want to perform should be done with a codecvt.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/

This topic is closed to new replies.

Advertisement