0

I have a multi index with 2 indexes(in real code, they are of different type).

class CrUsersKeys{
  int IMSI;
  int TIMESTAMP;
}

After i find an entry in the multi index, I have the iterator of the entry.

auto it = multi.GetIteratorBy<IMSI_tag>(searchKey);

Now i want to loop through all the indexed members in this specific (*it) and check them. Note that i don't want to iterate through the iterator, but through the the indexed element of CrUsersKeys. How can i do it?

for(key in it)
{
     if(isGoodKey(key))
         std::cout<<"key "<<key <<" is good key"<<std::endl;
}

So it should check isGoodKey((*it).IMSI) and isGoodKey((*it).TIMESTAMP). CrUsersKeys is template parameter, so i can't really know the members of CrUsersKeys.

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

My multi index class is in shared memory.

yaron
  • 439
  • 6
  • 16

1 Answers1

0

Your question has little to do with Boost.MultiIndex and basically asks for a way to compile-time iterate over the members of a class. If you're OK with CrUsersKeys being defined as a std::tuple (or a tuple-like class), then you can do something like this (C++17):

Edit: Showed how to adapt a non-tuple class to the framework.

Live On Coliru

#include <tuple>

template<typename Tuple,typename F>
bool all_of_tuple(const Tuple& t,F f)
{
  const auto fold=[&](const auto&... x){return (...&&f(x));};
  return std::apply(fold,t);
}

#include <iostream>
#include <type_traits>

bool isGoodKey(int x){return x>0;}
bool isGoodKey(const char* x){return x&&x[0]!='\0';}

template<typename Tuple>
bool areAllGoodKeys(const Tuple& t)
{
  return all_of_tuple(t,[](const auto& x){return isGoodKey(x);});
}

struct CrUsersKeys
{
  int         IMSI;
  const char* TIMESTAMP;
};

bool areAllGoodKeys(const CrUsersKeys& x)
{
  return areAllGoodKeys(std::forward_as_tuple(x.IMSI,x.TIMESTAMP));
}

int main()
{
  std::cout<<areAllGoodKeys(std::make_tuple(1,1))<<"\n";        // 1
  std::cout<<areAllGoodKeys(std::make_tuple(1,"hello"))<<"\n";  // 1
  std::cout<<areAllGoodKeys(std::make_tuple(1,0))<<"\n";        // 0
  std::cout<<areAllGoodKeys(std::make_tuple("",1))<<"\n";       // 0
  std::cout<<areAllGoodKeys(CrUsersKeys{1,"hello"})<<"\n";      // 1
  std::cout<<areAllGoodKeys(CrUsersKeys{0,"hello"})<<"\n";      // 0
  std::cout<<areAllGoodKeys(CrUsersKeys{1,""})<<"\n";           // 0
}
Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20