【问题标题】:Boost Serialise a Boost Interprocess stringBoost Serialise 一个 Boost Interprocess 字符串
【发布时间】:2018-06-24 04:30:18
【问题描述】:

我有一个结构实例,它通过 Boost 进程间传递给 TCP/IP 客户端,在客户端中,我需要使用 Boost 序列化库对其进行序列化。由于这个结构包含 boost::interprocess basic_string,它不能直接序列化,所以我正在通过使用它们在序列化函数中构造 std::string 来解决这个问题。

using CharAllocator = boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager>;
using MyShmString = boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator>;

MyShmString uuid_;


template<typename _Archive>
void save( _Archive &ar, unsigned int const version ) const
{
    ar << std::string{ this->uuid_.c_str(), this->uuid_.length() };
}

template<typename _Archive>
void load( _Archive &ar, unsigned int const version )
{
    auto tmp = std::string{};
    ar >> tmp; this->uuid_ = tmp.c_str();
}

如果没有构造函数开销,有没有更好的方法来执行此操作?

【问题讨论】:

  • pse展示它是如何将它的“传递给TCP / IP客户端 i>”。 span>
  • @RustyX 我会,但代码太多了,它只是一个进程间双端队列

标签: c++ boost boost-serialization boost-interprocess


【解决方案1】:

对于std::string,包括

#include <boost/serialization/string.hpp>

成功了。老实说,我有点惊讶他们没有添加 boost::container::basic_string&lt;&gt; 支持 - 显然这是 YEARS 的一个未解决问题:https://svn.boost.org/trac10/ticket/8174

所以,我猜你现在应该手动完成。

您的“拐杖”解决方案将是最快的胜利。老实说,我不会投入更多时间,除非我确切地知道你所面临的情况。

注意 一个重要的认识是 std::string 被(正确地)标记为“原始类型”以用于提升序列化,这可以防止您因临时对象跟踪而感到头疼字符串对象。请务必包含上面的标题以保证正确的行为。

在这一点上,我有点奇怪你们同时使用共享内存序列化。您很可能可以直接利用共享内存缓冲区的特性 - 参见例如http://www.boost.org/doc/libs/1_66_0/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.managed_heap_memory_external_buffer)。

【讨论】:

  • 带有 STL 容器的序列化库很棒,这就是我选择它的原因。现在找到了解决方案(似乎正在工作并在下面发布)。我会看看托管的外部,听起来可能是个好方法。干杯
【解决方案2】:

对于任何想要它的人,我通过浏览用于序列化 std::vector 的 boost::interprocess 库源代码找到了答案,它与您可能需要的有点不同,因为我使用的是 1.65 所以'boost: :serialization::stl::load_collection' 已弃用。

如果有更好的方法,请发布。

template<typename _Archive, typename _T1, typename _T2, typename _Alloc>
inline void save( _Archive &ar, boost::interprocess::basic_string<_T1, _T2, _Alloc> const str, unsigned int const version )
{
    boost::serialization::stl::save_collection<
        _Archive,
        boost::interprocess::basic_string<_T1, _T2, _Alloc>>( ar, str );
}


template<typename _Archive, typename _T1, typename _T2, typename _Alloc>
inline void load( _Archive &ar, boost::interprocess::basic_string<_T1, _T2, _Alloc> &str, unsigned int const version )
{
    boost::archive::library_version_type const library_version{
        ar.get_library_version()
    };

    boost::serialization::item_version_type item_version{ 0 };
    boost::serialization::collection_size_type count;
    ar >> BOOST_SERIALIZATION_NVP( count );

    if ( boost::archive::library_version_type( 3 ) < library_version )
    {
        ar >> BOOST_SERIALIZATION_NVP( item_version );
    }

    str.reserve( count );
    boost::serialization::stl::collection_load_impl( ar, str, count, item_version );
}


template<typename _Archive, typename _T1, typename _T2, typename _Alloc>
inline void serialize( _Archive &ar, boost::interprocess::basic_string<_T1, _T2, _Alloc> &str, unsigned int const version )
{
    boost::serialization::split_member( ar, str, version );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2019-01-23
    • 1970-01-01
    相关资源
    最近更新 更多