boost multi index convert index to tag and loop on indexes

感情迁移 提交于 2019-12-12 07:04:15

问题


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

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

I have template class

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());
    }

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;

Code is at http://coliru.stacked-crooked.com/a/d97195a6e4bb7ad4

I asked a similar question at Find boost multi index Tag to index and number of indices


回答1:


You can use something like this:

Live On 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
    >{}
  );
}


来源:https://stackoverflow.com/questions/49260309/boost-multi-index-convert-index-to-tag-and-loop-on-indexes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!