使用元编程进行消息队列管理

Using metaprogramming for message queue management

本文关键字:消息 队列 管理 编程      更新时间:2023-10-16

我正在C++中实现一个通信机制,该机制是用消息队列和消息类设计的。即抽象父类Message和类Communication,其中存在一个方法Communication::send(Message&)。类Communication将消息发送到由消息类型决定的适当的消息队列message_queue。(也就是说,对于Msg1,它发送给Queue_Msg1队列,Msg2发送给Queue_Msg2队列)每个消息类型都将作为Message的派生类创建。

主要是,我对自动创建队列感兴趣。也就是说,如果我决定添加一个新的消息类型类newMsg,那么添加消息队列Queue_newMsg的过程将不需要更改Communication类中的代码,例如为每个消息类型创建队列的代码。

由于这可以在编译时完成(在编译时,所有派生的消息类都是已知的,因此需要消息队列),因此我试图考虑一些元编程解决方案,但没有设法找到这样的解决方案。

使用一些已知的MPL,如boost/mpl,我如何实现上述目标?

将您的类型打包成列表:

template<typename... Ts>
struct type_list {};

使用该列表和参数包解包来创建队列的std::array。如果您希望队列本身具有特定的类型,则需要将它们放在tuple中。

上面的列表暗示了索引和类型之间的双向映射。让每种类型的实例返回索引,您可以使用索引来获取队列(在数组中很容易——在tuple中需要更多的工作)。

一个index_of特征类,在type_list<Ts...>中查找T类型的索引:

template<typename T, typename list, typename=void>
struct index_of {};
template<typename T, typename T0, typename... Ts>
struct index_of<T, type_list<T0, Ts...>, 
                typename std::enable_if<std::is_same<T, T0>::value>::type
               > :  std::integral_constant<size_t, 0> 
{};
template<typename T, typename T0, typename... Ts>
struct index_of<T, type_list<T0, Ts...>, 
                typename std::enable_if<!std::is_same<T, T0>::value>::type
               > :  std::integral_constant<size_t, 
                                   index_of<T, type_list<Ts...>>::value+1> 
{};

可能实现基于CRTP的"消息助手",实现GetTypeIndex并确保您的类型在中心消息列表中。

这需要c++ 11,在c++ 03中更难,也更有限。一个c++ 11编译器也可以处理100个类型,而不需要做太多额外的模板元编程(严格的元编程,至少在理论上,1000个或更多),而一个c++ 03编译器即使有一个健壮的元编程库,也可能被限制在10个类型。

请注意,这种方法的一个优点是,理论上,您可以完全取消抽象父类,或者至少取消sendMessage( message const& m )接口(为什么允许人们发送抽象消息?)您可能只被允许发送实际的具体消息类型。这同样需要更多的工作(创建使用CRTP获取队列的包扩展继承树)。

struct MessageBase {
  virtual size_t GetTypeIndex() const = 0;
};
template<typename D, typename List>
struct MessageHelper: public MessageBase {
  static_assert( std::is_base_of< MessageHelper<D,List>, D >::value, "MessageHelper<D> must be inherited from by D" );
  D* self() { return static_cast<D*>(this); }
  D const* self() const { return static_cast<D const*>(this); }
  virtual size_t GetTypeIndex() const final override {
    return index_of<D,List>::value;
  }
};
struct A_Queue {
  std::deque< std::unique_ptr<MessageBase> > data;
};
template<typename type_list>
struct MessageQueues;
template<typename... Ts>
struct MessageQueues<type_list<Ts...>> {
  std::array< A_Queue, sizeof...(Ts) > queues;
  void Enqueue( std::unique_ptr<MessageBase> msg ) {
    size_t index = msg->GetTypeIndex();
    queues[ index ].data.push-back( std::move(msg) );
  }
};

您可以在运行时注册不同的消息类型,而不是依赖于元编程。注册中心可以创建队列向量并提供唯一标识符,以最小化查找成本,或者如果您不太关心这一点,您可以始终使用从某个id到适当队列的映射。

虽然我不推荐它,但如果你真的想写一个复杂的模板解决方案,你可以看看类型列表。你需要的所有构建块都在Alexandrescu的Modern c++ Design中(类型列表,如何从中构建层次结构,以及一些花哨的模板技巧)。