Boost logo

Boost :

From: John Madsen (johnmadsen_usenet_at_[hidden])
Date: 2003-07-19 21:14:57


Ross Smith <r-smith_at_[hidden]> wrote:
>>
>> struct win_file_handle {
>> typedef HANDLE handle_type;
>> static bool is_valid(handle_type h)
>> { return h != INVALID_HANDLE_TYPE; }
>> static void release(handle_type h)
>> { if (is_valid(h)) ::CloseHandle(h); }
>> static handle_type default_value()
>> { return INVALID_HANDLE_TYPE; }
>> static bool equal(handle_type lhs, handle_type rhs)
>> { return lhs==rhs; }
>> };
>>
>> (This example is taken from original post by John Madsen.)
>
>This strikes me as massive overkill. Why force the user to write a
>traits class when you can just pass the necessary information as an
>argument?
>

This doesn't strike me as massive overkill since release(), is_valid(), and
default_value() are necessary in many cases (e.g., for Windows' handles, where
the invalid handle value is (void*)-1 and not 0). An argument could be made
that the equal member is not necessary, but that hardly seems massive. If you
feel that this is too much typing, inheritance can be used to deal with classes
of related handles.

>
>Handles are essentially the same kind of resource management as smart
>pointers; the API should be as similar as possible. If the handle type

I argued at some length in my original post that handles are not essentially
the same as smart pointers. I may have been wrong there, but I'd loke someone
to address the points I made before I concede the issue.

>is itself a pointer, the existing boost::shared_ptr already solves the
>problem. For handle management, all we need is a couple of templates
>that take the handle type (instead of the pointed-to type) as a
>template argument but are otherwise identical to shared_ptr and
>scoped_ptr.
>

For reasons I mention above this is not sufficient.

John


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk