#include #include #include #include #include #include template struct multi_index_any_iterator { typedef typename adobe::any_iterator< const typename MultiIndexContainer::value_type, /* warning: hashed indices aren't bidir */ std::bidirectional_iterator_tag > type; }; template struct multi_index_any_iterator_pair { typedef typename multi_index_any_iterator< MultiIndexContainer>::type iterator; typedef std::pair type; }; template class any_range_dispatcher { public: any_range_dispatcher() { boost::mpl::for_each > (range_function_array_filler(&f)); } typedef typename multi_index_any_iterator_pair< MultiIndexContainer>::type iterator_pair; iterator_pair operator()(const MultiIndexContainer& m,int n)const { return f[n](m); } private: typedef iterator_pair (*range_function)(const MultiIndexContainer&); BOOST_STATIC_CONSTANT( int, index_number= boost::mpl::size< typename MultiIndexContainer::index_type_list>::value); typedef range_function range_function_array[index_number]; template static iterator_pair nth_range(const MultiIndexContainer& m) { typedef typename multi_index_any_iterator< MultiIndexContainer>::type iterator; return iterator_pair( iterator(m.template get().begin()), iterator(m.template get().end())); } struct range_function_array_filler { range_function_array_filler(range_function_array* pf):pf(pf){} template void operator()(const N&) { (*pf)[N::value]=&nth_range; } range_function_array* pf; }; range_function_array f; }; template typename multi_index_any_iterator_pair::type range(const MultiIndexContainer& m,int n) { static any_range_dispatcher d; return d(m,n); } /* testing */ #include #include #include #include #include struct person { person(const std::string& name,const std::string& city): name(name),city(city) {} std::string name; std::string city; }; struct city{}; struct name{}; using namespace boost::multi_index; typedef multi_index_container< person, indexed_by< ordered_unique< tag,member >, ordered_non_unique< tag, member > > > person_set; int main() { person_set ps; ps.insert(person("Leonardo","Vinci")); ps.insert(person("Aristotle","Stagira")); ps.insert(person("Leibniz","Leipzig")); ps.insert(person("Cervantes","Alcala")); std::cout<<"0=name, 1=city: "; int n; std::cin>>n; multi_index_any_iterator_pair::type p=range(ps,n); while(p.first!=p.second){ std::cout<name<<" of "<city<