有没有办法打出来的boost :: MPL的for_each的? [英] Is there a way to break out of boost::mpl for_each?

查看:190
本文介绍了有没有办法打出来的boost :: MPL的for_each的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题真的,让我给一些背景:

Simple question really, let me give some background:

我有一个的类型,每个类型都有一个id MPL ::矢量,在运行时我用的是 MPL :: for_each的通过这个载体进行迭代,并找到指定的ID匹配的类型。但是,一旦发现,就在不断的循环中没有点,所以 - 问题是,是否有办法摆脱它(的未抛出异常的)

I have a mpl::vector of types where each type has an id, at run time I use the mpl::for_each to iterate through this vector and find the matching type for the given id. But once found, there is no point in continuing the loop, so - question is, is there a way to break out of it (without throwing an exception)?

推荐答案

要实现类似 find_if 我改变了的for_each(称之为 exec_if )采取布尔模板参数。在布尔表示,如果执行应与下一序列,或者影响提前返回。

To implement something like find_if I changed the for_each (calling it exec_if) to take a bool template argument. The bool indicates if execution should with the next sequences, or in affect return early.

#include <iostream>

#include <boost/mpl/vector.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/assert.hpp>

namespace mpl = boost::mpl;

template< bool done = true >
struct exec_if_impl
{
  template<typename Iterator, typename LastIterator, typename Pred, typename Exec>
  static void execute(Iterator*, LastIterator*, Pred const&, Exec const&)
  {
  }
};

template<>
struct exec_if_impl<false>
{
  template<typename Iterator, typename LastIterator, typename Pred, typename Exec>
  static void execute(Iterator*, LastIterator*, Pred const& f, Exec const& e)
  {
    typedef typename mpl::deref<Iterator>::type item;

    if (!f(static_cast<item*>(0)))
    {
      typedef typename mpl::next<Iterator>::type iter;
      exec_if_impl<boost::is_same<iter, LastIterator>::value>
        ::execute(static_cast<iter*>(0), static_cast<LastIterator*>(0), f, e);
    }    
    else
      e(static_cast<item*>(0));
  }
};

template<typename Sequence, typename Pred, typename Exec>
inline
void exec_if(Pred const& f, Exec const& e, Sequence* = 0)
{
  BOOST_MPL_ASSERT(( mpl::is_sequence<Sequence> ));

  typedef typename mpl::begin<Sequence>::type first;
  typedef typename mpl::end<Sequence>::type last;

  exec_if_impl<boost::is_same<first,last>::value>
    ::execute(static_cast<first*>(0), static_cast<last*>(0), f, e);
}

namespace msg
{
  struct m1 { enum { TYPE = 1 }; static const char* name() { return "m1"; } };
  struct m2 { enum { TYPE = 2 }; static const char* name() { return "m2"; } };
  struct m3 { enum { TYPE = 3 }; static const char* name() { return "m3"; } };
  struct m4 { enum { TYPE = 4 }; static const char* name() { return "m4"; } };
  struct m5 { enum { TYPE = 5 }; static const char* name() { return "m5"; } };
}

struct checker
{
  checker(int chk_type) : type(chk_type) {}

  template <typename Mtype>
  bool operator()(Mtype* = 0) const
  {
    return Mtype::TYPE == type;
  }

  int type;
};

struct exec
{
  template <typename Mtype>
  void operator()(Mtype* = 0) const
  {
    std::cout << Mtype::name() << " executed" << std::endl;
  }
};

int main(void)
{
  typedef mpl::vector<msg::m1, msg::m2, msg::m3, msg::m4, msg::m5> mseq;

  checker chk(3); 

  exec_if<mseq>(chk, exec());

  return 0;
}

我把它改为 exec_if ,所以现在当predicate匹配,那么执行函子将与类型触发 - 这不正是我需要的

I changed this to exec_if, so now when the predicate matches, then the functor to execute will be triggered with the type - this does exactly what I need.

这篇关于有没有办法打出来的boost :: MPL的for_each的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