本文介绍了C ++的boost ::序列化设置固定标识码一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'mm使用boost序列化和反序列化一些类
I'mm using boost to serialize and deserialize some classes
像这样:
boost::archive::xml_oarchive xmlArchive(oStringStream);
xmlArchive.register_type(static_cast<BaseMessage *>(NULL));
xmlArchive.register_type(static_cast<IncomingTradeMessage *>(NULL));
xmlArchive.register_type(static_cast<InternalRequestInfo *>(NULL));
xmlArchive.register_type(static_cast<InternalTradeTransInfo *>(NULL));
const BaseMessage* myMessage =message;
xmlArchive << make_nvp("Message", myMessage);
现在我clasess按照使用顺序得到标识码,我不想说,我想控制标识码的
now my clasess get a class_id according to the order used, i dont want that, i want to control the Class_id's
所以我可以做类似
BOOST_SET_CLASS_ID(1234, BaseMessage);
和无处不在我的项目BaseMessage将有1234标识码。
and everywhere in my project BaseMessage would have class_id of 1234.
我怎样才能做这样的事情。
How can i do such a thing
推荐答案
你不能使用 BOOST_CLASS_EXPORT_GUID
或类似呢?即。
Can't you use BOOST_CLASS_EXPORT_GUID
or similar instead? I.e.
BOOST_CLASS_EXPORT_GUID(IncomingTradeMessage, "IncomingTradeMessage")
...
它会使用一些更多的带宽,因为字符串传递,而不是整数,但它会解决你的问题。
It will use some more bandwidth since strings are transmitted rather than integers, but it will solve your problem.
参阅并获取更多信息。
编辑:
这编译就好了:
#include <fstream>
#include <boost/serialization/export.hpp>
#include <boost/archive/text_oarchive.hpp>
class Foo {
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & dummy1;
}
int dummy1;
public:
virtual ~Foo() {}
};
class Bar : public Foo {
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
ar & boost::serialization::base_object<Foo>(*this);
ar & dummy2;
}
int dummy2;
};
BOOST_CLASS_EXPORT_GUID(Foo, "Foo")
BOOST_CLASS_EXPORT_GUID(Bar, "Bar")
int main(int argc, char *argv[]) {
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
Foo *f = new Bar;
oa << f;
return 0;
}
这篇关于C ++的boost ::序列化设置固定标识码一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!