我有一个结构实例,该实例通过Boost进程间传递给TCP / IP客户端,在客户端中,我需要使用Boost序列化库对其进行序列化。由于此结构包含boost::interprocess basic_string,因此无法直接序列化,因此我正在通过使用它们在serialize函数中构造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();
}

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

最佳答案

对于std::string包括

#include <boost/serialization/string.hpp>

绝招。坦白说,我对他们没有添加boost::container::basic_string<>支持感到有些惊讶-显然这是YEARS的未解决问题:https://svn.boost.org/trac10/ticket/8174

所以,我想您现在应该手动进行。

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



在这一点上,我同时使用共享内存和序列化有点奇怪。您很可能可以直接利用共享内存缓冲区的特性-
参见例如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)。

关于c++ - Boost序列化Boost进程间字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48264747/

10-13 08:15
查看更多