查找boost multi index标记索引和索引数量 [英] Find boost multi index Tag to index and number of indices
本文介绍了查找boost multi index标记索引和索引数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模板类(CrMultiIndex),它接收boost多索引(GlobalHash)的定义作为模板参数.
I have a template class(CrMultiIndex) that receive as template parameter a definition of boost multi index(GlobalHash).
我需要:
- 根据使用的索引将统计信息添加到我的模板类中. 因此,我需要一种在初始化时使用现有索引数来调整vector(m_StatsByIndex)大小的方法.
- 我仍然希望用户根据标签而不是索引号进行搜索. 因此,我需要一种从标记转换为索引号的方法,以便可以根据矢量中的索引更新矢量中的统计信息.
- To add statistics to my template class according to Index used. So i need a way to resize the vector(m_StatsByIndex) at init with the number of existing indices.
- I still want the user to search according to tag and not index number. So i need a way to convert from tag to index number so i can update statistics in vector according to index in vector.
我有模板类
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;
具有根据标签的搜索功能
with a search function according to Tag
template <typename TagType,typename SearchingKey>
typename MultiIndexType::template index<TagType>::type::iterator
GetIteratorBy(SearchingKey & key)
{
return m_pMultiIndex->template get<TagType>().find(key) ;
}
代码位于 http://coliru.stacked-crooked.com/a/d97195a6e4bb7ad4
推荐答案
作为sehe答案的补充,这是对IndexOfTag
的重写,它不依赖于未记录的Boost.MultiIndex功能:
As a supplement to sehe's answer, this a rewrite of IndexOfTag
that does not depend on undocumented Boost.MultiIndex features:
template<typename MultiIndexContainer,std::size_t N=0>
struct index_position:index_position<MultiIndexContainer,N+1>
{
using index_type=typename boost::multi_index::nth_index<MultiIndexContainer,N>::type;
using index_position<MultiIndexContainer,N+1>::case_of;
static constexpr std::size_t case_of(std::in_place_type_t<index_type>){return N;}
};
template<typename MultiIndexContainer>
struct index_position<
MultiIndexContainer,
boost::mpl::size<typename MultiIndexContainer::index_type_list>::value
>
{
static constexpr void case_of(...){}
};
template <typename MultiIndexContainer,typename Tag>
constexpr std::size_t IndexOfTag()
{
using index_type=typename boost::multi_index::index<MultiIndexContainer,Tag>::type;
return index_position<MultiIndexContainer>::case_of(std::in_place_type<index_type>);
}
在C ++ 14中:
In C++14:
template<typename MultiIndexContainer,std::size_t N=0>
struct index_position:index_position<MultiIndexContainer,N+1>
{
using index_type=typename boost::multi_index::nth_index<MultiIndexContainer,N>::type;
using index_position<MultiIndexContainer,N+1>::case_of;
static constexpr std::size_t case_of(index_type*){return N;}
};
template<typename MultiIndexContainer>
struct index_position<
MultiIndexContainer,
boost::mpl::size<typename MultiIndexContainer::index_type_list>::value
>
{
static constexpr void case_of(...){}
};
template <typename MultiIndexContainer,typename Tag>
constexpr std::size_t IndexOfTag()
{
using index_type=typename boost::multi_index::index<MultiIndexContainer,Tag>::type;
return index_position<MultiIndexContainer>::case_of((index_type*)(nullptr));
}
这篇关于查找boost multi index标记索引和索引数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文