Boost logo

Boost Users :

Subject: [Boost-users] Delayed/Lazy Function Call
From: John M. Dlugosz (mpbecey7gu_at_[hidden])
Date: 2011-08-02 10:36:39


Consider something like this:

typedef std::tr1::function<void ()> CapsuleFunc;

class worker_thread_queue;
class forwarder {
    worker_thread_queue* queue;
    const NotifyFunc f;
public:
    forwarder (worker_thread_queue* queue, const NotifyFunc f)
       : queue(queue), f(f) {}
    void operator() (uint32_t extended_ID, State_t newstate, State_t oldstate);
    };

class worker_thread_queue {
    // doesn't really work, just for illustration
public:
    worker_thread_queue()
       {
       // really, would create a thread and thread-safe FIFO
       }
    // Calling submit will put f in a queue, and that queue is processed by the
    // worker thread. It removes each item in FIFO order and executes them.
    void submit (const CapsuleFunc& f) { /* but it's just an example */ }
    // Calling wrap will return another function that when executed will call submit(f) on
this instance.
    NotifyFunc wrap (const NotifyFunc& f)
       {
       forwarder x (this, f);
       return x;
       }
    };

void forwarder::operator() (uint32_t extended_ID, State_t newstate, State_t oldstate)
    {
    CapsuleFunc x= std::tr1::bind (f, extended_ID, newstate, oldstate);
    queue->submit (x);
    }

// later...
worker_thread_queue work_queue;
work_queue.wrap(&foo);

Presuming a compiler that doesn't have Lambda functions supported, is there some nicer way
to acheive the effect shown by 'wrap'?
Given a function object f and a object q, I want to create a new function object that when
invoked will bind its arguments and call q->submit with the bound-up form.

I've seen forwarders throughout Boost; is there a publicly available one that I can use?
Or some kind of "delayed apply" that can be called like bind, with the thing to be built
up? I recall there is a full heavy duty library for lazy evaluation but don't recall its
name. But I'm also wondering if there is a specific (lighter weight or already being used
within the Boost libraries I'm already using) mechanism for this.

—John


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