代码之家  ›  专栏  ›  技术社区  ›  AK_

C++Booo::序列化设置类的固定类

  •  2
  • AK_  · 技术社区  · 15 年前

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

    现在我的领班根据使用的顺序得到一个班级ID,我不想这样,我想控制班级ID

    所以我可以做一些像

    BOOST_SET_CLASS_ID(1234, BaseMessage);
    

    在我的项目basemessage中的任何地方都将有1234的类ID。

    我怎么能做这种事

    2 回复  |  直到 9 年前
        1
  •  4
  •   Staffan    15 年前

    你不能用吗? BOOST_CLASS_EXPORT_GUID 还是类似的?即。

    BOOST_CLASS_EXPORT_GUID(IncomingTradeMessage, "IncomingTradeMessage")
    ...
    

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

    参照 this this 更多信息。

    编辑:

    这个编译很好:

    #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;
    }
    
        2
  •  1
  •   stinky472    15 年前

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

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

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