我开始在XML档案上使用boost :: serialization.我可以生成和读取数据,但是当我手工修改XML并交换两个标签时,它"无法失败"(即它会愉快地进行).
这是一个小的,自我完整的例子,展示了我所看到的:
#include <iostream>
#include <fstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/split_member.hpp>
using namespace std;
int main (void)
{
boost::archive::xml_oarchive oa (cout);
static const string producer = "XXX", version = "0.0.1";
oa << boost::serialization::make_nvp ("producer", producer);
oa << boost::serialization::make_nvp ("producer_version", version);
}
Run Code Online (Sandbox Code Playgroud)
这会将XML写入标准输出,其中包含:
<producer>XXX</producer>
<producer_version>0.0.1</producer_version>
Run Code Online (Sandbox Code Playgroud)
现在,我用一个阅读器替换main函数中的所有代码:
boost::archive::xml_iarchive ia (cin);
string producer, version;
ia >> boost::serialization::make_nvp ("producer", producer);
ia >> boost::serialization::make_nvp ("producer_version", version);
cout << producer << " " << version << endl;
Run Code Online (Sandbox Code Playgroud)
当输入前一个输出时,它按预期工作(输出"XXX 0.0.1").但是,如果我提供XML,其中我更改了两行"producer"和"producer_version"的顺序,它仍然运行并输出"0.0.1 XXX".
因此,它无法识别标签没有预期的名称,只是继续. …
我正在尝试使用boost序列化库将类序列化为字符串,并且在我的类中包含了几个双成员变量.
下面是我用来序列化的代码:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << mPoint;
Run Code Online (Sandbox Code Playgroud)
这是我的Point类中的序列方法:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
if (version > 0)
{
ar & mLatitude;
ar & mLongitude;
}
}
Run Code Online (Sandbox Code Playgroud)
当我序列化为字符串时,boost似乎没有像我期望的那样处理双字符串到字符串的转换(似乎存在舍入问题).研究一下,看起来其他人报告了相同的行为.我也理解与将double转换为字符串相关的精度相关问题,反之亦然,以及这可能导致问题.
有什么奇怪的,我不明白,虽然当我使用stringstream本身并将double重定向到流时,或者当我使用boost的lexical_cast函数从stringstream.str()返回时,这似乎不会发生一双.在发现boost有自己的序列化/反序列化类之前,我实际上已经使用stringstream和lexical_cast调用编写了自己的函数,并且它没有问题.我真的希望我不必放弃序列化库并回到之前的状态.希望只有一些设置/特征/等.我迷路了.
我有一个序列化的结构,通过套接字发送.我需要以块的形式读取它,因为它的一个字段包含剩余数据的大小:我需要读取前几个字节,找出长度并阅读其余部分.这就是我所拥有的:
boost::asio::streambuf buffer;
boost::system::error_code err_code;
// here I need to read only first 16 bytes
boost::asio::read(socket, buffer, err_code);
std::istream is(&buffer);
boost::archive::binary_iarchive ia(is);
ia >> my_struct;
Run Code Online (Sandbox Code Playgroud)
我看了看
boost::asio::async_read(s, boost::asio::buffer(data, size), handler);
Run Code Online (Sandbox Code Playgroud)
但它只能读取数据到boost :: asio :: buffer.我想知道我是否可以使用boost :: asio :: streambuf做同样的事情?先感谢您.
我们有很多使用boost :: serialization完美序列化的本机c ++类.
现在我们想将一些成员字段更改为property,因此我们可以在PropertyGrids中使用它们.当我们将类定义更改为ref class X时,我们得到了大量的编译错误:
#1:
error C2893: Failed to specialize function template 'boost::archive::text_oarchive &boost::archive::detail::interface_oarchive<Archive>::operator <<(T &)' d:\someAddress\someFile.cpp 58
#2:
error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::_Smanip<_Arg> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'boost::archive::text_oarchive' d:\someAddress\someFile.cpp 58
我们这里有很多小类,所以为每个类编写一个包装器会很麻烦!
这是我们使用的示例类:
ref class gps_position2
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & seconds;
}
public:
gps_position(){};
gps_position(float s)
{
this->seconds = …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个(工作)示例,用于外部序列化DLL中的类结构.目前我无法找到任何示例.Boost文档只是说明一些宏,论坛和新闻组只讨论他们的解决方案的具体问题.
所以我要求(外部)序列化类结构的例子,如下所示.除了类代码,我添加了一些用于序列化的代码(这不起作用,请参见底部的错误消息).
class Foo
{
public:
Foo() { number_ = 0; }
virtual ~Foo() {}
int getNumber() { return number_; }
void setNumber( int var ) { number_ = var; }
private:
int number_;
};
class Bar : public Foo
{
public:
Bar() { doubleNumber_ = 0.0; }
virtual ~Bar() {}
double getDouble() { return doubleNumber_; }
void setDouble( double var ) { doubleNumber_ = var; }
private:
double doubleNumber_;
};
Run Code Online (Sandbox Code Playgroud)
我到目前为止所有的代码都是这样的代码:
serializeFoo.h
#ifndef _SERIALIZE_FOO_H_
#define _SERIALIZE_FOO_H_
#include "Foo.h"
#include …
Run Code Online (Sandbox Code Playgroud) 我一直在尝试序列化,但每次我遇到如下错误: -
error: 'class std::vector<int, std::allocator<int> >' has no member named 'serialize'
Run Code Online (Sandbox Code Playgroud)
这是我的源代码和序列化方法,知道我使用的是boost.serialize
template <class E, class T>
class heap{
vector<E> * hp;
int index;//index is pointing to first empty place after the last element
int maxsize;
T comp;//comparable object designed to compare the objects
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<. Likewise, when the class Archive
// is a type of input archive …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 boost 运行单元测试,但当我运行测试时它给了我错误 - > “未知位置(0):“MyCheckTest”中的致命错误:”
错误甚至没有提到测试失败的行。我无法弄清楚,因此共享代码:
// 函数.hpp
#pragma once
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/access.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
struct Function
{
std::string id, name;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int /*version*/)
{
ar & id;
ar & name;
}
public:
Function(void);
typedef int parameter_strings;
Function(const parameter_strings & parms) { }
virtual ~Function(void);
};
BOOST_CLASS_EXPORT_KEY(Function);
Run Code Online (Sandbox Code Playgroud)
// 函数.cpp
#include "functions.hpp"
Function::~Function()
{
std::cout<< " destructor called \n"; …
Run Code Online (Sandbox Code Playgroud) 在我们的应用程序中,我们使用std::map
存储(键,值)数据并使用序列化将该数据存储在磁盘上.通过这种方法,我们发现磁盘I/O是性能瓶颈,使用密钥查找值不是很快.
我遇到了LevelDB并且正在考虑使用它.但我有一些问题.
std::map
和LevelDB 之间的区别在于LevelDB是持久的并且std::map
在内存中工作.这是否意味着磁盘I/O瓶颈对于levelDB来说会更成问题.更具体地说,任何人都可以解释一下LevelDB是否可能是更好的选择std::map
?
PS:我尝试使用hash_map
s但它看起来比较慢std::map
我已经递交了一份文件,该文件定义了一组通过串行通信信道传输和接收的消息.我想将传入的消息和反序列化为对象,并序列化我的出站消息.线路上的编码是建立的并且是不可改变的,并且由报头中的各种位域和不同的有效载荷组成,例如,
class Message{
int msg_num : 7
int dest_addr : 4
bool SRR : 1
bool IDE : 1
int source_addr : 6
//... and so on...
}
Run Code Online (Sandbox Code Playgroud)
我看了一下使用protobufs,但似乎建立了他们的varint编码方法.我也看过boost-serialization,但根据我到目前为止所读到的内容,编码是如何完成的还不完全清楚.
那么,有几个问题:
你会如何使用boost :: serialization序列化/反序列化这个类?
#include <vector>
struct Foo {
struct Bar {
std::vector<int> * data; // Must point to Foo::data
Bar( std::vector<int> * d ) : data(d) { }
};
std::vector<int> data;
std::vector<Bar> elements;
Foo() {
// do very time consuming calculation to populate "data" and "elements"
}
};
Run Code Online (Sandbox Code Playgroud)
当从序列化数据加载objected时,不得执行Foo中的构造函数,但如果该对象是默认构造的,则必须对构造函数进行求值.
可以向Bar添加默认构造函数,但在序列化之后,Foo :: Bar :: data必须指向Foo :: data.
编辑:以下是我的尝试的非工作实现
这是我基于@Matthieu提示的尝试.问题是,当我反序列化Foo时,我在Foo :: data和Foo :: elements中没有得到任何元素.
struct Foo {
struct Bar {
std::vector<int> * data;
Bar( ) : data( 0 ) { }
Bar( …
Run Code Online (Sandbox Code Playgroud) c++ ×9
boost ×7
boost-asio ×1
c++-cli ×1
dll ×1
leveldb ×1
managed-c++ ×1
stdmap ×1
streambuf ×1
vector ×1
visual-c++ ×1