boost序列化多态类 [英] boost serialize polymorphic class

查看:56
本文介绍了boost序列化多态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过下面的例子,我试图学习一些新的概念.

  1. 抽象
  2. 多态类
  3. 工厂编程.
  4. 增强序列化

指针行为的细微差别仍然是我正在努力弄清楚的事情.

这是我编写的一个小程序,用于向您展示我难以理解的问题.当我反序列化下面的多态对象时,我只能得到一个从默认构造函数创建的对象.
TodoFactory::retrieveATodo 不会从序列化数据中重新创建对象.这由该函数中未序列化命令"的输出显示.

这是完整的程序:

#include #include #include #include #include #include #include //抽象类类aTodo{私人的:友元类 boost::serialization::access;受保护:常量字符_initType;民众:aTodo():_initType(0x00){};aTodo(const char type):_initType(type){};std::string oarchive(){std::ostringstream 存档流;{boost::archive::text_oarchive 存档(archive_stream);存档<<*这个;}archive_stream.flush();std::string outbound_data=archive_stream.str();std::string foutbound_data;foutbound_data=_initType;foutbound_data+=outbound_data;std::cout <<长度:"<<foutbound_data.length() <<std::endl;返回 foutbound_data;}虚空 Do()=0;虚拟 ~aTodo(){};模板<类存档>无效序列化(存档和ar,无符号整数版本){和_initType;};char getInitType(){return _initType;};};//包含以简单文本格式实现存档的标题类 todoExec:public aTodo{私人的:友元类 boost::serialization::access;模板<类存档>无效序列化(存档&,无符号整数版本){std::cout <<序列化 todoexec" <<std::endl;//根据boost::serialization::base_object(*this);//衍生的和_命令;}std::string _command;受保护:民众:静态常量字符_TYPE=0x01;todoExec():aTodo(_TYPE){};todoExec(std::string command):aTodo(_TYPE){_command=command;};void Do(){std::cout <<富"<<std::endl;};虚拟 ~todoExec(){};std::string getCommand(){return _command;};};类 todoFactory{私人的:受保护:民众:std::unique_ptr检索Atodo(const std::string & total){std::cout <<这里"<<std::endl;字符类型=total.at(0);std::cout <<位集:" <<std::bitset<8>(类型)<tmp(new todoExec());std::stringstream 存档流(剩余);std::cout <<流余数:" <<archive_stream.str() <<std::endl;{boost::archive::text_iarchive 存档(archive_stream);存档 >>*tmp;}std::cout <<非序列化类型:" <<std::bitset<8>(tmp->getInitType())<getCommand()<createAtodo(字符类型,std::string 命令){如果(类型== 0x01){std::unique_ptrtmp(new todoExec(command));返回 std::move(tmp);}};};int main(){字符类型=0x01;std::string dataToSend = "ls -al/home/ajonen";std::unique_ptrtmpTodoFactory;//创建工厂std::unique_ptranExecTodo=tmpTodoFactory->createAtodo(mtype,dataToSend);//从工厂创建ExecTodoif(auto* m = dynamic_cast(anExecTodo.get()))std::cout <<"要序列化的命令:" <<m->getCommand()<oarchive();//现在读入发回的结果std::unique_ptr结果;theResult=tmpTodoFactory->retrieveAtodo(remainder);std::cout <<"结果类型:" <<std::bitset<8>(theResult->getInitType()) <<std::endl;if(auto* d = dynamic_cast(theResult.get()))std::cout <<结果命令:" <<d->getCommand() <

这是程序输出:

 序列化命令:ls -al/home/ajonen长度:36这里位集:00000001检索中的剩余部分:22 序列化::归档 12 0 0 1流余数:22 序列化::归档 12 0 0 1序列化 todoexec非序列化类型:00000001反序列化命令:结果类型:00000001结果命令:

我还发现只有基类 aTodo 才会调用 serialize 方法.我将需要找到一种方法来使该虚拟化,但它是一个模板函数.这是第一个问题.

解决方案

你的程序有未定义行为 因为所有工厂函数都缺少返回值.

接下来,在类层次结构中使用类型代码是设计气味.>

具体提示:

  • 序列化与反序列化相同的类型
  • 让 Boost Serialization 来处理多态(否则,为什么要使用多态,或者为什么要使用 Boost Serialization?).当您将(智能)指针序列化到 base 时,Boost 会处理它.
  • 注册您的课程 (BOOST_CLASS_EXPORT).您已包含标题但未使用它.
  • 工厂似乎没有原因.考虑放弃它

一般来说,去除残留物.当您的代码太嘈杂时,很难思考.这是我清理过的版本:

生活在 Coliru

这也使用 Boost 流式传输到字符串,而无需进行不必要的复制.

