By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

serializing and deserializing enum type

P: n/a
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>::operator<<(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 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Aug 14, 11:02 am, yu_...@sina.com 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>::operator<<(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:ja*********@gmail.com
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

Aug 14 '07 #2

P: n/a
<yu****@sina.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
>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>::operator<<(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.
Aug 15 '07 #3

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:rd*************@newsfe02.lga...
<yu****@sina.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
>>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>::operator<<(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.
Aug 15 '07 #4

P: n/a
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.
Aug 15 '07 #5

P: n/a
On Aug 15, 3:19 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Jim Langston" <tazmas...@rocketmail.comwrote in message

news:rd*************@newsfe02.lga...


<yu_...@sina.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
>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>::operator<<(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 '07 #6

P: n/a
On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.comwrote:
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 '07 #7

P: n/a
On Aug 14, 9:59 pm, James Kanze <james.ka...@gmail.comwrote:
On Aug 14, 11:02 am, yu_...@sina.com 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>::operator<<(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:james.ka...@gmail.com
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 '07 #8

P: n/a
yu****@sina.com wrote:
On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>
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.
Aug 15 '07 #9

P: n/a
On Aug 15, 5:54 pm, Ian Collins <ian-n...@hotmail.comwrote:
yu_...@sina.com wrote:
On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.comwrote:
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 '07 #10

P: n/a
yu****@sina.com 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.
Aug 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.