作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
通过以下示例,我尝试学习一些新概念。
我仍在努力弄清楚指针行为方式的细微差别。
这是我编写的一个小程序,用于向您展示我难以理解的问题。当我反序列化下面的多态对象时,我只得到一个从默认构造函数创建的对象。
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 流式传输到字符串,而无需进行不必要的复制。
#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/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!