序列化和反序列化枚举类型 [英] serializing and deserializing enum type

查看:373
本文介绍了序列化和反序列化枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用写入枚举类型到ostream时,我使用

运算符<<,比如


调用`std :: basic_ostream< ; _CharT,_Traits>&

std :: basic_ostream< _CharT,_Traits> :: operator<<(int)[with _CharT =

char,_Traits = std :: char_traits< char>]''

A.cpp:60:警告:传递`NAME_ENUM''选择`int''超过`long int''


编译器gcc 3.4.3

从istream反序列化时,我必须首先使用int,然后将

转换为枚举类型。并且需要当场检查范围。


我知道我可以重载运算符<<和>对于枚举类型,但这样做
每个枚举类型的
是不是太无聊?我想知道这样做的方法

一击,是吗?


问候,

Kevin

解决方案

8月14日上午11点02分,yu _... @ sina.com写道:


当使用写入枚举类型到ostream使用

运算符<<,如


调用`std :: basic_ostream< _CharT,_Traits>&

std :: basic_ostream< _CharT,_Traits> :: operator<<(int)[with _CharT =

char,_Traits = std :: char_traits< char>]''

A.cpp:60:警告:传递`NAME_ENUM''选择'int''过` long int''


编译器gcc 3.4.3


从istream反序列化,我必须首先使用int,然后

然后转换为枚举类型。并且需要当场检查范围。


我知道我可以重载运算符<<和>对于枚举类型,但这样做
每个枚举类型的
是不是太无聊?我想知道一种方法来实现这一点

一击,是吗?



是的。编写一个程序,读取枚举并生成

所需的映射。 (在我的网站上有一个:

kanze.james.neuf.fr/code-en.html,可执行的enumgen。

版本有点初步然而,我已经对它进行了修改,以便生成的代码不依赖于我的库中的其他东西。但是,代码很简单<你应该可以采用它。$

-

James Kanze(GABI软件)电子邮件:ja ** *******@gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''école,法国,+ 33(0)1 30 23 00 34


< yu **** @ sina .comwrote in message

news:11 ********************* @ w3g2000hsg.googlegrou ps.com ...


>使用

运算符<<,类似

$时,我使用写枚举类型给ostream发出了系列警告b $ BI n调用`std :: basic_ostream< _CharT,_Traits>&

std :: basic_ostream< _CharT,_Traits> :: operator<<(int)[with _CharT =

char,_Traits = std :: char_traits< char>]''

A.cpp:60:警告:传递`NAME_ENUM''选择`int''超过'long int''


编译器gcc 3.4.3


从istream反序列化时,我必须首先使用int,并且

然后转换为枚举类型。并且需要当场检查范围。


我知道我可以重载运算符<<和>对于枚举类型,但这样做
每个枚举类型的
是不是太无聊?我想知道一种方法来实现这一点

一击,是吗?



您可以重载运算符<<和>用于枚举类型?我不认为你可以

覆盖<< >用于C ++的内置类型。我将不得不玩

。现在我正在做:


int种族,性别;

是> CChar.GM> / * ... * / Race> ;性别/ * ... * /;

CChar.Race = static_cast< ERaces>(种族);

CChar.Sex = static_cast< ESexes>(性别);


重载运算符<<和>对于我的ERaces和ESexes枚举会使它更加清晰。


" Jim Langston" < ta ******* @ rocketmail.comwrote in message

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


< yu **** @ sina.comwrote in message

news:11 ************* ********@w3g2000hsg.googlegrou ps.com ...


>>使用写入枚举类型到ostream时出现系列警告使用
运算符<<<< ;,

调用`std :: basic_ostream< _CharT,_Traits>&
std :: basic_ostream< _CharT,_Traits> :: operator< ;<(int)[with _CharT =
char,_Traits = std :: char_traits< char>]''
A.cpp:60:警告:传递`NAME_ENUM''选择`int''在`long int''

编译器gcc 3.4.3

从istream反序列化时,我必须首先使用一个int,然后
然后转换为枚举类型。并且需要当场检查范围。

我知道我可以重载operator<<和>对于枚举类型,但为每个枚举类型做那个是不是太无聊?我想知道如何在一次罢工中做到这一点,是吗?



您可以重载运算符<<和>用于枚举类型?我不认为你

可以覆盖<< >用于C ++的内置类型。我将不得不玩那个
。现在我正在做:


int种族,性别;

是> CChar.GM> / * ... * / Race> ;性别/ * ... * /;

CChar.Race = static_cast< ERaces>(种族);

CChar.Sex = static_cast< ESexes>(性别);


重载运算符<<和>对于我的ERaces和ESexes枚举会使

更清晰。



这似乎有效:


enum ERaces

{

Raceless,

人类,

天使,

恶魔,

恶魔

};


std :: istream& operator>>(std :: istream& is,ERaces& Race)

{

int IntRace;

is> IntRace;

Race = static_cast< ERACEs>(IntRace);

返回;

}


对我来说足够好。


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

解决方案

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


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


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


这篇关于序列化和反序列化枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