Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   serializing and deserializing enum type (http://www.velocityreviews.com/forums/t529076-serializing-and-deserializing-enum-type.html)

yu_kuo@sina.com 08-14-2007 09:02 AM

serializing and deserializing enum type
 
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


James Kanze 08-14-2007 01:59 PM

Re: serializing and deserializing enum type
 
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.kanze@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


Jim Langston 08-15-2007 06:40 AM

Re: serializing and deserializing enum type
 
<yu_kuo@sina.com> wrote in message
news:1187082154.724455.50100@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.



Jim Langston 08-15-2007 07:19 AM

Re: serializing and deserializing enum type
 
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:rdxwi.53$_Y6.24@newsfe02.lga...
> <yu_kuo@sina.com> wrote in message
> news:1187082154.724455.50100@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.



Ian Collins 08-15-2007 07:38 AM

Re: serializing and deserializing enum type
 
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.

yu_kuo@sina.com 08-15-2007 08:55 AM

Re: serializing and deserializing enum type
 
On Aug 15, 3:19 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Jim Langston" <tazmas...@rocketmail.com> wrote in message
>
> news:rdxwi.53$_Y6.24@newsfe02.lga...
>
>
>
>
>
> > <yu_...@sina.com> wrote in message
> >news:1187082154.724455.50100@w3g2000hsg.googlegro ups.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 :)


yu_kuo@sina.com 08-15-2007 09:13 AM

Re: serializing and deserializing enum type
 
On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.com> 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...


yu_kuo@sina.com 08-15-2007 09:15 AM

Re: serializing and deserializing enum type
 
On Aug 14, 9:59 pm, James Kanze <james.ka...@gmail.com> wrote:
> 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


Ian Collins 08-15-2007 09:54 AM

Re: serializing and deserializing enum type
 
yu_kuo@sina.com wrote:
> On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.com> 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.

yu_kuo@sina.com 08-18-2007 02:35 AM

Re: serializing and deserializing enum type
 
On Aug 15, 5:54 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> yu_...@sina.com wrote:
> > On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.com> 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



All times are GMT. The time now is 11:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.