从 Boost object_pool 构造的指针的 C++ Boost 二进制序列化
C++ Boost binary serialization of pointer constructed from Boost object_pool
我的应用程序有一个 class "MyClass"。它的对象是从 Boost Object_pool.
构建的
我需要通过 Boost 二进制序列化 serialized/de 序列化此 class 对象。
用于序列化 -
我从池中取出一个指针,进行一些操作并通过 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&’
使用对象池序列化是正确的方法吗?
非常感谢您的帮助。
我建议制作一个 value-wrapper 或具有自定义序列化逻辑的自定义智能指针。
binary_object
方法将起作用,但仅适用于 POD 数据类型。
来源于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
我的应用程序有一个 class "MyClass"。它的对象是从 Boost Object_pool.
构建的我需要通过 Boost 二进制序列化 serialized/de 序列化此 class 对象。
用于序列化 -
我从池中取出一个指针,进行一些操作并通过 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&’
使用对象池序列化是正确的方法吗?
非常感谢您的帮助。
我建议制作一个 value-wrapper 或具有自定义序列化逻辑的自定义智能指针。
binary_object
方法将起作用,但仅适用于 POD 数据类型。
来源于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