streaming enum

Discussion in 'C++' started by toton, Feb 13, 2007.

  1. toton

    toton Guest

    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
     
    toton, Feb 13, 2007
    #1
    1. Advertising

  2. 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
     
    John Harrison, Feb 13, 2007
    #2
    1. Advertising

  3. toton

    toton Guest

    On Feb 13, 12:54 pm, John Harrison <>
    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
     
    toton, Feb 13, 2007
    #3
  4. toton

    dasjotre Guest

    On 13 Feb, 08:39, "toton" <> wrote:
    > On Feb 13, 12:54 pm, John Harrison <>
    > 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
     
    dasjotre, Feb 13, 2007
    #4
  5. 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
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Feb 13, 2007
    #5
  6. toton

    Marcus Kwok Guest

    toton <> wrote:
    > 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
     
    Marcus Kwok, Feb 15, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    549
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    472
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    428
    Dave Thompson
    Jun 10, 2004
  5. Replies:
    0
    Views:
    294
Loading...

Share This Page