I'm writing a custom object pool because I couldn't find any that meet the specific requirements of my application. The object pool itself follows a simple model of "fetch a pre-allocated object from list if exists, otherwise create a new one and add to list of pre-allocated objects". So far so good: locking in case of multi-threading, RAII on returned object construction/destruction, and it has been templated so that I can specify any object type to "pool".
Now I'm at my final task. I want to be able to allow the client to specify a custom function to use which constructs a new object to be returned when requested, instead of the default constructor new _obj()
. Reasoning so that a non-default constructor can be used which perhaps someone may want to pre-bind some arguments and use that bounded constructor as the allocation method. A simplified example of usage:
class obj {
obj(int x) { ... }
};
void foo() {
object_pool<obj> pool(bind(&obj::obj, 1))
obj *o = pool.acquire() // obj pool will construct as "new obj(1)"
}
I've been looking into allocator
, which seems more appropriate for low-level memory-related performance than object construction. I've also considered boost::bind
with boost::function
or functor
's, which seems more inherently appropriate to the problem I'm trying to solve.
I'm sure this has been done before - so, what's the typical approach?