shared_resource<T>

C

CFG

I need a class similar to shared_ptr<> but without pointer semantics.

The class should implement thread-safe reference counting
and custom deleter to be call when RefCount==0

Examples:
shared_resource<HGDIOBJ> a(hBrush, ::DeleteObject);
shared_resource<HANDLE> a(hHandle, ::CloseHandle);
shared_resource<int> a(iFileHandleCRT, ::close);


Any hints?
 
J

Jonathan Turkanis

CFG said:
I need a class similar to shared_ptr<> but without pointer semantics.

The class should implement thread-safe reference counting
and custom deleter to be call when RefCount==0

Examples:
shared_resource<HGDIOBJ> a(hBrush, ::DeleteObject);
shared_resource<HANDLE> a(hHandle, ::CloseHandle);
shared_resource<int> a(iFileHandleCRT, ::close);

Here's the synopsis for several templates I implemented last year for
a win32 template library.

Base class for several handle-based types:

template<class Handle>
class gdi_object {
protected:
gdi_object();
gdi_object(Handle hnd, bool owns_handle);
public:
Handle handle() const;
operator Handle() const;
private:
struct Impl {
Impl(Handle hnd, bool owns) : hnd_(hnd),
owns_handle_(owns) { }
~Impl() { if (owns_handle_) ::DeleteObject(hnd_); }
Handle hnd_;
bool owns_handle_;
};
Impl* impl() const;
boost::shared_ptr<Impl> pimpl_;
};

Sample subclass (there are others for pens, etc.):

struct brush : public gdi_object<HBRUSH> {
brush() { }

// Constructs a Brush based on the given HBRUSH. Does not
transfer
// ownership of the HBRUSH.
brush(HBRUSH hbrush);
explicit brush(const color& rgb, uint style = BS_SOLID, uint
hatch = 0);
explicit brush(const bitmap& pattern);
void assign(const color& rgb, uint style = BS_SOLID, uint
hatch = 0);
void assign(const bitmap& pattern);
};

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top