我的应用程序有一个类" MyClass"。它的对象是从Boost Object_pool构建的。
我需要通过Boost Binary Serialization序列化/反序列化这个类对象。
对于序列化 -
我从池中取一个指针,执行一些操作并通过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&’
使用对象池序列化的正确方法是什么? 帮助将受到高度赞赏。
答案 0 :(得分:1)
来自this回答,我认为应该是
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
答案 1 :(得分:0)
我建议制作一个value-wrapper或者使用自定义智能指针和自定义序列化逻辑。
binary_object
方式可行但仅适用于POD数据类型。