Is there a generic handle class in STL or boost? I am interfacing with some C code that has an alloc, release api. I'd like to use a handle to auto release resource.
For example:
some_resource_type rsc;
int err = capi_alloc(&rsc);
if (err != NOERR) {
// .. do work with resource
capi_release(rsc);
}
I want something like
// looking for this class
class wrapper {
public:
wrapper(T obj, void (del)(T&)):obj(obj_),del_(del) {}
~wrapper() {_del(obj);}
T obj_;
void (del_)(T&);
};
some_resource_type rsc;
int err = capi_alloc(&rsc);
wrapper w;
if (err != NOERR) {
w = wrapper(rsc, &capi_release);
// .. do work with resource
}
// then auto release
anything like this in STL or boost? It's essentially some unique pointer implementation with custom create and custom delete.
P.S. I haven't compiled the wrapper code, it may not work.