mardi 29 décembre 2015

How do you create a custom deleter for a unique_ptr class member that wraps a c function which requires 2 arguments?

I am trying to use mupdf to create a program (in Qt) that will allow me to list the objects of the document as a list and allow me to select which objects to render / not render. Since Qt is c++, and I am more comfortable with it, I am trying to wrap structures defined in mupdf in C++ classes. Right now my problem is this - one of the first things you do in mupdf is create a global context, that is passed around to everything, including functions that clean up and delete structures. I am familiar with creating an object that has an overloaded operator(), much like:

struct ContextDeleter
{
    inline void operator()(fz_context* ctx)
    {
        fz_drop_context(ctx);
    }
};

which you would then hand to unique_ptr -

std::unique_ptr<fz_context, ContextDeleter> ctxPtr;

What I can't figure out is how to do the same thing with function like:

fz_drop_page(ctx, page);

ie:

struct PageDeleter
{
     inline void operator()(fz_context* ctx, fz_page* pg)
     {
          fz_drop_page(ctx, pg);
     }
}
(this is obviously incoorect, but what I am trying to achieve)

How can I create a deleter to pass to a member of a class that is a unique_ptr that includes 2 arguments (in this case the necessary context pointer)? Is there a way for me to make unique_ptr aware of the context pointer to delete the (in this example) page? Or (one thought I had) do I need to create something that wraps the unique_ptr so I can hand it the context for deletion later somehow (haven't fully thought it through yet).

I have seen the examples here: How do I use a custom deleter with a std::unique_ptr member?

and

Wrapping of C-code with a unique_ptr and custom deleter

but I can't figure out how to make them work in my case.

Aucun commentaire:

Enregistrer un commentaire