473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ost ream<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<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 3782
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_ost ream<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<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.neu f.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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 14 '07 #2
<yu****@sina.co mwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
>I got series warning when using write enum type to a ostream using
operator<<, like

in call to `std::basic_ost ream<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<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*******@rock etmail.comwrote in message
news:rd******** *****@newsfe02. lga...
<yu****@sina.co mwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
>>I got series warning when using write enum type to a ostream using
operator<<, like

in call to `std::basic_ost ream<_CharT, _Traits>&
std::basic_ost ream<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<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<Enu mType>(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...@rock etmail.comwrote :
"Jim Langston" <tazmas...@rock etmail.comwrote in message

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


<yu_...@sina.co mwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
>I got series warning when using write enum type to a ostream using
operator<<, like
in call to `std::basic_ost ream<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<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.co mwrote:
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<Enu mType>(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<Enu mType>(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...@gm ail.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_ost ream<_CharT, _Traits>&
std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int) [with _CharT =
char, _Traits = std::char_trait s<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.neu f.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 objektorientier ter Datenverarbeitu ng
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.co mwrote:
>>
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<Enu mType>(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<Enu mType>(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.co mwrote:
yu_...@sina.com wrote:
On Aug 15, 3:38 pm, Ian Collins <ian-n...@hotmail.co mwrote:
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<Enu mType>(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<Enu mType>(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2071
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
3168
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 the server (W2K3) it throws an exception. It only seems to happen when serializing/deserializing _arrays_ of a type. If I just serialize/deserialize one instance, it works fine. The exception I get is: (sorry for the word wrapping.)...
10
8299
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) This way I have to write another "serializing" function for every new kind of struct I want to write, though. Is there a way to write functions that can write/read any struct to/from plain text format in a portable way?
5
4795
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. "payload") using pointers. Examples of these are binary trees, hash tables etc. Thus, the message itself contains only a pointer to the actual data. When the message is sent to the same processor, these pointers point to the original locations, which...
2
3097
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 format. I can, of course, serialize a class to a file, either binary or XML, but this is not what I am looking for. Currently I am using ToString() or Convert.xxx to do this, but thought that if there was a true serializer, deserializer, that would be...
1
3259
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 'Common' class is an enum like so: public class Common { ....
7
2825
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 the user to specify some settings and I need to persist these settings between runs. I made a serializable class which uses the BinaryFormatter to serialize/deserialize the setttings. Serialization works great. However, when I try to...
2
697
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 UserControl are like that). When serializing, how could I ignore all the properties coming from the UserControl class? I know there is XmlIgnoreAttribute, but how could I set it to every property of UserControl, as it is not my class? Thank you...
0
2444
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 on user input and workflow. It is sending out the integer value for enums instead of the string values. This seems like it should be the most common sense way to do it (since it's an easy cast from int to an enum (MyEnum)myInt) but XML...
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.