|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-02-21 11:07:10
Sam Partington wrote:
> Something I've been doing lately:
>
> template<class T, void (*f)(T)> struct resource_deleter
> {
> void operator()(T* t)
> {
> if (t)
> f(*t);
> delete t;
> }
> };
>
> int get_int_based_resource() { return 0;}
> void release_int_based_resource(int) {}
>
> void f()
> {
> boost::shared_ptr<int> resource(new int(get_int_based_resource()),
> resource_deleter<int, release_int_based_resource>());
>
> // do something with the resouce
>
> }
You might want to use detail::shared_count instead. It may be undocumented
but it works and it sure isn't going away since that's what shared_ptr uses
under the hood.
#include <boost/detail/shared_count.hpp>
#include <iostream>
int get_int_based_resource()
{
std::cout << "get_int_based_resource()\n";
return 0;
}
void release_int_based_resource(int r)
{
std::cout << "release_int_based_resource(" << r << ")\n";
}
template<class X> class resource
{
public:
template<class D> resource(X x, D d): x_(x), n_(x, d)
{
}
private:
X x_;
boost::detail::shared_count n_;
};
int main()
{
resource<int> r(get_int_based_resource(), release_int_based_resource);
resource<int> r2(r);
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk