如何序列化 boost::function 以将其发送到 message_queue

发布于 2024-10-07 20:59:14 字数 619 浏览 0 评论 0原文

我实际上正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说不完的你爱 2024-10-14 20:59:14

这不可能立即实现。

我能想到两个问题:

  • 传递函数的标识
  • 传递函数的上下文(例如,如果使用 bind 或 lambda 创建)

两者都不是微不足道的,也都无法完成无需检测代码(想想反射/内省)。

这里您需要的是Command 模式,以及序列化这些命令的方法。

这要求这两个进程都构建在一组通用命令之上(通用库似乎是个好主意),并且您为命令实现序列化和反序列化。

对于反序列化,您需要查找虚拟构造函数惯用语。

It's not going to be possible immediately.

There are 2 problems I can think of:

  • pass the identity of the function
  • pass the context of the function (for example, if created using 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.

锦上情书 2024-10-14 20:59:14

我不认为有什么办法可以做到这一点。为了能够序列化一个函数,您需要能够序列化它的二进制代码。但这是不可能的,因为代码至少是依赖于平台的。

但是,您可以创建一个函数表并序列化该表中函数的索引。在反序列化器中,您需要构建完全相同的表并使用序列化索引从表中获取实际函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文