More help with << and >>

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I know I keep asking similar questions, but I really want to do this
at least sort of right. Not to mention I got no on-group replies to
my previous post :( I desperately want an interface that can allow
classes that implement it to act sort of like streams, but I just
can't seem to get everything I want...

class Writable
{
protected:
ostringstream outbuf; // or should I use private inheritance?
virtual void WriteData( std::string& s )=0;

public:
Writable& operator<< (const char *cp); // buffers it
template <class T>
Writable& operator<< (const T& t) {outbuf<<t; return *this;}
void Flush() {WriteData(outbuf.str()); outbuf.str("");}
};

So that's great, it all works like a charm. But I'm trying to find a
nice way to actually get Flush() called, and I just can't seem to find
the way to do it... I've tried adding

Writable& operator<< ((*f)(Writable& w)) {f(*this); return *this;}

to Writable and defining

Writable& flush( Writable& w ) {w.Flush(); return w;}

to let me do things like

a << 3 << flush << my_var << flush; // a is a class implementing Writable

but of course it doesn't work thanks to Writable's template function.
Is there any way I can make this work the way I want?
 
J

John Harrison

Christopher Benson-Manica said:
I know I keep asking similar questions, but I really want to do this
at least sort of right. Not to mention I got no on-group replies to
my previous post :( I desperately want an interface that can allow
classes that implement it to act sort of like streams, but I just
can't seem to get everything I want...

class Writable
{
protected:
ostringstream outbuf; // or should I use private inheritance?
virtual void WriteData( std::string& s )=0;

public:
Writable& operator<< (const char *cp); // buffers it
template <class T>
Writable& operator<< (const T& t) {outbuf<<t; return *this;}
void Flush() {WriteData(outbuf.str()); outbuf.str("");}
};

So that's great, it all works like a charm. But I'm trying to find a
nice way to actually get Flush() called, and I just can't seem to find
the way to do it... I've tried adding

Writable& operator<< ((*f)(Writable& w)) {f(*this); return *this;}

to Writable and defining

Writable& flush( Writable& w ) {w.Flush(); return w;}

to let me do things like

a << 3 << flush << my_var << flush; // a is a class implementing Writable

but of course it doesn't work thanks to Writable's template function.
Is there any way I can make this work the way I want?

Yes derive from streambuf, that is the usual way to create your own stream
types. You leave the already defined interface for istream/ostream alone
(instead trying to duplicate it as you are doing) and just use a streambuf
derived class to direct where the input/output is really going. Get a good
book on the standard library for details on how do to this.

I haven't seen anything in the code you've posted to suggest you need to do
anything else.

john
 
J

John Harrison

Having now read your earlier post I can see that you trying to setup your
own parallel I/O system. All I can say is don't bother, the existing system
is very good, extendable in pretty much any direction. You would find it
much more productive to make you existing code work with the existing
streams.

As mentioned before, 'the interface that can allow classes that implement it
to act sort of like streams' already exists and its called streambuf.

john
 
C

Christopher Benson-Manica

John Harrison said:
Yes derive from streambuf, that is the usual way to create your own stream
types. You leave the already defined interface for istream/ostream alone
(instead trying to duplicate it as you are doing) and just use a streambuf
derived class to direct where the input/output is really going. Get a good
book on the standard library for details on how do to this.

I see. In that case, two questions:

1) What such book would you suggest? (one that's likely to be in a
bookstore)

2) Before I do that, what situations would prevent me from effectively
subclassing streambuf? What capabilities do the subclasses of this
template have to have for this to work?
I haven't seen anything in the code you've posted to suggest you need to do
anything else.

Unless 2) holds, that sounds like a good idea. Thanks!
 
C

Christopher Benson-Manica

John Harrison said:
Having now read your earlier post I can see that you trying to setup your
own parallel I/O system. All I can say is don't bother, the existing system
is very good, extendable in pretty much any direction. You would find it
much more productive to make you existing code work with the existing
streams.

What I'm *really* trying to do is make some existing classes that we
have behave like real C++ classes instead of C-with-classes classes ;)
The "gotcha" is that I can't change the implementation of any of these
classes, and so what I envision doing is subclassing them from
something - I guess maybe the "something" is streambuf?
As mentioned before, 'the interface that can allow classes that implement it
to act sort of like streams' already exists and its called streambuf.

But the classes that implement it have to have certain minimal
capabilities, right? This is where I wonder whether I can really make
this work or not...
 
J

John Harrison

Christopher Benson-Manica said:
I see. In that case, two questions:

1) What such book would you suggest? (one that's likely to be in a
bookstore)

The C++ Standard Library by Josuttis.
2) Before I do that, what situations would prevent me from effectively
subclassing streambuf? What capabilities do the subclasses of this
template have to have for this to work?

For output they have to override a method called overflow, it's called when
the buffer is full and you are expected to do the real output (whatever that
means for your stream). By this time the data has been formatted in the
usual iostream way and all you are expected to do is simple character I/O.

For input there's a method called underflow, which is when the buffer is
empty and you have to go back to the data source to get some more data,
again this is just character I/O. There's also a method called uflow, I
always have to look that one up.

There's a couple of methods for positioning, if you want to support that.

That's about it.

Basically all istream and ostream objects are is a pointer to a streambuf
derived object. Both istream and ostream have constructors which will accept
a pointer to any streambuf derived object. If you like you can also write
your own small ostream or istream derived classes which are basically just
ways of constructing an object with the right kind of buffer (this is how
fstream and stringstream work for instance), the real work goes on in the
streambuf derived class.
Unless 2) holds, that sounds like a good idea. Thanks!

john
 
C

Christopher Benson-Manica

John Harrison said:
The C++ Standard Library by Josuttis.

Great, will see if the local bookstore has it tonight. Thanks.
For output they have to override a method called overflow, it's called when
the buffer is full and you are expected to do the real output (whatever that
means for your stream). By this time the data has been formatted in the
usual iostream way and all you are expected to do is simple character I/O.

Character-by-character I/O? I don't think that's going to work in my
sistuation - these classes want whole lines of input, which streambuf
doesn't appear to be able to provide...

This would be great if it worked, though.
 
M

Mike Wahler

John Harrison said:
The C++ Standard Library by Josuttis.

That is indeed a great book, and has some good info on iostreams.
I'd also recommend to anyone delving into iostreams to get this
one also:
http://www.langer.camelot.de/iostreams.html
IMO it's *very* good at explaining the 'nitty-gritty' of this stuff.

I've found both of these books at any bookstore with a reasonably
sized computer section. I'm sure online booksellers have them too.

-Mike
 
J

John Harrison

Christopher Benson-Manica said:
Great, will see if the local bookstore has it tonight. Thanks.
I/O.

Character-by-character I/O? I don't think that's going to work in my
sistuation - these classes want whole lines of input, which streambuf
doesn't appear to be able to provide...

This would be great if it worked, though.

Lines of input are composed of characters no? The point is was making was
that you don't have to worry about formatting, you simply read or write as
many characters as you need. E.g. when mystreambuf::underflow is called you
read a whole line of input.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,162
Messages
2,570,893
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top