#include #include #include #include #include #include #include 命名空间 Todo{struct BaseTodo {使用 Ptr = std::unique_ptr;虚拟 ~BaseTodo() = 默认值;虚空 Do() = 0;虚拟无符号 getInitType() { return 0x00;};私人的:友元类 boost::serialization::access;模板<类Ar>void serialize(Ar &, unsigned) {}};类执行:公共 BaseTodo {民众:Exec(std::string const &command = "") : _command(command){};虚拟无符号 getInitType() { return 0x01;};virtual void Do() { std::cout <<"foo:" <<获取命令()<(*this);ar &_command;}std::string _command;};}//BOOST_CLASS_EXPORT(BaseTodo)BOOST_SERIALIZATION_ASSUME_ABSTRACT(Todo::BaseTodo)BOOST_CLASS_EXPORT(Todo::Exec)命名空间 Todo{类工厂{工厂()=默认;民众:使用 Ptr = BaseTodo::Ptr;使用 FactoryPtr = std::shared_ptr;静态 FactoryPtr create() { return FactoryPtr(new Factory);}静态 std::string 保存(Ptr todo){std::string 输出;{命名空间 io = boost::iostreams;io::stream<io::back_insert_device<std::string>>操作系统(输出);boost::archive::text_oarchive 存档(操作系统);存档<<去做;}返回;}静态 Ptr 负载(std::string const &s){pt;{命名空间 io = boost::iostreams;io::streamis(io::array_source{ s.data(), s.size() });boost::archive::text_iarchive 存档(是);存档 >>p;}返回 std::move(p);}Ptr createExec(std::string command) { return BaseTodo::Ptr(new Exec(command));}};}int main() {汽车工厂 = Todo::Factory::create();//往返保存,加载自动待办事项=工厂->加载(工厂->保存(factory->createExec("ls -al/home/ajonen")));std::cout <<类型:" <<std::hex <<std::showbase <<todo->getInitType()<Do();}

With the following example I attempting to learn a few new to me concepts.

  1. abstraction
  2. polymorphic classes
  3. factory programming.
  4. boost serialization

The nuances of how pointers behave are still something I am working to figure out.

Here is a small program that I have written to show you the issue I am struggling to understand. When I unserialize the polymorphic object below I only get an object created from the default constructor.
TodoFactory::retrieveATodo is not recreating the object from the serialized data. This is displayed by the output of "unserialzed command" in that function.

Here is the full program:

#include <string>
#include <bitset>
#include <boost/serialization/string.hpp>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>

//abstract class
class aTodo{
private:

   friend class boost::serialization::access;

protected:
   const char _initType;

public:
   aTodo():_initType(0x00){};

   aTodo(const char type):_initType(type){};

std::string  oarchive(){
   std::ostringstream archive_stream;
   {
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   }

   archive_stream.flush();
   std::string outbound_data=archive_stream.str();

   std::string  foutbound_data;
   foutbound_data=_initType;
   foutbound_data+=outbound_data;
   std::cout << "length: " << foutbound_data.length() << std::endl;
   return foutbound_data;
}


   virtual void Do()=0;
   virtual ~aTodo(){};

   template<class Archive>
   void serialize(Archive & ar, unsigned int version){
      ar & _initType;
   };
   char getInitType(){return _initType;};
};

// include headers that implement a archive in simple text format
class todoExec:public aTodo{
private:
  friend class boost::serialization::access;
   template<class Archive>
   void serialize(
            Archive& ar,
            unsigned int version
            )
    {
      std::cout << "serialize todoexec" << std::endl;
    //base
    boost::serialization::base_object<aTodo>(*this);
//derived
        ar & _command;
    }

  std::string _command;
protected:

public:
   static const char _TYPE=0x01;
   todoExec():aTodo(_TYPE){};
   todoExec(std::string command):aTodo(_TYPE){_command=command;};
   void Do(){std::cout << "foo" << std::endl;};
   virtual ~todoExec(){};

   std::string getCommand(){return _command;};


};

class todoFactory{
private:

protected:


public:
   std::unique_ptr<aTodo> retrieveAtodo(const std::string & total){
   std::cout << "here" << std::endl;
   char type=total.at(0);
   std::cout << "bitset: " << std::bitset<8>(type) << std::endl;
   std::string remainder=total.substr(1);
   if(type==0x01){
      std::cout << "remainder in retrieve: " << remainder << std::endl;
      std::unique_ptr<todoExec> tmp(new todoExec());
      std::stringstream archive_stream(remainder);
      std::cout << "stream remainder: " << archive_stream.str() << std::endl;
   {     
      boost::archive::text_iarchive archive(archive_stream);
      archive >> *tmp;
      }
      std::cout << "unserialized type: " << std::bitset<8>(tmp->getInitType()) << std::endl;
      std::cout << "unserialized command: " << tmp->getCommand() << std::endl;
      return std::move(tmp);
   }
   };

