Boost logo

Boost Users :

From: someuserat (marktutt_at_[hidden])
Date: 2002-09-18 10:39:02


First, many thanks to Peter Dimov for answering my earlier questions.

I've gotten hung up on something that may not be directly related to
shared_ptr, but it's in the mix here. I suspect the real problem is
my lack of experience using the STL algorithms, but I'm making an
effort not to go back to writing my own loops with this project.

Using my Widget theme, here's what I've got.

A WidgetManager class contains a stl::list of shared_ptr<Widget>
objects. It also contains a stl::vector of shared_ptr<WidgetMap>
objects. WidgetMap objects contain a B+tree. (I know, I should
probably change this to stl::map or multimap, I'll do that later) The
index maps a data member of a Widget to a shared_ptr to the Widget for
fast lookups. It is possible that new data members can be added or
removed from the Widgets, and we have functions for handling that.
Additional WidgetMap objects are added to the WidgetManager for Widget
data members as needed.

When adding a new WidgetMap, I need to loop through the existing set
of Widgets and add them to the new WidgetMap. This is the simplest
example I can think of as to what I'm trying to do, however obviously
this does not work...

void WidgetManager::addMap(const WidgetMapPtr i){
    maps_.push_back(i);
    // have to write a function adapter here?
    for_each(widgetset_.begin(),widgetset_.end(),i->insert);
}

Along a similar line, when a Widget's data member collection is
changed, I need to remove any WidgetMap objects fromm the
WidgetManager that referenced the member being deleted.

void WidgetManager::dropWidgetMember(const DataMemberPtr d){

    // delete any maps using the target member
    // how to fix this bloody line? getIndexedMember returns a
DataMemberPtr to the member it is keyed on.
maps_.erase(remove_if(maps_.begin(),maps_.end(),(boost::mem_fn(&WidgetMap::getIndexedMember)).equals(d)));
    // get rid of the member itself
members_.erase(remove_if(members_.begin(),members_.end(),boost::bind(&DataMember::equals,_1,d)));
    // rewrite the data file, we're all dirty anyway.
    this->compact();
}

I feel like I understand what I want to do, but I cannot find a
reference that explains how to do it the 'correct' way. At least not
that I can grasp. Can someone point me in the right direction?

Thanks,

Mark


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net