I'm new to boost::bind and boost::mem_fn. I've read the documentation
and I think I understand what they do, but I'm not sure how to use them
to accomplish my task.
I have a class that contains several vectors in a map. I want to put a
"foreach" method on the class that will callback an arbitrary member
function, of an arbitrary class, for each item in the vector.
I'm stuck on the signature of that method.
A quick sketch of the structure:
class VectorContent
{
public:
void dosomething();
// there are bunches more, which is why I don't
// want a foreach for every possible choice.
};
class VectorHolder
{
std::map;
std::vector<VectorContent>& rvectGet(const std::string& rksName);
public:
void foreach( const std::string& rksName, );
};
// the callback
Caller::Bar( VectorContent& vc )
{
vc.dosomething();
}
I see two options for the caller:
// option 1
Caller::Foo1()
{
VectorHolder callee;
// in this case, foreach uses boost::mem_fn(), not me
callee.foreach( "VectorName", &this::Bar );
}
// option 2
Caller::Foo2();
{
VectorHolder callee;
callee.foreach( "VectorName", boost::mem_fn(&this::Bar) );
}
In the case of option 1, all callers must share a common base class,
which they do NOT, so that the signature of VectorHolder::foreach can be:
typedef void (BaseClass::* MyCallBack)(VectorContent&);
Leading to:
void VectorHolder::foreach( const std::string& rsName,
MyCallBack callback )
{
std::vector<VectorContent>& rvect = rvectGet( rsName );
std::for_each( rvect.begin(), rvect.end(), boost::mem_fn(callback) );
}
In the case of option 2, I have no idea what the signature should be.
But the resulting method would be:
void VectorHolder::foreach( std::string& rsName,
WHAT? callback )
{
std::vector<VectorContent>& rvect = rvectGet( rsName );
std::for_each( rvect.begin(), rvect.end(), callback );
}
Grateful for any help,
- Mark
P.S. If there's an "option 3", I'm up for that, too.