Erasing and modifying items in a Boost MultiIndex container

I am trying to use the Boost MultiIndex container in my simulation. My knowledge of C ++ syntax is very weak, and I am concerned that I am deleting an element from a container or deleting it from memory incorrectly. I also need to change the elements, and I was hoping to validate the syntax and basic philosophy here too.

// main.cpp ... #include <boost/multi_index_container.hpp> #include <boost/multi_index/hashed_index.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/mem_fun.hpp> #include <boost/tokenizer.hpp> #include <boost/shared_ptr.hpp> ... #include "Host.h" // class Host, all members private, using get fxns to access using boost::multi_index_container; using namespace boost::multi_index; typedef multi_index_container< boost::shared_ptr< Host >, indexed_by< hashed_unique< const_mem_fun<Host,int,&Host::getID> > // ordered_non_unique< BOOST_MULTI_INDEX_MEM_FUN(Host,int,&Host::getAge) > > // end indexed_by > HostContainer; typedef HostContainer::nth_index<0>::type HostsByID; int main() { ... HostContainer allHosts; Host * newHostPtr; newHostPtr = new Host( t, DOB, idCtr, 0, currentEvents ); allHosts.insert( boost::shared_ptr<Host>(newHostPtr) ); // allHosts gets filled up int randomHostID = 4; int newAge = 50; modifyHost( randomHostID, allHosts, newAge ); killHost( randomHostID, allHosts ); } void killHost( int id, HostContainer & hmap ){ HostsByID::iterator it = hmap.find( id ); cout << "Found host id " << (*it)->getID() << "Attempting to kill. hmap.size() before is " << hmap.size() << " and "; hmap.erase( it ); // Is this really erasing (freeing from mem) the underlying Host object? cout << hmap.size() << " after." << endl; } void modifyHost( int id, HostContainer & hmap, int newAge ){ HostsByID::iterator it = hmap.find( id ); (*it) -> setAge( newAge ); // Not actually the "modify" function for MultiIndex... } 

My questions

  • In the MultiIdex container, allHosts of shared_ptrs to Host objects calls allHosts.erase( it ) on the iterator for the shared_ptr object enough to delete the object permanently and free it from memory? It looks like you are removing shared_ptr from the container.
  • The allHosts container has one functional index that relies on the host identifier. If I present an ordered second index that calls a member function (Host :: getAge ()), where the age changes during the simulation, is the index always updated when I access it?
  • What is the difference between using MultiIndex to change the age of a base object compared to the approach I show above?
  • I am vaguely confused about what is supposed / required to be persistent in MultiIndex.

Thanks in advance.


Update

Here's my attempt to get modify syntax based on what I see in the Boost related example .

 struct update_age { update_age():(){} // have no idea what this really does... elicits error void operator() (boost::shared_ptr<Host> ptr) { ptr->incrementAge(); // incrementAge() is a member function of class Host } }; 

and then in modifyHost , I would have hmap.modify(it,update_age) . Even if by some miracle this turns out to be correct, I would like to get some explanation of what is happening.

+6
c ++ boost
source share
1 answer

shared_ptr will delete the actual Host object in its destructor (if there are no other instances of shared_ptr ). All objects in MultiIndex are considered permanent. To modify an object, you must use the modify MultiIndex method. In this case, the indices will be updated if necessary.

You can use the following functor to change the age field:

  struct change_age { change_age(int age) : age_(age) {} void operator()(boost::shared_ptr<Host> h) // shared_ptr !!! { h->age = age_; } private: int age_; }; 

Then use it as follows:

  testHosts.modify( it, Host::change_age( 22 ) ); // set age to 22 
+7
source share

All Articles