如何重载serialize boost函数以使一个文件存储,另一个用于tcp消息?

时间:2021-12-28 00:41:50

I have class and I have implemented serialize function like (this is what I use to store on file and deserialize from file)

我有类,并且实现了序列化函数(这是我用来存储文件和从文件中反序列化的方法)

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & id;
        ar & first_name;
        ar & last_name;
        ar & salary;
        ar & username;
// and lot of more data
        ar & data;
    }

What I need in addition to serialize just couple members (not all, for example id, username, salary) to char array (for sending over tcp like message) and deserialize back (on client side). How can I overload serialize to have one for file storing and another for tcp messages ?

除了将几个成员(不是所有的,例如id、用户名、薪水)序列化为char数组(用于发送类似于tcp的消息)和反序列化(在客户端)之外,我还需要什么。如何重载序列化以使一个用于文件存储,另一个用于tcp消息?

1 个解决方案

#1


2  

You can create your own Archive classes and then specialize the serialize() function template for your classes:

您可以创建自己的存档类,然后专门化类的serialize()函数模板:

template<>
void serialize(MyBriefOutputArchive& ar, const unsigned int version) {
  // Alternative serialization.
}

template<>
void serialize(MyBriefInputArchive& ar, const unsigned int version) {
  // Alternative deserialization.
}

To make your own archive classes, you can subclass or copy the Boost classes so they otherwise work identically. Be careful because some archive classes, like boost::binary_oarchive and boost::binary_iarchive, have header comments that say they shouldn't be subclassed - you should copy their implementation instead (which is simple as all the work is done in their superclass).

为了创建您自己的存档类,您可以子类化或复制Boost类,以便它们能够以相同的方式工作。要小心,因为一些存档类,比如boost::binary_oarchive和boost::binary_iarchive,有标题注释说它们不应该被子类化——您应该复制它们的实现(这很简单,因为所有的工作都是在它们的超类中完成的)。

If you use some template meta-programming tricks, you can recognize your own archive class within the generic serialize() without the need for specializations. This would be preferred under the DRY principle, but it might be easier to get things working with specializations first.

如果您使用一些模板元编程技巧,您可以在通用串行化()中识别您自己的存档类,而不需要专门化。在DRY原则下,这将是首选,但是首先使用专门化可能会更容易。

#1


2  

You can create your own Archive classes and then specialize the serialize() function template for your classes:

您可以创建自己的存档类,然后专门化类的serialize()函数模板:

template<>
void serialize(MyBriefOutputArchive& ar, const unsigned int version) {
  // Alternative serialization.
}

template<>
void serialize(MyBriefInputArchive& ar, const unsigned int version) {
  // Alternative deserialization.
}

To make your own archive classes, you can subclass or copy the Boost classes so they otherwise work identically. Be careful because some archive classes, like boost::binary_oarchive and boost::binary_iarchive, have header comments that say they shouldn't be subclassed - you should copy their implementation instead (which is simple as all the work is done in their superclass).

为了创建您自己的存档类,您可以子类化或复制Boost类,以便它们能够以相同的方式工作。要小心,因为一些存档类,比如boost::binary_oarchive和boost::binary_iarchive,有标题注释说它们不应该被子类化——您应该复制它们的实现(这很简单,因为所有的工作都是在它们的超类中完成的)。

If you use some template meta-programming tricks, you can recognize your own archive class within the generic serialize() without the need for specializations. This would be preferred under the DRY principle, but it might be easier to get things working with specializations first.

如果您使用一些模板元编程技巧,您可以在通用串行化()中识别您自己的存档类,而不需要专门化。在DRY原则下,这将是首选,但是首先使用专门化可能会更容易。