Win32, RAII and C++ lambdas
Back when I was last writing serious amounts of C++ for pay (best part of a decade ago), I would end up with a whole bunch of little classes like this one:
to wrap those fiddly Win32 handle types with RAII behaviour; all much the same, most only being instantiated once, the only real difference being the contained type (which could be templated away) and the destructor behaviour (which isn't so easy).
With modern C++, you could wrap a pointer to the handle in a smart pointer with appropriate custom deleter. Or, avoiding the extra indirection from having to track the HANDLE
and a smart-HANDLE*
, take a leaf from how shared_ptr
is implemented and do:
which directly holds a value (expected to be something pointer-like, like the Win32 HWHATEVER
s) with RAII semantics; then in the code:
and apply the same wrapper to different types, injecting the deleter as a lambda:
No comments :
Post a Comment