473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

More help with << and >>

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.
Jul 22 '05 #1
8 1450

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
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
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
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...


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
Jul 22 '05 #3
John Harrison <jo*************@hotmail.com> spoke thus:
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!

--
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.
Jul 22 '05 #4
John Harrison <jo*************@hotmail.com> spoke thus:
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...

--
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.
Jul 22 '05 #5

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
John Harrison <jo*************@hotmail.com> spoke thus:
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)


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


john
Jul 22 '05 #6
John Harrison <jo*************@hotmail.com> spoke thus:
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.

--
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.
Jul 22 '05 #7

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
John Harrison <jo*************@hotmail.com> spoke thus:
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)


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

Jul 22 '05 #8

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
John Harrison <jo*************@hotmail.com> spoke thus:
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.


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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
4
by: PASQUALE | last post by:
Hi I have a question: do the both statements below give the same result? If yes then does somebody know something about preformance differencies using these joins? SELECT A.* FROM Table1 A...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
6
by: tentstitcher | last post by:
Hi all: I have a source xml document with an element of type string. This element contains something like the following: <stringData> &lt;Header&gt; &lt;Body&gt; </stringData> I would like to apply an...
1
by: James | last post by:
I would like to call a method of my code from within XSL but it does not seem to like the presence of the '%' character inside the xsl. Basicly the XSL loops through an XML writing url's into...
1
by: rdemyan via AccessMonster.com | last post by:
I have the following SQL statement in code: strSQL = "SELECT * FROM TABLEA WHERE F2 = 'SITE' This returns the record that I expect. However: the following does not strSQL = "SELECT *...
8
by: vunet.us | last post by:
I create an ASP file using fso.createtextfile. But how do I avoid double quotes and <% %signs when writing to file? FileAsp.WriteLine("<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252">")...
1
by: hedgracer | last post by:
have a query that has a <>0 in a summed number field. The query runs extremely slow (30+ seconds) when this parameter is in this field. If I eliminate this parameter the query runs in milliseconds....
4
by: saneman | last post by:
I have a folder 'app' containing a file 'main.cpp' and a subfolder 'types' (containing various header files). In main.cpp some header files from the subdir 'types' are included like: 1)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.