Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is there possiblitity to portable way to serialize enum type? (http://www.velocityreviews.com/forums/t285527-is-there-possiblitity-to-portable-way-to-serialize-enum-type.html)

Voronkov Konstantin 09-10-2004 07:07 AM

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

lallous 09-10-2004 10:27 AM

Re: Is there possiblitity to portable way to serialize enum type?
 
"Voronkov Konstantin" <beowinkle@yahoo.com> wrote in message
news:77a39ae7.0409092307.6441a3c3@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



Stephan Br?nnimann 09-10-2004 03:06 PM

Re: Is there possiblitity to portable way to serialize enum type?
 
beowinkle@yahoo.com (Voronkov Konstantin) wrote in message news:<77a39ae7.0409092307.6441a3c3@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
broeni@osb-systems.com
Open source rating and billing engine for communication networks.


All times are GMT. The time now is 02:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.