Sign in to follow this  

auto_"handle"?

This topic is 4098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

No. The Standard C++ Library is a cross-platform library, so it would be a little odd to provide a very platform-specific feature like that.

However, I believe you can use boost smart pointers and specify the clean-up function (CloseHandle or DeleteObject, depending on the type of handle). I haven't done this myself, yet, but someone recently corrected me in an earlier post, suggesting you could indeed do this.

Or you could quickly make your own, even. Here's a shoddy example, probably complete with bugs, compiler errors, and so forth, to give you an example:
class auto_handle
{
public:
explicit auto_handle(HANDLE handle);
~auto_handle();

operator HANDLE ();

private:
HANDLE mHandle;

auto_handle(const auto_handle& handle);
auto_handle& operator = (const auto_handle& handle);
};

explicit auto_handle::auto_handle(HANDLE handle) :
mHandle(handle)
{
}

auto_handle::~auto_handle()
{
CloseHandle(mHandle);
}

auto_handle::operator HANDLE ()
{
return mHandle;
}

auto_handle::auto_handle(const auto_handle& handle)
{
}

auto_handle& auto_handle::operator = (const auto_handle& handle)
{
}

Share this post


Link to post
Share on other sites

This topic is 4098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this