473,320 Members | 1,722 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.

streaming enum

Hi,
I have a bunch of enums, which I want to stream with their names
rather than the enum value (just like bool has a facility in the
stream)
Any facility available in standard library / boost ?

something like,
enum TYPE{
PRINTED,CURSIVE,MIXED
};
and
ostream << PRINTED will print "PRINTED" if some flag is set to
ostream ...
abir

Feb 13 '07 #1
5 4328
toton wrote:
Hi,
I have a bunch of enums, which I want to stream with their names
rather than the enum value (just like bool has a facility in the
stream)
Any facility available in standard library / boost ?

something like,
enum TYPE{
PRINTED,CURSIVE,MIXED
};
and
ostream << PRINTED will print "PRINTED" if some flag is set to
ostream ...
abir
No standard facility. The problem is that like any variable name the
names of the enums no longer exist when your program has been compiled.

Of course you can just write

ostream& operator<<(ostream& out, TYPE t)
{
switch (t)
{
case PRINTED:
out << "PRINTED";
...
}
}

You could even make this dependent on a flag in ostream if you wanted.

john
Feb 13 '07 #2
On Feb 13, 12:54 pm, John Harrison <john_androni...@hotmail.com>
wrote:
toton wrote:
Hi,
I have a bunch of enums, which I want to stream with their names
rather than the enum value (just like bool has a facility in the
stream)
Any facility available in standard library / boost ?
something like,
enum TYPE{
PRINTED,CURSIVE,MIXED
};
and
ostream << PRINTED will print "PRINTED" if some flag is set to
ostream ...
abir

No standard facility. The problem is that like any variable name the
names of the enums no longer exist when your program has been compiled.

Of course you can just write

ostream& operator<<(ostream& out, TYPE t)
{
switch (t)
{
case PRINTED:
out << "PRINTED";
...
}

}

You could even make this dependent on a flag in ostream if you wanted.

john
This is one which I use at present. That one is needed for all enum
and need a default case.
The names do not exist at run time (except rtti, or any kind of
extended rtti in that matter) What I was thinking is that, the names
are always available at the compile time. Thus a template based
opetartor<< which generates the above function spitted in its parts
(specialization ) can do that trick, just like the marco based rtti
used in most of the common frameworks like Qt , VCF etc (which used
some meta class to store this info even at the run time). Only my need
may be generating a bunch of such functions at the compile time, than
making enum heavy with the meta classes.

Thanks
abir

Feb 13 '07 #3
On 13 Feb, 08:39, "toton" <abirba...@gmail.comwrote:
On Feb 13, 12:54 pm, John Harrison <john_androni...@hotmail.com>
wrote:
toton wrote:
Hi,
I have a bunch of enums, which I want to stream with their names
rather than the enum value (just like bool has a facility in the
stream)
Any facility available in standard library / boost ?
something like,
enum TYPE{
PRINTED,CURSIVE,MIXED
};
and
ostream << PRINTED will print "PRINTED" if some flag is set to
ostream ...
abir
No standard facility. The problem is that like any variable name the
names of the enums no longer exist when your program has been compiled.
Of course you can just write
ostream& operator<<(ostream& out, TYPE t)
{
switch (t)
{
case PRINTED:
out << "PRINTED";
...
}
}
You could even make this dependent on a flag in ostream if you wanted.
john

This is one which I use at present. That one is needed for all enum
and need a default case.
The names do not exist at run time (except rtti, or any kind of
extended rtti in that matter) What I was thinking is that, the names
are always available at the compile time. Thus a template based
opetartor<< which generates the above function spitted in its parts
(specialization ) can do that trick, just like the marco based rtti
used in most of the common frameworks like Qt , VCF etc (which used
some meta class to store this info even at the run time). Only my need
may be generating a bunch of such functions at the compile time, than
making enum heavy with the meta classes.
#define NAMEOF(X) #X

ostream << NAMEOF(PRINTED)

this is the only way I can think of
Feb 13 '07 #4
toton wrote:
The names do not exist at run time (except rtti, or any kind of
extended rtti in that matter) What I was thinking is that, the names
are always available at the compile time. Thus a template based
opetartor<< which generates the above function spitted in its parts
(specialization ) can do that trick, just like the marco based rtti
You can write a tool that reads a file with your enum values and maybe some
additional information and generates the code for the enum and the operator
<< . This can be much cleaner and less error prone than a combination of
templates and macros.

--
Salu2
Feb 13 '07 #5
toton <ab*******@gmail.comwrote:
I have a bunch of enums, which I want to stream with their names
rather than the enum value (just like bool has a facility in the
stream)
Any facility available in standard library / boost ?

something like,
enum TYPE{
PRINTED,CURSIVE,MIXED
};
and
ostream << PRINTED will print "PRINTED" if some flag is set to
ostream ...
There is no standard library or flag that you can pass to ostream for
this, but there is a discussion of this issue here with several
alternatives for implementation:

http://www.comeaucomputing.com/techtalk/#enumtostring

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 15 '07 #6

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

Similar topics

22
by: googlegroups | last post by:
I am playing with the XMLHTTPRequest method to perform client/server transactions. I have it set up right now so that when readyState is 4, it takes the XML and processes it. This works great until...
5
by: John | last post by:
Hi all, I have an (well, what I think to be, at least) interesting question: Is it possible to stream data down to the client and, after a certain amount of data has been streamed, allow the...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
2
by: mpaliath | last post by:
Hi guys I am currently involved in a project which requires me to recieve and play streaming video as well as send it. In Visual C++ is there any free library which helps me do this as...
8
by: Amjad | last post by:
Hi i am writing a application where i want to browse video file and copy data into stream and send that stream over network...I have develop P2P windows application where i successfully transfer...
2
by: SPG | last post by:
Hi, Two questions for you all.. Firstly, is there a way of streaming video using PHP? At the moment I just have a link to a video file and the whole thing downloads before playing which is a...
5
by: pmakoi | last post by:
dear all this might be a piece of cake for some of you out there but it is causing me a lot of stress given the fact that there is not enogh documentation out there regarding this topic I am...
1
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from...
0
by: tony_in_da_uk | last post by:
*** I posted this on comp.lang.c++.moderated a couple days ago, and got a near-flippant response from someone who clearly didn't take the time to consider the import. I replied, and it's still not...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.