473,395 Members | 1,677 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,395 software developers and data experts.

Is there possiblitity to portable way to serialize enum type?

Thank you for answer, but I still did not
got *how* to make serialization of enum type.

Can you provide more instructions or hint, please?

My task is to serialize enum to something like byte array,
and then load enum from that byte array to the same enum
type on other platform.

One way to do I see is:
- static cast enum to integer
- convert integer to network byte order with htonl()
- transmit the result via network to remote host
- get result and convert it to host byte order with ntohl()
- cast integer back to enum

Is it portable way? Is there any well-known
tenchnics for such kind of tasks?

Thanks for help,
Konstantin
Jul 22 '05 #1
2 4257
"Voronkov Konstantin" <be*******@yahoo.com> wrote in message
news:77**************************@posting.google.c om...
Thank you for answer, but I still did not
got *how* to make serialization of enum type.

Can you provide more instructions or hint, please?

My task is to serialize enum to something like byte array,
and then load enum from that byte array to the same enum
type on other platform.

One way to do I see is:
- static cast enum to integer
- convert integer to network byte order with htonl()
- transmit the result via network to remote host
- get result and convert it to host byte order with ntohl()
- cast integer back to enum

Is it portable way? Is there any well-known
tenchnics for such kind of tasks?


What you described is a way. However you're not using a function to
transform the enum, you're simply casting it to an integer.

For instance you can write a function that converts an enum to string, then
back to deserialzie, from string to enum (using a series of if/else or other
methods).

HTH,
Elias
Jul 22 '05 #2
be*******@yahoo.com (Voronkov Konstantin) wrote in message news:<77**************************@posting.google. com>...
Thank you for answer, but I still did not
got *how* to make serialization of enum type.

Can you provide more instructions or hint, please?

My task is to serialize enum to something like byte array,
and then load enum from that byte array to the same enum
type on other platform.

One way to do I see is:
- static cast enum to integer
- convert integer to network byte order with htonl()
- transmit the result via network to remote host
- get result and convert it to host byte order with ntohl()
- cast integer back to enum

Is it portable way? Is there any well-known
tenchnics for such kind of tasks?

Thanks for help,
Konstantin


By example:

class RecordStream {
public:
//! Type of a record stream and database codes.
enum Type {
//! Unknown record stream type.
stUnknown = '\0',
//! Record stream generated from the external source.
stIncoming = 'i',
//! Record stream generated by the OSB.
stRegular = 'r',
//! Record stream contains error records.
stError = 'e',
//! Record stream contains filtered records.
stFilter = 'f'
};
};

RecordStream::Type RecordStream::toType(char c)
{
Type rc = Type(tolower(c));
bool done = false;
switch (rc) {
case stIncoming: // fall through for all defined types
case stRegular:
case stError:
case stFilter:
case stUnknown: done = true; break;
// no default: let compiler warn
}
if (!done) rc = stUnknown;
return rc;
}

char RecordStream::toChar(RecordStream::Type t)
{
int rc = t;
rc = toType(static_cast<char>(rc));
return static_cast<char>(rc);
}

Note the casts in order to avoid compiler warnings.

Stephan Brönnimann
br****@osb-systems.com
Open source rating and billing engine for communication networks.
Jul 22 '05 #3

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

Similar topics

2
by: Fernando Callejon | last post by:
Hello, i'm trying to serialize a class with a enum property...it seems to be serializing ok, but when i try to de-serialize it throw me an error like " isn't valid for type ". someone have done...
1
by: Voronkov Konstantin | last post by:
Hello, All! I want to ask question whether there is portable way to serialize enum type? Is it safe to threat enum as integer? Best Regards, Voronkov Konstantin
32
by: Martin Vorbrodt | last post by:
Hi there. Is this code portable between platforms? Is it also 100% standard compliant? #include <iostream> using namespace std; class Point { public: enum COORDS { X = 0, Y, Z }; Point(int...
1
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
1
by: Chris Dunaway | last post by:
Suppose I have the following enum: public enum MyEnum { EnumVal1 = 1, EnumVal2 = 2, EnumVal3 = 3 } I want to serialize the enum but I want the /names/ of the enum to be stored in the .xml...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.