简体   繁体   English

使用Boost序列化的Microsoft GUID序列化?

[英]Microsoft GUID serialization using boost serialize?

I have the following: 我有以下内容:

struct Member
{
    GUID id;
    int extra;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int file_version)
    {
        ar & id;
        ar & extra;
    }

}

When I compile the code I get the following compile error: 编译代码时,出现以下编译错误:

Error 25 error C2039: 'serialize' : is not a member of '_GUID' 错误25错误C2039:“序列化”:不是“ _GUID”的成员

How do I specialise boost serialization for Microsoft GUID? 如何专门针对Microsoft GUID进行Boost序列化?

Here's a hint: assuming the UUID type is POD, you can use make_binary_object . 提示:假设UUID类型为POD,则可以使用make_binary_object

Here's a mockup type for UUID as a POD: 这是UUID作为POD的模型类型:

#include <array>

using UUID = std::array<uint8_t, 16>; // mock up
static_assert(std::is_pod<UUID>(), "assumes UUID is POD");

Any POD will do. 任何POD都可以。 Eg boost::uuids::uuid ¹ is also POD. 例如boost::uuids::uuid也是POD。 Same for struct UUID { char data[16]; } struct UUID { char data[16]; } struct UUID { char data[16]; } etc. struct UUID { char data[16]; }

Non-intrusive serialization: 非侵入式序列化:

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/binary_object.hpp>

namespace boost { namespace serialization {
    template <typename Ar>
        void serialize(Ar& ar, UUID& u, unsigned /*version*/) {
            ar & make_binary_object(&u, sizeof(u));
        }
} }

Demo 演示版

Live On Coliru 生活在Coliru

#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

int main() {
    std::stringstream ss;
    {
        boost::archive::text_oarchive oa(ss);
        UUID u {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}};
        oa << u;
    }

    std::cout << ss.str() << "\n";

    {
        boost::archive::text_iarchive ia(ss);
        UUID u {};
        ia >> u;

        std::copy(begin(u), end(u), std::ostream_iterator<int>(std::cout << "roundtripped: ", " "));
    }
}

¹ although that library already defines serialization in the boost/uuid/uuid_serialize.hpp header ¹尽管该库已经在boost/uuid/uuid_serialize.hpp标头中定义了序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM