views:

81

answers:

1

I currently have the following code:

using boost::bind;

typedef boost::signal<void(EventDataItem&)> EventDataItemSignal;
class EventDataItem
{
...
EventDataItemSignal OnTrigger;
...
}

typedef std::list< shared_ptr<EventDataItem> > DataItemList;
typedef std::list<boost::signals::connection>  ConnectionList;

class MyClass
{

void OnStart()
{
     DataItemList dilItems;
     ConnectionList clConns;

     DataItemList::iterator iterDataItems;
     for(iterDataItems = dilItems.begin();
         iterDataItems != dilItems.end();
         iterDataItems++)
     {
         // Create Connections from Triggers
         clConns.push_back((*iterDataItems)->OnTrigger.connect(
                               bind(&MyClass::OnEventTrigger, this)));
     }
}

void OnEventTrigger()
{
    // ... Do stuff on Trigger...
}
}

I'd like to change MyClass::OnStart to use std::transform to achieve the same thing:

void MyClass::OnStart()
{
     DataItemList dilItems;
     ConnectionList clConns;

     // Resize connection list to match number of data items
     clConns.resize(dilItems.size());
     // Build connection list from Items
     // note: errors on the placeholder _1->OnTrigger
     std::transform(dilItems.begin(), dilItems.end(), 
                    clConns.begin(),
                    bind(&EventDataItemSignal::connect, _1->OnTrigger, 
                             bind(&MyClass::OnEventTrigger, this)));
}

However, my hiccup is _1->OnTrigger. How can I reference OnTrigger from placeholder _1?

+3  A: 

You can solve it in the same way as http://stackoverflow.com/questions/2323413/accessing-member-variables-through-boost-lambda-placeholder: replace _1->OnTrigger with bind(&EventDataItem::OnTrigger, _1).

MSN
Tried this, the compiler returns an error: error: no matching function for call to 'bind(<unresolved overloaded function type>, ...)
sheepsimulator
@sheepsimulator, can you copy and paste in the entire compiler output?
MSN
MSN
@MSN - No, the code is not exactly what I am compiling, and you are correct, ::Stuff should be ::OnEventTrigger. Fixed via edit in my OP.
sheepsimulator
@sheepsimulator, without the full compiler output, it's hard to determine where it's failing.
MSN
@MSN - I understand; I simply don't have time to put it up here in a suitable format right now. The main reason I'm posting this question is to learn more about how this technique works, not to use it in production code right away, so I'm not really in a hurry here, and I apologize if that attitude is wearing your patience. I will post it when I can.
sheepsimulator
@sheepsimulator, ah, well, in that case, best of luck :)
MSN
I tried writing a bit more code, and I couldn't get a boost::bind to work to directly call a trigger. Which kinda befuddles me, because shouldn't it behave as a function object?
sheepsimulator