serializing and deserializing enum type

Discussion in 'C++' started by yu_kuo@sina.com, Aug 14, 2007.

  1. Guest

    I got series warning when using write enum type to a ostream using
    operator<<, like

    in call to `std::basic_ostream<_CharT, _Traits>&
    std::basic_ostream<_CharT, _Traits>::eek:perator<<(int) [with _CharT =
    char, _Traits = std::char_traits<char>]'
    A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int'

    Compiler gcc 3.4.3

    When deserialize from an istream, I have to first using an int, and
    then cast to the enum type. And need check the range on the spot.

    I do know I can overload operator<< and >> for enum type, but do that
    for every enum type isn't too boring? I'm wondering a way to do it in
    one strike, is there?

    Regards,
    Kevin
     
    , Aug 14, 2007
    #1
    1. Advertising

  2. James Kanze Guest

    On Aug 14, 11:02 am, wrote:
    > I got series warning when using write enum type to a ostream using
    > operator<<, like


    > in call to `std::basic_ostream<_CharT, _Traits>&
    > std::basic_ostream<_CharT, _Traits>::eek:perator<<(int) [with _CharT =
    > char, _Traits = std::char_traits<char>]'
    > A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int'


    > Compiler gcc 3.4.3


    > When deserialize from an istream, I have to first using an int, and
    > then cast to the enum type. And need check the range on the spot.


    > I do know I can overload operator<< and >> for enum type, but do that
    > for every enum type isn't too boring? I'm wondering a way to do it in
    > one strike, is there?


    Yes. Write a program which reads the enum and generates the
    desired mapping. (There's one at my site:
    kanze.james.neuf.fr/code-en.html, the executable enumgen. The
    version there is somewhat preliminary, however, and I've since
    modified it so that the generated code has no dependencies on
    other things in my library. Still, the code is simple enough
    that you should be able to adopt it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 14, 2007
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    >I got series warning when using write enum type to a ostream using
    > operator<<, like
    >
    > in call to `std::basic_ostream<_CharT, _Traits>&
    > std::basic_ostream<_CharT, _Traits>::eek:perator<<(int) [with _CharT =
    > char, _Traits = std::char_traits<char>]'
    > A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int'
    >
    > Compiler gcc 3.4.3
    >
    > When deserialize from an istream, I have to first using an int, and
    > then cast to the enum type. And need check the range on the spot.
    >
    > I do know I can overload operator<< and >> for enum type, but do that
    > for every enum type isn't too boring? I'm wondering a way to do it in
    > one strike, is there?


    You can overload operator<< and >> for enum type? I didn't think you could
    override << >> for C++'s built in types. I'm going to have to play with
    that. Right now I'm doing:

    int Race, Sex;
    is >> CChar.GM >> /* ... */ Race >> Sex /* ... */ ;
    CChar.Race = static_cast< ERaces >( Race );
    CChar.Sex = static_cast< ESexes >( Sex );

    Overloading operator << and >> for my ERaces and ESexes enums would make it
    a lot cleaner.
     
    Jim Langston, Aug 15, 2007
    #3
  4. Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:rdxwi.53$...
    > <> wrote in message
    > news:...
    >>I got series warning when using write enum type to a ostream using
    >> operator<<, like
    >>
    >> in call to `std::basic_ostream<_CharT, _Traits>&
    >> std::basic_ostream<_CharT, _Traits>::eek:perator<<(int) [with _CharT =
    >> char, _Traits = std::char_traits<char>]'
    >> A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int'
    >>
    >> Compiler gcc 3.4.3
    >>
    >> When deserialize from an istream, I have to first using an int, and
    >> then cast to the enum type. And need check the range on the spot.
    >>
    >> I do know I can overload operator<< and >> for enum type, but do that
    >> for every enum type isn't too boring? I'm wondering a way to do it in
    >> one strike, is there?

    >
    > You can overload operator<< and >> for enum type? I didn't think you
    > could override << >> for C++'s built in types. I'm going to have to play
    > with that. Right now I'm doing:
    >
    > int Race, Sex;
    > is >> CChar.GM >> /* ... */ Race >> Sex /* ... */ ;
    > CChar.Race = static_cast< ERaces >( Race );
    > CChar.Sex = static_cast< ESexes >( Sex );
    >
    > Overloading operator << and >> for my ERaces and ESexes enums would make
    > it a lot cleaner.


    This seems to work:

    enum ERaces
    {
    Raceless,
    Human,
    Angel,
    Fiend,
    Demon
    };

    std::istream& operator>>( std::istream& is, ERaces& Race )
    {
    int IntRace;
    is >> IntRace;
    Race = static_cast< ERaces >( IntRace );
    return is;
    }

    Good enough for me.
     
    Jim Langston, Aug 15, 2007
    #4
  5. Ian Collins Guest

    Jim Langston wrote:
    >>
    >> Overloading operator << and >> for my ERaces and ESexes enums would make
    >> it a lot cleaner.

    >
    > This seems to work:
    >
    > enum ERaces
    > {
    > Raceless,
    > Human,
    > Angel,
    > Fiend,
    > Demon
    > };
    >
    > std::istream& operator>>( std::istream& is, ERaces& Race )
    > {
    > int IntRace;
    > is >> IntRace;
    > Race = static_cast< ERaces >( IntRace );
    > return is;
    > }
    >
    > Good enough for me.
    >

    That's the normal approach, what I think the OP was looking for was a
    more generic solution for any enum.

    The only solutions I am aware of are code generation or a partial
    solution with templates:

    template <typename EnumType>
    inline std::istream& read( std::istream& in, EnumType& s )
    {
    unsigned n;
    in >> n;
    s = static_cast<EnumType>(n);
    return in;
    }

    inline std::istream& operator>>( std::istream& in, ERaces& s )
    {
    return read( in, s );
    }

    --
    Ian Collins.
     
    Ian Collins, Aug 15, 2007
    #5
  6. Guest

    On Aug 15, 3:19 pm, "Jim Langston" <> wrote:
    > "Jim Langston" <> wrote in message
    >
    > news:rdxwi.53$...
    >
    >
    >
    >
    >
    > > <> wrote in message
    > >news:...
    > >>I got series warning when using write enum type to a ostream using
    > >> operator<<, like

    >
    > >> in call to `std::basic_ostream<_CharT, _Traits>&
    > >> std::basic_ostream<_CharT, _Traits>::eek:perator<<(int) [with _CharT =
    > >> char, _Traits = std::char_traits<char>]'
    > >> A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int'

    >
    > >> Compiler gcc 3.4.3

    >
    > >> When deserialize from an istream, I have to first using an int, and
    > >> then cast to the enum type. And need check the range on the spot.

    >
    > >> I do know I can overload operator<< and >> for enum type, but do that
    > >> for every enum type isn't too boring? I'm wondering a way to do it in
    > >> one strike, is there?

    >
    > > You can overload operator<< and >> for enum type? I didn't think you
    > > could override << >> for C++'s built in types. I'm going to have to play
    > > with that. Right now I'm doing:

    >
    > > int Race, Sex;
    > > is >> CChar.GM >> /* ... */ Race >> Sex /* ... */ ;
    > > CChar.Race = static_cast< ERaces >( Race );
    > > CChar.Sex = static_cast< ESexes >( Sex );

    >
    > > Overloading operator << and >> for my ERaces and ESexes enums would make
    > > it a lot cleaner.

    >
    > This seems to work:
    >
    > enum ERaces
    > {
    > Raceless,
    > Human,
    > Angel,
    > Fiend,
    > Demon
    >
    > };
    >
    > std::istream& operator>>( std::istream& is, ERaces& Race )
    > {
    > int IntRace;
    > is >> IntRace;
    > Race = static_cast< ERaces >( IntRace );
    > return is;
    >
    > }
    >
    > Good enough for me.- Hide quoted text -
    >
    > - Show quoted text -


    Good to see :)
     
    , Aug 15, 2007
    #6
  7. Guest

    On Aug 15, 3:38 pm, Ian Collins <> wrote:
    > Jim Langston wrote:
    >
    > >> Overloading operator << and >> for my ERaces and ESexes enums would make
    > >> it a lot cleaner.

    >
    > > This seems to work:

    >
    > > enum ERaces
    > > {
    > > Raceless,
    > > Human,
    > > Angel,
    > > Fiend,
    > > Demon
    > > };

    >
    > > std::istream& operator>>( std::istream& is, ERaces& Race )
    > > {
    > > int IntRace;
    > > is >> IntRace;
    > > Race = static_cast< ERaces >( IntRace );
    > > return is;
    > > }

    >
    > > Good enough for me.

    >
    > That's the normal approach, what I think the OP was looking for was a
    > more generic solution for any enum.
    >
    > The only solutions I am aware of are code generation or a partial
    > solution with templates:
    >
    > template <typename EnumType>
    > inline std::istream& read( std::istream& in, EnumType& s )
    > {
    > unsigned n;
    > in >> n;
    > s = static_cast<EnumType>(n);
    > return in;
    > }
    >
    > inline std::istream& operator>>( std::istream& in, ERaces& s )
    > {
    > return read( in, s );
    > }
    >
    > --
    > Ian Collins.- Hide quoted text -
    >
    > - Show quoted text -



    I'm trying to combine those, and got this:

    template <typename EnumType>
    inline std::istream& operator>>( std::istream& in, EnumType& s )
    {
    unsigned n;
    in >> n;
    s = static_cast<EnumType>(n);
    return in;
    }


    But then aware that I'm redefining the global (std) operater>>,
    whew...
    So I still need a per enum type effort to do, ritht?
    I'm thinking if I should use a more inteligent templete type, which
    overloaded operater << and >> and doing underflow and overflow check?
    But how can I give each possible value a name?

    Pondering...
     
    , Aug 15, 2007
    #7
  8. Guest

    On Aug 14, 9:59 pm, James Kanze <> wrote:
    > On Aug 14, 11:02 am, wrote:
    >
    > > I got series warning when using write enum type to a ostream using
    > > operator<<, like
    > > in call to `std::basic_ostream<_CharT, _Traits>&
    > > std::basic_ostream<_CharT, _Traits>::eek:perator<<(int) [with _CharT =
    > > char, _Traits = std::char_traits<char>]'
    > > A.cpp:60: warning: passing `NAME_ENUM' chooses `int' over `long int'
    > > Compiler gcc 3.4.3
    > > When deserialize from an istream, I have to first using an int, and
    > > then cast to the enum type. And need check the range on the spot.
    > > I do know I can overload operator<< and >> for enum type, but do that
    > > for every enum type isn't too boring? I'm wondering a way to do it in
    > > one strike, is there?

    >
    > Yes. Write a program which reads the enum and generates the
    > desired mapping. (There's one at my site:
    > kanze.james.neuf.fr/code-en.html, the executable enumgen. The
    > version there is somewhat preliminary, however, and I've since
    > modified it so that the generated code has no dependencies on
    > other things in my library. Still, the code is simple enough
    > that you should be able to adopt it.)
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Thanks for your nice offer, I'll definitely try it.

    Regards,
    Kevin
     
    , Aug 15, 2007
    #8
  9. Ian Collins Guest

    wrote:
    > On Aug 15, 3:38 pm, Ian Collins <> wrote:
    >>
    >> The only solutions I am aware of are code generation or a partial
    >> solution with templates:
    >>
    >> template <typename EnumType>
    >> inline std::istream& read( std::istream& in, EnumType& s )
    >> {
    >> unsigned n;
    >> in >> n;
    >> s = static_cast<EnumType>(n);
    >> return in;
    >> }
    >>
    >> inline std::istream& operator>>( std::istream& in, ERaces& s )
    >> {
    >> return read( in, s );
    >> }
    >>

    *Please don't quote signatures or that google quoted test nonsense*
    >
    > I'm trying to combine those, and got this:
    >
    > template <typename EnumType>
    > inline std::istream& operator>>( std::istream& in, EnumType& s )
    > {
    > unsigned n;
    > in >> n;
    > s = static_cast<EnumType>(n);
    > return in;
    > }
    >
    >
    > But then aware that I'm redefining the global (std) operater>>,
    > whew...
    > So I still need a per enum type effort to do, ritht?
    > I'm thinking if I should use a more inteligent templete type, which
    > overloaded operater << and >> and doing underflow and overflow check?
    > But how can I give each possible value a name?
    >

    One way is to expand the read template to take a min a max value
    template parameters.

    --
    Ian Collins.
     
    Ian Collins, Aug 15, 2007
    #9
  10. Guest

    On Aug 15, 5:54 pm, Ian Collins <> wrote:
    > wrote:
    > > On Aug 15, 3:38 pm, Ian Collins <> wrote:

    >
    > >> The only solutions I am aware of are code generation or a partial
    > >> solution with templates:

    >
    > >> template <typename EnumType>
    > >> inline std::istream& read( std::istream& in, EnumType& s )
    > >> {
    > >> unsigned n;
    > >> in >> n;
    > >> s = static_cast<EnumType>(n);
    > >> return in;
    > >> }

    >
    > >> inline std::istream& operator>>( std::istream& in, ERaces& s )
    > >> {
    > >> return read( in, s );
    > >> }

    >
    > *Please don't quote signatures or that google quoted test nonsense*
    >
    >
    >
    >
    >
    > > I'm trying to combine those, and got this:

    >
    > > template <typename EnumType>
    > > inline std::istream& operator>>( std::istream& in, EnumType& s )
    > > {
    > > unsigned n;
    > > in >> n;
    > > s = static_cast<EnumType>(n);
    > > return in;
    > > }

    >
    > > But then aware that I'm redefining the global (std) operater>>,
    > > whew...
    > > So I still need a per enum type effort to do, ritht?
    > > I'm thinking if I should use a more inteligent templete type, which
    > > overloaded operater << and >> and doing underflow and overflow check?
    > > But how can I give each possible value a name?

    >
    > One way is to expand the read template to take a min a max value
    > template parameters.
    >
    > --
    > Ian Collins.- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    But still I don't have a natural way to deduce the min and max value
    of a general enum type, do I?

    Kevin
     
    , Aug 18, 2007
    #10
  11. Ian Collins Guest

    wrote:
    >> One way is to expand the read template to take a min a max value
    >> template parameters.
    >>
    >> --
    >> Ian Collins.- Hide quoted text -
    >>
    >> - Show quoted text -- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > But still I don't have a natural way to deduce the min and max value
    > of a general enum type, do I?
    >

    Nope. Not only that, but you still quoted my signature and that google crap.

    --
    Ian Collins.
     
    Ian Collins, Aug 18, 2007
    #11
    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. PJ
    Replies:
    1
    Views:
    1,803
    Ramzey
    Jun 25, 2003
  2. Eric Sosman
    Replies:
    3
    Views:
    781
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    502
  4. Lew
    Replies:
    0
    Views:
    540
  5. Roedy Green
    Replies:
    0
    Views:
    732
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page