Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a boost::archive::binary_oarchive handle an enum?

Tags:

c++

enums

boost

I have the following Enum

enum Example : uint8_t {
   First = 1,
   Second = 2,
 };

and a stringstream:

std::stringstream stream;
boost::archive::binary_oarchive ar(stream);

now i have noticed that, if i serialize an enum:

ar << Example::First;

boost serializes 4 byte (in this case 0x01, 0x00, 0x00, 0x00) bit instead of the needed 8 bit (0x01) for an uint8_t. Is there any way to avoid this? I mean, I know I can cast that enum to an uint8_t but this seams not very smart (and I have to change a lot of things if I have to do this).

Thanks and Greetings

like image 894
Ventu Avatar asked May 02 '26 14:05

Ventu


2 Answers

As always with Boost Serialization, to customize the treatment of user-defined types you'd need to implement the customization point which is either member serialize/load/save or free function serialize/load/save (looked up by ADL).

Since member functions are not an option for enums, you'd need to supply an overload of, e.g., serialize for your type. Sadly there is no way to get a generic implementation of that to be "better" than the predefined overloads for builtin primitive types.

Here's what would comes close (but it doesn't work ¹):

namespace boost { namespace serialization {

        template <typename Ar, typename T>
            typename std::enable_if<std::is_enum<T>::value, void>::type
            serialize(Ar& ar, T& e, unsigned) 
            {
                ar & boost::serialization::make_binary_object(&e, sizeof(e));
            }

} }

We can take the shortcut of "binary_object" serialization as we know by definition that enums have integral values as their underlying type, which makes them POD.

In the light of this - unfortunate - limitation, perhaps the best way is to manually call make_binary_object as shown:

Live On Coliru

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/serialization/serialization.hpp>
#include <iostream>
#include <sstream>

using boost::serialization::make_binary_object;

enum class Example : uint8_t {
    First  = 1,
    Second = 2,
};

int main() {

    std::stringstream stream;
    boost::archive::binary_oarchive ar(stream, boost::archive::no_header);

    auto data = Example::First;
    ar << make_binary_object(&data, sizeof(data));

    std::cout << "Size: " << stream.str().size() << "\n";
}

Which prints

Size: 1

as expected. You can use the make_binary_object wrapper inside serialize implementations and it will transparently take care of both serialization and deserialization.

See: Boost Serialization Wrappers in the Boost documentation


¹ for similar reasons, BOOST_IS_BITWISE_SERIALIZABLE(Example) will not work; I tested it

like image 148
sehe Avatar answered May 04 '26 04:05

sehe


I think the natural answer would be to convert the enum to the underlying type of the enum and (de)serialize that.

At the least, this will give a more readable output; and at the best, it will create more compatible archives across systems.

Also notice that binary serialization can break xml's (which I think it is a bug in Boost.Serialization)

using boost::serialization::make_binary_object;

enum class Example : uint8_t {
    First  = 1,
    Second = 2,
};

int main() {

    std::stringstream stream;
    boost::archive::xml_oarchive ar(stream, boost::archive::no_header);

    auto data = Example::First;
    auto data_ul = static_cast<uint8_t>(data);

    ar << boost::make_nvp("data", data_ul);
    
    ar << boost::make_nvp("data_bin", make_binary_object(&data, sizeof(data)));

    ar << make_binary_object(&data, sizeof(data));

    std::cout << "Size: " << stream.str() << "\n";
}

http://coliru.stacked-crooked.com/a/c824a88d139d07cf

output:

Size: <data>1</data>

<data_bin>

AQ==

</data_bin>


AQ==
like image 43
alfC Avatar answered May 04 '26 04:05

alfC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!