gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:00:59 25 4
gpt4 key购买 nike

我的应用程序有一个类“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 序列化文档,找到了 Binary object wrappers 的概念,但是它导致了编译错误。

主要功能:-

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&’

使用对象池序列化是正确的方法吗?
非常感谢您的帮助。

最佳答案

来源于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

DEMO

关于从 Boost object_pool 构造的指针的 C++ Boost 二进制序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com