473,406 Members | 2,273 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,406 software developers and data experts.

Subclassing to get << and >>

Sorry for the lame title, but it's hard to condense what I'm about to
say... Here's my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don't let the name scare you, this
isn't OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could
probably come up with some hackneyed scheme to do this on my own, but
I'd really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I'm really feeling lost
looking for a good way to do this. I'd be extremely grateful for any
guidance!

--
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
10 1652
At 2004-02-20 14:04 +0000, Christopher Benson-Manica wrote:
Sorry for the lame title, but it's hard to condense what I'm about to
say... Here's my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don't let the name scare you, this
isn't OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could
probably come up with some hackneyed scheme to do this on my own, but
I'd really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I'm really feeling lost
looking for a good way to do this. I'd be extremely grateful for any
guidance!


Well I'm not sure I understand what you're trying to do, but would
something like the following do what you want?

#include <string>
#include <iostream>
#include <sstream>

class foo
{
public:
void send(const std::string& str)
{ std::cout << "Sending: " << str << '\n'; }
};

template <class T>
foo& operator<<(foo& s, const T& t)
{
std::ostringstream ss;
ss << t;
s.send(ss.str());
return s;
}

int main()
{
foo f;

f << "Hello!\n";
f << "2 + 2 = " << 2 + 2 << '\n';
}
Jul 22 '05 #2
fifo wrote:
At 2004-02-20 14:04 +0000, Christopher Benson-Manica wrote:
Sorry for the lame title, but it's hard to condense what I'm about to
say... Here's my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don't let the name scare you, this
isn't OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I
could probably come up with some hackneyed scheme to do this on my
own, but I'd really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I'm really feeling lost
looking for a good way to do this. I'd be extremely grateful for any
guidance!

Well I'm not sure I understand what you're trying to do, but would
something like the following do what you want?

#include <string>
#include <iostream>
#include <sstream>

class foo
{
public:
void send(const std::string& str)
{ std::cout << "Sending: " << str << '\n'; }
};

template <class T>
foo& operator<<(foo& s, const T& t)
{
std::ostringstream ss;
ss << t;
s.send(ss.str());
return s;
}

int main()
{
foo f;

f << "Hello!\n";
f << "2 + 2 = " << 2 + 2 << '\n';


Change that into:

f << "2 + 2 = " << 2 + 2 << std::endl;

adn try to compile it again.
}


--
First they ignore you. Then they laugh about you.
Then they fight you. And then you win.
(Mahatma Gandhi)

Jul 22 '05 #3

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c1**********@chessie.cirr.com...
Sorry for the lame title, but it's hard to condense what I'm about to
say... Here's my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don't let the name scare you, this
isn't OT) that has a socket member object that has methods Send() and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could
probably come up with some hackneyed scheme to do this on my own, but
I'd really like some advice on how to do it well.


[SNIP]

IMHO you're walking on thin ice with this because if you supply << and >>
then the class user will want to use the stream inserters and extractors
exactly the way they are used to. As Rolf already showed with his remark
this might become a very tedious business starting with stream manipulators
(like std::endl). Probably the easiest way is to stay with Send & Getline()
methods.

Cheers
Chris
Jul 22 '05 #4
Chris Theis <Ch*************@nospam.cern.ch> spoke thus:
IMHO you're walking on thin ice with this because if you supply << and >>
then the class user will want to use the stream inserters and extractors
exactly the way they are used to. As Rolf already showed with his remark
this might become a very tedious business starting with stream manipulators
(like std::endl). Probably the easiest way is to stay with Send & Getline()
methods.


That's a good point, but I disagree that Send() is easier - either we
continue using <ot>temporary character buffers</ot> or static char
arrays (without being 100% sure they are big enough) and worrying
about populating them. Both of those alternatives seem like hassles
to me, so I think << and >> would at least make *my* life easier...

--
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
>
That's a good point, but I disagree that Send() is easier - either we
continue using <ot>temporary character buffers</ot> or static char
arrays (without being 100% sure they are big enough) and worrying
about populating them. Both of those alternatives seem like hassles
to me, so I think << and >> would at least make *my* life easier...


