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);
}

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to