473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Aug 14 '07 #1
10 3715
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
<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
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gad Rosenthal | last post by:
Can you direct the serialization process to correctly convert enum values? Example: public enum DiscreteOverlapping { none = -1, Ok = 0, StillOk = 0, Bad = -99
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
2
by: Earl Teigrob | last post by:
I am saving and restoring value types such as Int32, DateTime and Boolean in strings. I was wondering if there is a mechanism build into .NET for serializing and deserializing these to string...
1
by: Jim Hart | last post by:
Hi all! Can anyone explain this one to me. . . I have a 'Util' namespace with a 'Common' class that contains consts & values I use throughout multiple projects. One of the members of this...
7
by: fjlaga | last post by:
I have written an Office Add-in for Excel using VB.NET and the .NET 1.1 Framework (I have Visual Studio 2003 .NET ). All works great. I want to add a User Settings/Prefereneces dialog and allow...
2
by: she_prog | last post by:
I have a class derived from UserControl. I need to serialize an object of this class, but only some properties of it, as not all properties are serializable (some of the properties coming from...
0
by: =?Utf-8?B?dm96ZWxkcg==?= | last post by:
I'm having an issue in deserializing an object from XML. I generated my XML schema from my .NET dll using xsd.exe. I've used that schema in another application. That application spits out XML based...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.