加速C ++的序列化一个char * [英] Boost c++ serializing a char *
问题描述
我有一个类,我试图序列化一个shared_ptr,但序列化对象的常用的方法是行不通的:
类对象{
上市:
目的();
〜对象(); shared_ptr的<焦炭> objectone; 友元类的boost ::系列化::访问;
模板< typename的归档和GT;
无效连载(归档和放大器; AR,const的无符号整型版)
{
AR&安培; objectone;
}
};
我甚至企图它以这种方式,但它仍然无法正常工作:
无效连载(归档和放大器; AR,const的无符号整型版)
{
的for(int i = 0;我≤(strlen的(objectone.get()));我++)
AR&安培; objectone.get()[I];
}
任何想法如何处理呢?谢谢你。
一些额外的信息:
我已经既包括shared_ptr的头文件:
的#include<升压/系列化/ shared_ptr.hpp>
#包括LT&;升压/系列化/ shared_ptr_132.hpp>
我已经尝试转换为字符串,并以这种方式序列化,但它产生以下错误:
提高::档案:: archive_exception
什么():流错误
友元类的boost ::系列化::访问;
模板< typename的归档和GT;
无效连载(归档和放大器; AR,const的无符号整型版)
{
如果(objectone.get()){
串温度(objectone.get());
AR&安培;温度;
}
AR&安培; objectone;
}
您缺少一些关键概念。你并不需要一个share_ptr了,如果你使用一个std ::字符串。
您可以做
类对象{
上市:
目的();
〜对象(); 性病::字符串名称; 模板< typename的归档和GT;
无效连载(归档和放大器; AR,const的无符号整型版)
{
AR&安培;名称;
}
};
和你做。
加入
由于您的要求,这是你必须做的事情。
类测试
{
上市:友元类的boost ::系列化::访问;测试()
{
}模板<类归档和GT;
无效保存(归档和放大器; AR,const的无符号整型版)常量
{
INT LEN = strlen的(data.get());
AR&安培; LEN;
对于(INT指数= 0;指数 - LT; LEN ++指数)
AR&安培;数据[指数]
}
模板<类归档和GT;
无效负载(归档和放大器; AR,const的无符号整型版)
{
INT LEN = 0;
AR&安培; LEN;
数据提振= :: shared_array<&烧焦GT;(新的char [LEN + 1]);
对于(INT指数= 0;指数 - LT; LEN ++指数)
AR&安培;数据[指数]
数据[长度] = 0;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()提高:: shared_array<&烧焦GT;数据;
};
I have a class and I am trying to serialize a shared_ptr but the normal method of serializing an object is not working:
class Object {
public:
Object();
~Object();
shared_ptr<char>objectone;
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & objectone;
}
};
I've even attempted it in this way but it still doesn't work:
void serialize(Archive &ar, const unsigned int version)
{
for (int i = 0; i < (strlen(objectone.get())); i++)
ar & objectone.get()[i];
}
Any ideas how to approach this? Thanks.
Some extra information:
I've already included both shared_ptr header files:
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/shared_ptr_132.hpp>
I've attempted to convert to a string and serialize it in that way but it produces the following error: boost::archive::archive_exception' what(): stream error
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
if (objectone.get()) {
string temp(objectone.get());
ar & temp;
}
ar & objectone;
}
You are missing some key concepts. You don't need a share_ptr anymore if you use a std::string.
You can do
class Object {
public:
Object();
~Object();
std::string name;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & name;
}
};
and you are done.
Addition
Given your requirements this is what you have to do
class test
{
public:
friend class boost::serialization::access;
test()
{
}
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
int len = strlen(data.get());
ar & len;
for (int index = 0; index < len; ++index)
ar & data[index];
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
int len = 0;
ar & len;
data = boost::shared_array<char>(new char[len+1]);
for (int index = 0; index < len; ++index)
ar & data[index];
data[len] = 0;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
boost::shared_array<char> data;
};
这篇关于加速C ++的序列化一个char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!