Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-02-14 07:50:04


bjorn.karlsson_at_[hidden] wrote:
>
> HANDLE hPrinter=0;
> OpenPrinter(_T("Some printer"),&hPrinter,0);
> if (boost::shared_ptr<void> p(hPrinter,&ClosePrinter)) {
>
> }
>
> While this works, it doesn't convey the intent of the code (nor
> preserve it): the resource should typically not be shared, only
> scoped, and I believe that scoped_ptr<X,D> would be a perfect fit.

A zero-overhead scoped_ptr<X, D> would require a helper class here:

struct printer_deleter
{
    void operator()(void * pv) const { ClosePrinter(pv); }
}

scoped_ptr<void, printer_deleter> p(hPrinter);

The other option is

scoped_ptr<void, void (*) (void *)> p(hPrinter, &ClosePrinter);

that would increase sizeof(scoped_ptr) and is of questionable utility. :-)

There is always the option to define a dedicated printer class, of course.


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