C++ 从 Boost object_pool构造的指针的 Boost 二进制序列化



我的应用程序有一个类"MyClass"。它的对象是从Boost Object_pool构建的。

我需要通过 Boost 二进制序列化来序列化/反序列化此类对象。

对于序列化 -

我从池中获取一个指针,执行一些操作并通过 Boost 二进制序列化对其进行序列化。到目前为止,一切都很好。

对于反序列化 -

我获取该序列化缓冲区并使用 Boost 二进制序列化对其进行反序列化。反序列化成功发生,但在此过程中,Boost 序列化机制正在分配新内存,该机制不是从对象池构造的。
如果我在反序列化之前从池中分配指针,则反序列化会再次分配堆上的内存,并且池中的内存跟踪将丢失。因此,我将无法重用由 boost 序列化机制分配的此内存,因为它无法返回对象池,因为它不是从池构造的。

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/pool/object_pool.hpp>
#include <iostream>
#include <sstream>
#include <string>
class MyClass
{
public :
friend class boost::serialization::access;
MyClass():data(9)
{
std::cout << std::endl << "MyClass()" << std::endl ;
}
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & data;
}
~MyClass()
{
}
private :
int data ;
};

int main()
{
try
{
boost::object_pool<MyClass> pool ;
// Get object from the Pool.
MyClass *b = pool.construct();
// Serialization
std::stringbuf strbuf;
boost::archive::binary_oarchive oa( strbuf ) ;
oa << b ;
// Deserilaiztion
//MyClass *a = pool.construct() ;
MyClass *a ;
std::stringbuf strbufr(strbuf.str()) ; 
boost::archive::binary_iarchive ia( strbufr ) ;
// This calls the constructor and allocates memory for the pointer.
// If we allocates this from the pool, new memory is still allocated     
// and we loose track of the pointer fetched from the pool.   
ia >> a;
}
catch ( boost::archive::archive_exception &e )
{
std::cout << std::endl << e.what() << std::endl ;
}
}

我需要什么 -

我想将缓冲区反序列化为从池中分配的指针。

浏览了 Boost 序列化文档并发现了二进制对象包装器的概念,但它导致了编译错误。

主要功能 : -

int main()
{
try
{
boost::object_pool<MyClass> pool ;
MyClass *b = pool.construct();
std::stringbuf strbuf;
boost::archive::binary_oarchive oa( strbuf ) ;
oa << boost::serialization::binary_object(b, sizeof(MyClass) ) ;
MyClass *a = pool.construct() ;
std::stringbuf strbufr(strbuf.str()) ;
boost::archive::binary_iarchive ia( strbufr ) ;
ia >> boost::serialization::binary_object(b, sizeof(MyClass) );
}
catch ( boost::archive::archive_exception &e )
{
std::cout << std::endl << e.what() << std::endl ;
}
}

它给出了以下编译错误: -

serializationCpy.cpp: In function ‘int main()’:  
serializationCpy.cpp:56:8: error: no match for ‘operator>>’ (operand types are ‘boost::archive::binary_iarchive’ and ‘boost::serialization::binary_object’)
ia >> boost::serialization::binary_object(b, sizeof(MyClass) );  
^
serializationCpy.cpp:56:8: note: candidate is:
In file included from /home/boost_root/include/boost/archive/detail/common_iarchive.hpp:23:0,
from /home/boost_root/include/boost/archive/basic_binary_iarchive.hpp:30,
from /home/boost_root/include/boost/archive/binary_iarchive_impl.hpp:21,
from /home/boost_root/include/boost/archive/binary_iarchive.hpp:20,
from serializationCpy.cpp:1:
/home/boost_root/include/boost/archive/detail/interface_iarchive.hpp:67:15: note: Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = boost::serialization::binary_object; Archive = boost::archive::binary_iarchive]
Archive & operator>>(T & t){
^
/home/boost_root/include/boost/archive/detail/interface_iarchive.hpp:67:15: note:   no known conversion for argument 1 from ‘boost::serialization::binary_object’ to ‘boost::serialization::binary_object&’  

这是对对象池使用序列化的正确方法吗?
帮助将不胜感激。

来源于这个答案,我认为应该是

ia >> boost::serialization::make_binary_object(a, sizeof(MyClass));

ia & boost::serialization::make_binary_object(a, sizeof(MyClass));

而不是ia >> boost::serialization::binary_object(b, sizeof(MyClass) );

修复其他错别字,它将变成:

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/pool/object_pool.hpp>
#include <iostream>
#include <sstream>
class MyClass
{
public:
friend class boost::serialization::access;
MyClass(int data) :data(data)
{
std::cout << "nMyClass(" << data << ")n";
}
MyClass()
{
std::cout << "nMyClass()n";
}
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & data;
}
void printData()
{
std::cout << "Data = " << data << "n";
}
private:
int data;
};
int main()
{
try
{
boost::object_pool<MyClass> pool;
MyClass *b = pool.construct(6);
std::stringbuf strbuf;
boost::archive::binary_oarchive oa(strbuf);
oa << boost::serialization::binary_object(b, sizeof(MyClass));
MyClass *a = pool.construct();
std::stringbuf strbufr(strbuf.str());
boost::archive::binary_iarchive ia(strbufr);
ia >> boost::serialization::make_binary_object(a, sizeof(MyClass));
a->printData();
}
catch (boost::archive::archive_exception &e)
{
std::cout << std::endl << e.what() << std::endl;
}
return 0;
}

输出:

MyClass(6)
MyClass()
Data = 6

演示

我建议制作一个值包装器或有一个带有自定义序列化逻辑的自定义智能指针。

binary_object方法将起作用,但仅适用于 POD 数据类型。

相关内容

  • 没有找到相关文章