serializing and deserializing enum type

Y

yu_kuo

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
 
J

James Kanze

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.)
 
J

Jim Langston

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.
 
J

Jim Langston

Jim Langston said:
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.
 
I

Ian Collins

Jim said:
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 );
}
 
Y

yu_kuo

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 :)
 
Y

yu_kuo

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 );
}


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...
 
Y

yu_kuo

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:[email protected]
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
 
I

Ian Collins

*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.
 
Y

yu_kuo

*Please don't quote signatures or that google quoted test nonsense*








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
 
I

Ian Collins

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top