Connecting Tech Pros Worldwide Help | Site Map

More help with << and >>

Christopher Benson-Manica
Guest
 
Posts: n/a
#1: Jul 22 '05
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: More help with << and >>



"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c1j353$b3a$1@chessie.cirr.com...[color=blue]
> 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?
>[/color]

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


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: More help with << and >>



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1j3p1$1ikshn$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:c1j353$b3a$1@chessie.cirr.com...[color=green]
> > 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...[/color][/color]

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


Christopher Benson-Manica
Guest
 
Posts: n/a
#4: Jul 22 '05

re: More help with << and >>


John Harrison <john_andronicus@hotmail.com> spoke thus:
[color=blue]
> 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.[/color]

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?
[color=blue]
> I haven't seen anything in the code you've posted to suggest you need to do
> anything else.[/color]

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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Christopher Benson-Manica
Guest
 
Posts: n/a
#5: Jul 22 '05

re: More help with << and >>


John Harrison <john_andronicus@hotmail.com> spoke thus:
[color=blue]
> 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.[/color]

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?
[color=blue]
> As mentioned before, 'the interface that can allow classes that implement it
> to act sort of like streams' already exists and its called streambuf.[/color]

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...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: More help with << and >>



"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c1j47g$bd6$1@chessie.cirr.com...[color=blue]
> John Harrison <john_andronicus@hotmail.com> spoke thus:
>[color=green]
> > Yes derive from streambuf, that is the usual way to create your own[/color][/color]
stream[color=blue][color=green]
> > types. You leave the already defined interface for istream/ostream alone
> > (instead trying to duplicate it as you are doing) and just use a[/color][/color]
streambuf[color=blue][color=green]
> > derived class to direct where the input/output is really going. Get a[/color][/color]
good[color=blue][color=green]
> > book on the standard library for details on how do to this.[/color]
>
> I see. In that case, two questions:
>
> 1) What such book would you suggest? (one that's likely to be in a
> bookstore)[/color]

The C++ Standard Library by Josuttis.
[color=blue]
>
> 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?[/color]

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.
[color=blue]
>[color=green]
> > I haven't seen anything in the code you've posted to suggest you need to[/color][/color]
do[color=blue][color=green]
> > anything else.[/color]
>
> Unless 2) holds, that sounds like a good idea. Thanks!
>[/color]

john


Christopher Benson-Manica
Guest
 
Posts: n/a
#7: Jul 22 '05

re: More help with << and >>


John Harrison <john_andronicus@hotmail.com> spoke thus:
[color=blue]
> The C++ Standard Library by Josuttis.[/color]

Great, will see if the local bookstore has it tonight. Thanks.
[color=blue]
> 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.[/color]

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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Mike Wahler
Guest
 
Posts: n/a
#8: Jul 22 '05

re: More help with << and >>



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1j55o$1bfr6a$1@ID-196037.news.uni-berlin.de...[color=blue]
>
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:c1j47g$bd6$1@chessie.cirr.com...[color=green]
> > John Harrison <john_andronicus@hotmail.com> spoke thus:
> >[color=darkred]
> > > Yes derive from streambuf, that is the usual way to create your own[/color][/color]
> stream[color=green][color=darkred]
> > > types. You leave the already defined interface for istream/ostream[/color][/color][/color]
alone[color=blue][color=green][color=darkred]
> > > (instead trying to duplicate it as you are doing) and just use a[/color][/color]
> streambuf[color=green][color=darkred]
> > > derived class to direct where the input/output is really going. Get a[/color][/color]
> good[color=green][color=darkred]
> > > book on the standard library for details on how do to this.[/color]
> >
> > I see. In that case, two questions:
> >
> > 1) What such book would you suggest? (one that's likely to be in a
> > bookstore)[/color]
>
> The C++ Standard Library by Josuttis.[/color]

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





John Harrison
Guest
 
Posts: n/a
#9: Jul 22 '05

re: More help with << and >>



"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c1j5vu$btt$1@chessie.cirr.com...[color=blue]
> John Harrison <john_andronicus@hotmail.com> spoke thus:
>[color=green]
> > The C++ Standard Library by Josuttis.[/color]
>
> Great, will see if the local bookstore has it tonight. Thanks.
>[color=green]
> > For output they have to override a method called overflow, it's called[/color][/color]
when[color=blue][color=green]
> > the buffer is full and you are expected to do the real output (whatever[/color][/color]
that[color=blue][color=green]
> > 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[/color][/color]
I/O.[color=blue]
>
> 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.[/color]

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


Closed Thread