提升多索引将索引转换为标签并在索引上循环 [英] boost multi index convert index to tag and loop on indexes

查看:123
本文介绍了提升多索引将索引转换为标签并在索引上循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类(CrMultiIndex),它接收一个模板参数,该参数定义了boost multi index(GlobalHash)。我使用c ++ 14

I have a template class(CrMultiIndex) that receive a template parameter a definition of boost multi index(GlobalHash).I work with c++14

我需要一种将索引转换为标签(n_to_tag)的方法?并在CrMultiIndex ctor或Init函数中循环索引?
我的最初目的是循环索引并在初始化时生成带有typeid(T).name()的标签名称字符串。这样我就可以根据标签名称显示统计信息

I need a way to translate index to tag(n_to_tag)? And to loop on indexes in CrMultiIndex ctor or Init function? My original purpose is to loop on indexes and generate tags names string with typeid(T).name() at init. So i can display statistics according to Tag name

我有模板类

template <typename KeysType, typename MultiIndexType>
class CrMultiIndex
{

    std::vector<SrStatisticsByIndex> m_StatsByIndex;

public:
    MultiIndexType *m_pMultiIndex=NULL; 

    CrMultiIndex()
    {
        m_pMultiIndex = new MultiIndexType(typename 
        MultiIndexType::ctor_args_list());
    }

这里是boost多索引容器的定义:

Here is the definition of boost multi index container:

typedef boost::multi_index::multi_index_container<
  CrUsersKeys,
  UsersKey_hash_indices/*,
  bip::allocator<CrUsersKeys,bip::managed_shared_memory::segment_manager>*/
> GlobalHash;

代码位于 http://coliru.stacked-crooked.com/a/d97195a6e4bb7ad4

我在查找增强的多索引标记以索引和编号索引

推荐答案

您可以使用类似这样的东西:

You can use something like this:

在Coliru上直播

template<typename MultiIndexContainer,std::size_t N>
std::string static_tag_name()
{
 const std::type_info& i=typeid(
    typename boost::mpl::front<
      typename boost::multi_index::nth_index<MultiIndexContainer,N>::
        type::tag_list
    >::type
  );
  return i.name();
}

template<typename MultiIndexContainer,std::size_t... I>
std::string tag_name(std::size_t n,std::index_sequence<I...>)
{
  static std::array<std::string(*)(),sizeof...(I)> a=
    {{static_tag_name<MultiIndexContainer,I>...}};
  return a[n]();
}

template<typename MultiIndexContainer>
std::string tag_name(std::size_t n)
{
  return tag_name<MultiIndexContainer>(
    n,std::make_index_sequence<
      boost::mpl::size<
        typename MultiIndexContainer::index_type_list
      >::value
    >{}
  );
}

这篇关于提升多索引将索引转换为标签并在索引上循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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