Connecting Tech Pros Worldwide Help | Site Map

Subclassing to get << and >>

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

re: Subclassing to get << and >>


At 2004-02-20 14:04 +0000, Christopher Benson-Manica wrote:[color=blue]
> 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!
>[/color]

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';
}
Rolf Magnus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Subclassing to get << and >>


fifo wrote:
[color=blue]
> At 2004-02-20 14:04 +0000, Christopher Benson-Manica wrote:[color=green]
>> 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!
>>[/color]
>
> 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';[/color]

Change that into:

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

adn try to compile it again.
[color=blue]
> }[/color]

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

Chris Theis
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Subclassing to get << and >>



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

[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


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

re: Subclassing to get << and >>


Chris Theis <Christian.Theis@nospam.cern.ch> spoke thus:
[color=blue]
> 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.[/color]

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.
Jorge Rivera
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Subclassing to get << and >>


>[color=blue]
> 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...
>[/color]

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.
Jonathan Turkanis
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Subclassing to get << and >>



"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in
message news:c1544q$huq$1@chessie.cirr.com...[color=blue]
> Sorry for the lame title, but it's hard to condense what I'm about[/color]
to[color=blue]
> 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()[/color]
and[color=blue]
> 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[/color]
could[color=blue]
> probably come up with some hackneyed scheme to do this on my own,[/color]
but[color=blue]
> 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[/color]
outbuf[color=blue]
> (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[/color]
any[color=blue]
> guidance!
>[/color]

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


Jorge Rivera
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Subclassing to get << and >>


Jonathan Turkanis wrote:
[color=blue]
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in
> message news:c1544q$huq$1@chessie.cirr.com...
>[color=green]
>>Sorry for the lame title, but it's hard to condense what I'm about[/color]
>
> to
>[color=green]
>>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()[/color]
>
> and
>[color=green]
>>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[/color]
>
> could
>[color=green]
>>probably come up with some hackneyed scheme to do this on my own,[/color]
>
> but
>[color=green]
>>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[/color]
>
> outbuf
>[color=green]
>>(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[/color]
>
> any
>[color=green]
>>guidance!
>>[/color]
>
>
> 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
>
>[/color]
Can I get it?
Jonathan Turkanis
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Subclassing to get << and >>



"Jorge Rivera" <jorgeri@rochester.rr.com> wrote in message
news:v%xZb.33269$um1.29883@twister.nyroc.rr.com...[color=blue]
> Jonathan Turkanis wrote:[/color]
[color=blue][color=green]
> > I have a library (submitted to boost) which would allow you easily[/color][/color]
to[color=blue][color=green]
> > 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);
> > };[/color][/color]
<snip>[color=blue][color=green]
> >[/color]
> Can I get it?[/color]

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


Dylan Nicholson
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Subclassing to get << and >>


"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message news:<c168vg$1ene5u$1@ID-216073.news.uni-berlin.de>...
[color=blue]
> 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();
>[/color]
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
Christopher Benson-Manica
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Subclassing to get << and >>


Christopher Benson-Manica <ataru@nospam.cyberspace.org> spoke thus:
[color=blue]
> class
> MyStreamBasedTCPThread : public MyTCPThread
> {
> protected:
> std::ostringstream outbuf;
> };[/color]

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


Similar C / C++ bytes