简体   繁体   English

如何序列化boost :: function以在message_queue中发送它

[英]How serialize a boost::function to send it in a message_queue

I am actually trying to serialize a boost::function using boost::serialize because I want to share it in a boost::interprocess::message_queue. 我实际上正在尝试使用boost :: serialize序列化boost :: function,因为我想在boost :: interprocess :: message_queue中共享它。 I only see one way to do that, it is to use the non-intrusive version of boost::serialize. 我只看到一种方法,它是使用非侵入式的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. 我还需要序列化vtable和functor,我没有尝试过,我不确定它是否可以正常工作。

So is there any way to serialize a boost::function in a proper way? 那么,有什么方法可以正确地序列化boost :: function?

Thank you. 谢谢。

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) 传递函数的上下文(例如,如果使用bind或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. 您想要的是Command模式,以及一种序列化这些命令的方法。

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. 对于反序列化,您将需要查找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. 在反序列化器中,您将需要构造相同的表,并使用序列化索引从表中获取实函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM