gpt4 book ai didi

c++ - Boost - 仅在大小匹配时反序列化数组

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:53 25 4
gpt4 key购买 nike

当我尝试反序列化的数组大小与原始序列化数组的大小不匹配时,我希望反序列化失败。

到目前为止它只在 arr1_size > arr2_size 时失败我希望它是 arr1_size != arr2_size :

#include <iostream>
#include <sstream>
#include <array>

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

int main()
{
const size_t arr1_size = 4, arr2_size = 3;
std::stringstream ss;

// save
std::array<int, arr1_size> arr1;
boost::archive::text_oarchive oar(ss, boost::archive::no_header);
oar & arr1;

// load
std::array<int, arr2_size> arr2;
boost::archive::text_iarchive iar(ss, boost::archive::no_header);
iar & arr2; // throw on size inequality, please
}

Live on Coliru


我想过:

  • 序列化 std::vector s 并自己处理,但这可能会导致性能损失

  • 检查 arr2之后(如果它没有抛出 arr1_size > arr2_size )用于尾随默认构造的类类型元素或其他特殊值)来处理 arr1_size < arr2_size

有没有我错过的更简单的东西,最好由 boost 提供?

最佳答案

Here是你想绕过的Boost代码,特别是这个测试是不够的:

    if(static_cast<std::size_t>(count) > current_count)
boost::serialization::throw_exception(
archive::archive_exception(
boost::archive::archive_exception::array_size_too_short
)
);

一种解决方法是用您自己的序列化替换 std::array。如果您可以避免为序列化 std::array 的任何翻译单元包含 header boost/serialization/array.hpp,这是最简单的。如果您需要该头文件(例如序列化普通数组),仍然有可能 - 避免匹配 Boost templated function 的技巧:

template <class Archive, class T, std::size_t N>
void serialize(Archive& ar, std::array<T,N>& a, const unsigned int /* version */)
...

一种方法是明确指定您的元素类型:

typedef int MyArrayElementType;

namespace std {
template<class Archive, size_t N>
void serialize(Archive& ar, std::array<MyArrayElementType, N>& a, const unsigned int version)
...

这是对您的 MCVE 的改编:

#include <iostream>
#include <sstream>
#include <array>

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/array.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

// Supply your element type here.
typedef int MyArrayElementType;

namespace std {
template<class Archive, size_t N>
void serialize(Archive& ar, std::array<MyArrayElementType, N>& a, const unsigned int version) {
boost::serialization::split_free(ar, a, version);
}

template<class Archive, size_t N>
void save(Archive& ar, const std::array<MyArrayElementType, N>& a, const unsigned int version) {
// Adapted code from oserializer.hpp save_array_type::invoke().
boost::serialization::collection_size_type count(N);
ar << BOOST_SERIALIZATION_NVP(count);
ar << boost::serialization::make_array(static_cast<MyArrayElementType const*>(&a[0]), count);
}

template<class Archive, size_t N>
void load(Archive& ar, std::array<MyArrayElementType, N>& a, const unsigned int version) {
// Adapted code from iserializer.hpp load_array_type::invoke().
boost::serialization::collection_size_type count;
ar >> BOOST_SERIALIZATION_NVP(count);
if(static_cast<std::size_t>(count) != N)
boost::serialization::throw_exception(
std::runtime_error("std::array size mismatch")
);
ar >> boost::serialization::make_array(static_cast<MyArrayElementType*>(&a[0]), count);
}
}

int main()
{
const size_t arr1_size = 3, arr2_size = 4;
std::stringstream ss;

// save
std::array<int, arr1_size> arr1;
boost::archive::text_oarchive oar(ss, boost::archive::no_header);
oar & arr1;

// load
std::array<int, arr2_size> arr2;
boost::archive::text_iarchive iar(ss, boost::archive::no_header);
iar & arr2; // throw on size inequality, please
}

Live on CoLiRu

这使用与内置序列化相同的数组序列化机制,因此它应该具有完全相同的性能。如果您能够删除 boost/serialization/array.hpp,您可以将 MyArrayElementType 更改为模板参数。

关于c++ - Boost - 仅在大小匹配时反序列化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com