Is there possiblitity to portable way to serialize enum type?

  • Thread starter Voronkov Konstantin
  • Start date
V

Voronkov Konstantin

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
 
L

lallous

Voronkov Konstantin said:
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
 
S

Stephan Br?nnimann

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
(e-mail address removed)
Open source rating and billing engine for communication networks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top