Boost logo

Boost Users :

Subject: [Boost-users] How to change the default enums serialization in Boost.Serialization
From: Andrés Senac (andres_at_[hidden])
Date: 2011-10-03 12:06:08


Hi,

By default in Boost.Serialization, enum types are serialized as a 32-bit
integer. But I need to serialize some enum types as different width integer.
I've tried to specialize the boost::serialization::serialize method, but it
seems it doesn't work for enums.

Here is my attempt:

 #include <iostream>

#include <boost/archive/binary_oarchive.hpp>

#include <boost/asio.hpp>

enum MyEnum_t

{

    HELLO, BYE

};

namespace boost

{

namespace serialization

{

template< class Archive >

void save(Archive & ar, const MyEnum_t & t, unsigned int version)

{

    unsigned char c = (unsigned char) t;

    ar & c;

}

template< class Archive >

void load(Archive & ar, MyEnum_t & t, unsigned int version)

{

    unsigned char c;

    ar & c;

    t = (MyEnum_t) c;

}

} // namespace serialization

} // namespace boost

BOOST_SERIALIZATION_SPLIT_FREE(MyEnum_t)

int main(int argc, const char *argv[])

{

    boost::asio::streambuf buf;

    boost::archive::binary_oarchive pboa(buf);

    buf.consume(buf.size()); // Ignore headers

    MyEnum_t me = HELLO;

    pboa << me;

    std::cout << buf.size() << std::endl; // buf.size() = 4, but I want 1

    return 0;

}

Thanks in advance,

Andrés

-- 
Andrés Senac González
andres_at_[hidden], andres.senac_at_[hidden]
+(34) 627550280


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net