473,664 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(outb uf.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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
8 1468

"Christophe r Benson-Manica" <at***@nospam.c yberspace.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(outb uf.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...

"Christophe r Benson-Manica" <at***@nospam.c yberspace.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)cybers pace.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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #5

"Christophe r Benson-Manica" <at***@nospam.c yberspace.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)cybers pace.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...

"Christophe r Benson-Manica" <at***@nospam.c yberspace.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

"Christophe r Benson-Manica" <at***@nospam.c yberspace.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::un derflow 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
3209
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 writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
4
21425
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 INNER JOIN Table2 B on A.Field1 <> B.Field1
29
3668
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 id="Form1" method="post" runat="server"> <%
6
24307
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 XSLT and replace all occurances of &lt; with < and &gt; with >.
1
1355
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 links on a menu... I have a method that when passed a URL encrypts the query string portion and spits it back out as URL+encoded QueryString. I would like to call this method using somthing akin to <%EncodeQueryString(<xsl:value-of...
1
1597
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 * FROM TABLEA WHERE F2 <'SITE'
8
4772
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">") FileAsp.WriteLine("<!-- #Include file="file.asp" -->") FileAsp.WriteLine(" <% =Blah %>") Thanks
1
1481
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. This is a query on a linked odbc sql server 2005 table. Any thoughts on the theory of why this happens? Thanks. dave
4
2130
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) #include<types/vector.h> #include<types/matrix.h> But I have to change them to
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.