   std::unique_ptr<aTodo> createAtodo(char type,std::string command){

      if(type==0x01){
         std::unique_ptr<todoExec> tmp(new todoExec(command));
         return std::move(tmp);
      }
   };


};

int main(){
   char mtype=0x01;
   std::string dataToSend = "ls -al /home/ajonen";
   std::unique_ptr<todoFactory> tmpTodoFactory; //create factory
   std::unique_ptr<aTodo> anExecTodo=tmpTodoFactory->createAtodo(mtype,dataToSend); //create ExecTodo from factory
   if(auto* m = dynamic_cast<todoExec*>(anExecTodo.get()))
      std::cout << "command to serialize: " << m->getCommand() << std::endl;
   //archive
   std::string remainder = anExecTodo->oarchive();
   //now read in results that are sent back
   std::unique_ptr<aTodo> theResult;
   theResult=tmpTodoFactory->retrieveAtodo(remainder);
   std::cout << "resultant type: " << std::bitset<8>(theResult->getInitType()) <<std::endl;
   if(auto* d = dynamic_cast<todoExec*>(theResult.get()))
      std::cout << "resultant Command: " << d->getCommand() <<std::endl;


   return 0;
}

And here is the program output:

command to serialize: ls -al /home/ajonen
length: 36
here
bitset: 00000001
remainder in retrieve: 22 serialization::archive 12 0 0 1

stream remainder: 22 serialization::archive 12 0 0 1

serialize todoexec
unserialized type: 00000001
unserialized command: 
resultant type: 00000001
resultant Command: 

I also found out that the serialize method is only being called for the base class aTodo. I am going to need to find a way to make that virtual, but it is a template function. That is problem number one.

解决方案

Your program has Undefined Behaviour because all of the factory functions have missing returns.

Next up, using a type code in a class hierarchy is a Design Smell.

Concrete hints:

  • serialize the same type as you deserialize
  • let Boost Serialization handle the polymorphism (otherwise, why do you use polymorphism, or why do you use Boost Serialization?). Boost handles it when you serialize (smart) pointers to base.
  • register your classes (BOOST_CLASS_EXPORT). You had included the header but didn't use it.
  • There doesn't seem to be a reason for the factory. Consider dropping it

In general, remove cruft. it's hard to think when your code is too noisy. Here's my cleaned up version:

Live On Coliru

This also uses Boost for streaming to string without unnecessary copying.

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/unique_ptr.hpp>

#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>

namespace Todo
{
    struct BaseTodo {
        using Ptr = std::unique_ptr<BaseTodo>;

        virtual ~BaseTodo() = default;
        virtual void Do() = 0;
        virtual unsigned getInitType() { return 0x00; };

      private:
        friend class boost::serialization::access;
        template <class Ar> void serialize(Ar &, unsigned) {}
    };

    class Exec : public BaseTodo {
      public:
        Exec(std::string const &command = "") : _command(command){};

        virtual unsigned getInitType() { return 0x01; };
        virtual void Do() { std::cout << "foo: " << getCommand() << std::endl; };

        std::string getCommand() const { return _command; };

      private:
        friend class boost::serialization::access;
        template <class Archive> void serialize(Archive &ar, unsigned) {
            boost::serialization::base_object<BaseTodo>(*this);
            ar &_command;
        }

        std::string _command;
    };
}

//BOOST_CLASS_EXPORT(BaseTodo)
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Todo::BaseTodo)
BOOST_CLASS_EXPORT(Todo::Exec)

namespace Todo 
{
    class Factory {
        Factory() = default;
      public:
        using Ptr = BaseTodo::Ptr;
        using FactoryPtr = std::shared_ptr<Factory>;

        static FactoryPtr create() { return FactoryPtr(new Factory); }

        static std::string save(Ptr todo) {
            std::string out;
            {
                namespace io = boost::iostreams;
                io::stream<io::back_insert_device<std::string> > os(out);

                boost::archive::text_oarchive archive(os);
                archive << todo;
            }

            return out;
        }

        static Ptr load(std::string const &s) {
            Ptr p;
            {
                namespace io = boost::iostreams;
                io::stream<io::array_source> is(io::array_source{ s.data(), s.size() });
                boost::archive::text_iarchive archive(is);
                archive >> p;
            }
            return std::move(p);
        }

        Ptr createExec(std::string command) { return BaseTodo::Ptr(new Exec(command)); }
    };
}

int main() {
    auto factory = Todo::Factory::create();

    // ROUNDTRIP save,load
    auto todo = factory->load(
            factory->save(
                factory->createExec("ls -al /home/ajonen")
            )
        );

    std::cout << "Type: " << std::hex << std::showbase << todo->getInitType() << std::endl;
    todo->Do();
}

这篇关于boost序列化多态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