gpt4 book ai didi

c++ - boost序列化多态类

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:53 24 4
gpt4 key购买 nike

通过以下示例,我尝试学习一些新概念。

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

我仍在努力弄清楚指针行为方式的细微差别。

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

完整程序如下:

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

程序输出如下:

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:

我还发现 serialize 方法只被基类 aTodo 调用。我将需要找到一种方法使它成为虚拟的,但它是一个模板函数。这是第一个问题。

最佳答案

你的程序有 Undefined Behaviour因为所有的工厂函数都缺少返回值。

接下来,在类层次结构中使用类型代码是 Design Smell .

具体提示:

  • 序列化与反序列化相同的类型
  • 让 Boost Serialization 处理多态性(否则,为什么要使用多态性,或者为什么要使用 Boost Serialization?)。当您将(智能)指针序列化为 base 时,Boost 会处理它。
  • 注册您的类(class) (BOOST_CLASS_EXPORT)。您包含了 header 但没有使用它。
  • 工厂似乎没有理由。考虑放弃它

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

Live On Coliru

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

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

关于c++ - boost序列化多态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com