Hi,
I'm really stuck on this one, so I'm hoping someone can lend me a hand.
Basically, I'm defining a templated multi-index-container to hold one piece
of data along with two indexes to that data. Next, I'm wrapping that in
another templated class that will handle concurrent access to that
structure.
My problem is that I'm having trouble figuring out how to define a type that
will give me an interator on the 2nd index of the
multi-index-container ("bimimapIterator2"
below).
I'm basing this on the Boost multi-index-container example here:
http://www.boost.org/doc/libs/1_39_0/libs/multi_index/example/bimap.cpp
Here's what I have...
//********************************************************************
// multi-index-container
//********************************************************************
struct index1{};
struct index2{};
template
struct bimultimap
{
typedef boost::tuple value_type;
typedef boost::multi_index_container<
value_type,
indexed_by<
ordered_non_unique<
tag<index1>,member >,
ordered_non_unique<
tag<index2>,member >
>
bimimap;
};
//********************************************************************
// concurrent access class.
//********************************************************************
template
class concurrent_bimultimap
{
private:
typename bimultimap::bimimap the_bimultimap;
mutable boost::mutex the_mutex;
boost::condition_variable the_condition_variable;
public:
typedef typename bimultimap::bimimap bimimapType;
typedef typename bimultimap::bimimap::iterator
bimimapIterator;
typedef typename
bimultimap::bimimap::index<index2>::type::iterator
bimimapIterator2;
void insert(Key1 const& key1, Key2 const& key2, Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
the_bimultimap.insert(
bimultimap::bimimap::value_type(key1,key2,data) );
lock.unlock();
// Note: because we unlock above and notify below, we can starve the "pop"
thread for short durations in burst situations. That's ok so far.
the_condition_variable.notify_one();
}
bool findKey1(Key1& key1, Data& data)
{
boost::mutex::scoped_lock lock(the_mutex);
bimimapIterator itFindIt = the_bimultimap.find(key1);
if (itFindIt != the_bimultimap.end()) {
data = itFindIt->third;
return true;
}
else return false;
}
bool findKey2(Key2& key2, Data& data)
{
boost::mutex::scoped_lock lock(the_mutex);
bimimapIterator2 itFindIt = the_bimultimap.get<index2>().find(key2);
if (itFindIt != the_bimultimap.get<index2>().end()) {
data = itFindIt->third;
return true;
}
else return false;
}
..... there's more but this gives the essence of the problem ....
};