Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

c++ - How does a boost::archive::binary_oarchive handle an enum?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 1):

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() << "
";
}

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


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...