There are some references online on how to subclass
std::basic_streambuf, std::basic_ostream and std::basic_istream. By
doing this, you would end up with a nice tcp_ostream and tcp_istream
objects that would behave like the other standard stream types.
Jul 22 '05 #6

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in
message news:c1**********@chessie.cirr.com...
Sorry for the lame title, but it's hard to condense what I'm about to say... Here's my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don't let the name scare you, this
isn't OT) that has a socket member object that has methods Send() and GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I could probably come up with some hackneyed scheme to do this on my own, but I'd really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to outbuf (which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I'm really feeling lost
looking for a good way to do this. I'd be extremely grateful for any guidance!


I have a library (submitted to boost) which would allow you easily to
create a standard stream or stream buffer for writing to the TCP
connection if the underlying connection, if your interface is rich
enough to allow you to define a wrapper something like this

struct tcp_connection {
// Constructors, etc.
int read(char*, int);
void write(char*, int);
};

where read and write have the obvious semantics.

You'd end up with something like this

typedef resource_stream<tcp_connection> tcp_stream;
tcp_stream tcp;
tcp.open(tcp_connection("www.microsoft.com", 80));
tcp << "DELETE http://www.microsoft.com/ HTTP/1.1\r\n"
<< "Host: www.microsoft.com\r\n"
<< "Connection:close\r\n\r\n";
tcp.flush();

Jonathan
Jul 22 '05 #7
Jonathan Turkanis wrote:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in
message news:c1**********@chessie.cirr.com...
Sorry for the lame title, but it's hard to condense what I'm about


to
say... Here's my situation (my first real C++ situation, yay!).
Currently we have a MyTCPThread (don't let the name scare you, this
isn't OT) that has a socket member object that has methods Send()


and
GetLine() for sending and receiving data. I want to subclass
MyTCPThread so I can use << and >> as wrappers for Send() and
GetLine() (Send() is my primary concern at the moment, FWIW). I


could
probably come up with some hackneyed scheme to do this on my own,


but
I'd really like some advice on how to do it well.

My initial newbie thoughts suggest something like

class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};

but the big problem I see with this is how to make << apply to


outbuf
(which will get sent) and still keep all the cool overloaded <<
operators that ostringstreams already have. I'm really feeling lost
looking for a good way to do this. I'd be extremely grateful for


any
guidance!

I have a library (submitted to boost) which would allow you easily to
create a standard stream or stream buffer for writing to the TCP
connection if the underlying connection, if your interface is rich
enough to allow you to define a wrapper something like this

struct tcp_connection {
// Constructors, etc.
int read(char*, int);
void write(char*, int);
};

where read and write have the obvious semantics.

You'd end up with something like this

typedef resource_stream<tcp_connection> tcp_stream;
tcp_stream tcp;
tcp.open(tcp_connection("www.microsoft.com", 80));
tcp << "DELETE http://www.microsoft.com/ HTTP/1.1\r\n"
<< "Host: www.microsoft.com\r\n"
<< "Connection:close\r\n\r\n";
tcp.flush();

Jonathan

Can I get it?
Jul 22 '05 #8

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:v%*******************@twister.nyroc.rr.com...
Jonathan Turkanis wrote:

I have a library (submitted to boost) which would allow you easily to create a standard stream or stream buffer for writing to the TCP
connection if the underlying connection, if your interface is rich
enough to allow you to define a wrapper something like this

struct tcp_connection {
// Constructors, etc.
int read(char*, int);
void write(char*, int);
}; <snip>

Can I get it?


Sure! Thanks for your interest. The Boost files section is sometimes
difficult to get into, so I've put up a copy here:

http://www.xmission.com/~turkanis/iostreams/

Look for the documentation in the 'lib' subdirectory. The
functionality described above is described in the docs in the section
"5.6 Unfiltered Streams and Stream Buffers"

Please let me know what you think. If you run into any problems, I'll
try to fix them immediately.

Jonathan
Jul 22 '05 #9
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<c1*************@ID-216073.news.uni-berlin.de>...
typedef resource_stream<tcp_connection> tcp_stream;
tcp_stream tcp;
tcp.open(tcp_connection("www.microsoft.com", 80));
tcp << "DELETE http://www.microsoft.com/ HTTP/1.1\r\n"
<< "Host: www.microsoft.com\r\n"
<< "Connection:close\r\n\r\n";
tcp.flush();

I tried this, but now I can't access the MSDN library online...it
seems the whole microsoft web site has disappeared. What did I do
wrong?

;-)

Dylan
Jul 22 '05 #10
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
class
MyStreamBasedTCPThread : public MyTCPThread
{
protected:
std::ostringstream outbuf;
};


Thanks for the replies to this, but with my new understanding I don't
think I can do what I want here. If anyone cares, the real situation
(which I didn't attempt to describe in my original post) is something
like

class MySocket
{
public:
...
Send();
GetLine();
};

class MyTCPThread : MyBaseThreadClass // implementation not relevant
{
public:
...
MySocket socket;
};

What I wanted to do (I now know) was to wrap MySocket such that I
could send and receive using << and >>. Unfortunately, MyTCPThread
depends heavily on the functionality of MySocket, such that I can't
simply add a MyCoolSocket to the class and have it work correctly, and
I'm not at liberty to change the implemenation of MyTCPThread at all.
Oh well.

--
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 #11

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...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
0
by: Uwe Hoffmann | last post by:
Hi, i have used Pyrex to build some python Extensionclasses. Now i stumbled over the fact that if a subclass has a __hash__ method the __richcmp__ of the base is not called. # pseudocode ...
1
by: mike | last post by:
I've got some code like this: gametype_id = Request.Form("gametype_id") response.write "<br>gametype_id from form>" & gametype_id & "<" response.write "<br>gametype_id from database>" &...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
3
by: Josh Valino | last post by:
Hi, I have a client that has our product and in one of the aspx files, there is code like this: <%= SomePublicProperty %> where the public property returns a string. In the test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.