Boost logo

Boost Users :

From: Nat Goodspeed (ngoodspeed_at_[hidden])
Date: 2006-12-11 07:52:06


From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Sohail Somani
Sent: Sunday, December 10, 2006 9:17 PM
To: boost-users_at_[hidden]; boost-users_at_[hidden]
Subject: Re: [Boost-users] shared_ptr with custom free function
From: boost-users-bounces_at_[hidden] on behalf of Nat Goodspeed

> > [Nat] It might be worth binding that knowledge into a specific
> > shared-pointer-to-blob type so that the required deleter function is
> > implicitly captured EVERY time you construct such a pointer.
>
> -------------
>
> How do you encode that knowledge into the type?

[Nat] I admit I haven't tried coding this. My thought is to derive your
"blob pointer" type from shared_ptr<blob>, implementing a subset of the
constructor variants that will be useful to you, and ensuring that the
constructor that accepts a dumb blob* pointer implicitly passes
free_blob to the base-class constructor. Something like this:

class blob;
void free_blob(blob*);

class blob_ptr: public shared_ptr<blob>
{
    typedef shared_ptr<blob> super;
public:
    // should only need to implement (a subset of) constructors
    // ...
    // This constructor is the reason blob_ptr is a class rather
    // than a typedef. Since we MUST use free_blob() to free a
    // heap blob instance, always bind free_blob() as shared_ptr's
    // custom deleter.
    blob_ptr(blob* ptr): super(ptr, free_blob) {}
    // ...
};


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