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

时间:2016-06-19 21:11:34

标签: c++ serialization boost deserialization stdarray

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

到目前为止它仅在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并自行处理,但这可能会导致性能下降

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

有没有更简单的东西,最好是我错过了提升?

1 个答案:

答案 0 :(得分:2)

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。如果您可以避免为序列化boost/serialization/array.hpp的任何翻译单元添加标题std::array,这是最简单的方法。如果您需要该头文件(例如序列化普通数组)仍然可以 - 避免匹配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更改为模板参数。