【问题标题】:c++ boost::serialization setting a fixed class_id for a classc++ boost::serialization 为一个类设置一个固定的class_id
【发布时间】:2010-06-30 11:32:31
【问题描述】:

我正在使用 boost 来序列化和反序列化一些类

像这样:

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);

现在我的班级根据使用的顺序得到一个class_id,我不想要那个,我想控制Class_id的

所以我可以做类似的事情

BOOST_SET_CLASS_ID(1234, BaseMessage);

在我的项目 BaseMessage 中的任何地方都会有 1234 的 class_id。

我怎么能做这样的事

【问题讨论】:

  • 为什么需要这样做?
  • 我使用它在 C# 和 C++(通过 tcp)之间进行通信,我希望该属性不依赖于存档看到类的顺序

标签: c++ serialization boost


【解决方案1】:

你不能改用BOOST_CLASS_EXPORT_GUID 或类似的吗?即

BOOST_CLASS_EXPORT_GUID(IncomingTradeMessage, "IncomingTradeMessage")
...

由于传输的是字符串而不是整数,它将使用更多带宽,但它会解决您的问题。

请参阅thisthis 了解更多信息。

编辑:

这个编译就好了:

#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;
}

【讨论】:

  • @Hellfrost:你能说得更具体点吗?它给出了什么错误?您使用的是什么 Boost 版本和编译器?我更新了我的答案以包含一个工作示例(它编译,我测试了)。
【解决方案2】:

我不确定这是否适用于您的情况(如果您的问题是专门寻求增强机制),但是字符串呢?据我所知,没有像这样的提升工具,但我已经将这种解决方案应用于我们的代码库:

#include <iostream>
#include <string>
using namespace std;

template <class T>
const char* my_type_id()
{
    return "Unknown";
}

#define REGISTER_TYPE(some_type)            \
    template <> inline                      \
    const char* my_type_id<some_type>()     \
    {                                       \
        return #some_type;                  \
    }

REGISTER_TYPE(int)
REGISTER_TYPE(std::string)

int main()
{
    // displays "int"
    cout << my_type_id<int>() << endl;

    // displays "std::string"
    cout << my_type_id<string>() << endl;

    // displays "Unknown" - we haven't registered char
    cout << my_type_id<char>() << endl;
}

它基本上是对 RTTI 的重新发明,如果您可以将其用于生产构建,则无需使用上述解决方案。我们不能这样做,因为它是针对软件开发工具包的,我们不想假设每个使用它的人都会启用 RTTI。

如果你需要整数而不是字符串,那么很容易适应这个:

template <class T>
int my_type_id()
{
    return -1;
}

#define REGISTER_TYPE(some_type, type_code) \
    template <> inline                      \
    int my_type_id<some_type>()             \
    {                                       \
        return type_code;                   \
    }

REGISTER_TYPE(int, 1234)
REGISTER_TYPE(std::string, 4567)

您甚至可以避免函数调用的开销,只需生成一个类,将这些与类型相关的整数值存储为枚举常量(保证为右值)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多