如何序列化 boost::function 以将其发送到 message_queue
我实际上正在尝试使用 boost::serialize 序列化 boost::function ,因为我想在 boost::interprocess::message_queue 中共享它。 我只看到一种方法可以做到这一点,那就是使用非侵入式版本的 boost::serialize。
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, boost::function<void()> & fct, const unsigned int version)
{
ar & fct.args;
ar & fct.arity;
ar & fct.vtable;
ar & fct.functor;
}
}
}
我还需要序列化 vtable 和 functor,我没有尝试过,我不确定它是否有效。
那么有没有办法以正确的方式序列化 boost::function 呢?
谢谢。
I am actually trying to serialize a boost::function using boost::serialize because I want to share it in a boost::interprocess::message_queue.
I only see one way to do that, it is to use the non-intrusive version of boost::serialize.
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, boost::function<void()> & fct, const unsigned int version)
{
ar & fct.args;
ar & fct.arity;
ar & fct.vtable;
ar & fct.functor;
}
}
}
I will also need to serialize vtable and functor, I didn't try it, I am not sure it is working.
So is there any way to serialize a boost::function in a proper way?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不可能立即实现。
我能想到两个问题:
bind
或 lambda 创建)两者都不是微不足道的,也都无法完成无需检测代码(想想反射/内省)。
这里您需要的是
Command
模式,以及序列化这些命令的方法。这要求这两个进程都构建在一组通用命令之上(通用库似乎是个好主意),并且您为命令实现序列化和反序列化。
对于反序列化,您需要查找虚拟构造函数惯用语。
It's not going to be possible immediately.
There are 2 problems I can think of:
bind
or with a lambda)Neither is trivial, and neither can be done without instrumenting the code (think reflection / introspection).
What you want here is the
Command
pattern, and a way to serialize those commands.This requires that both processes are built on top of a common set of commands (a common library seems like a good idea) and that you implement serialization and deserialization for your commands.
For deserialization, you will want to look-up the Virtual Constructor Idiom.
我不认为有什么办法可以做到这一点。为了能够序列化一个函数,您需要能够序列化它的二进制代码。但这是不可能的,因为代码至少是依赖于平台的。
但是,您可以创建一个函数表并序列化该表中函数的索引。在反序列化器中,您需要构建完全相同的表并使用序列化索引从表中获取实际函数。
I don't think there is any way to do it. To be able to serialize a function you would need to be able to serialize its binary code. But that is not possible as the code is at least platform dependent.
You may however make a function table and serialize index of a function in that table. In the deserializer you would need to construct that very same table and use the serialized index to get the real function from the table.