Is there possiblitity to portable way to serialize enum type?

Discussion in 'C++' started by Voronkov Konstantin, Sep 10, 2004.

  1. 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
    Voronkov Konstantin, Sep 10, 2004
    #1
    1. Advertising

  2. Voronkov Konstantin

    lallous Guest

    "Voronkov Konstantin" <> wrote in message
    news:...
    > 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
    lallous, Sep 10, 2004
    #2
    1. Advertising

  3. (Voronkov Konstantin) wrote in message news:<>...
    > 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

    Open source rating and billing engine for communication networks.
    Stephan Br?nnimann, Sep 10, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordz
    Replies:
    3
    Views:
    3,925
    John Oakes
    Jun 7, 2004
  2. Voronkov Konstantin
    Replies:
    1
    Views:
    401
    Victor Bazarov
    Sep 9, 2004
  3. Voronkov Konstantin
    Replies:
    0
    Views:
    279
    Voronkov Konstantin
    Sep 10, 2004
  4. Eric Sosman
    Replies:
    3
    Views:
    770
    Mayeul
    Feb 26, 2010
  5. Wojtek
    Replies:
    1
    Views:
    484
Loading...

Share This Page