I'm writing a templated class that encapsulates win32 handles RAII. This is what I've got so far:
#define NOMINMAX
#include <Windows.h>
#include <functional>
// problem: optionally pass additional params to the deleter function
template<typename ResourceT, std::function<void(ResourceT)> &Deleter>
class Win32Raii
{
Win32Raii(const Win32Raii &);
Win32Raii &operator=(const Win32Raii &);
public:
Win32Raii()
: m_resource(nullptr)
{}
Win32Raii(const ResourceT &r)
: m_resource(r)
{}
Win32Raii(Win32Raii &&other)
: m_resource(nullptr)
{
*this = std::move(other);
}
~Win32Raii()
{
if (m_resource)
{
Deleter(m_resource);
}
}
Win32Raii &operator=(Win32Raii &&other)
{
std::swap(m_resource, other.m_resource);
return *this;
}
ResourceT get() const { return m_resource; }
private:
ResourceT m_resource;
};
// library code for each resource type
std::function<void(HICON)>destroy_icon = [](HICON h){ ::DestroyIcon(h); };
std::function<void(HDC) > delete_dc = [](HDC h){ ::DeleteDC(h); };
//problem: pass real HWND first arg, not just a nullptr constant
std::function<void(HDC) > release_dc = [](HDC dc) { ::ReleaseDC(nullptr, dc); };
typedef Win32Raii<HICON, destroy_icon> HiconRaii;
typedef Win32Raii<HDC, delete_dc > HdcDelRaii;
typedef Win32Raii<HDC, release_dc > HdcRelRaii;
typedef Win32Raii<HMENU, destroy_menu> HmenuRaii;
//client usage examples
void main()
{
HWND hWnd = ::FindWindowA(nullptr, "some window");
// problem: pass hWnd
HdcRelRaii rdc(::GetDC(hWnd) /*, hWnd */);
HdcDelRaii ddc(::CreateCompatibleDC(rdc.get()));
HiconRaii h;
HiconRaii h2(::LoadIconW(nullptr, IDI_APPLICATION));
h = HiconRaii(std::move(h2));
HiconRaii h3 = std::move(h);
h3 = HiconRaii();
}
It's good for API's that take a single HANDLE argument and release it. Now, my problem is the API's that take multiple arguments in order to release a handle, like
ReleaseDC(HWND, HDC);
SelectObject(HDC, HGDIOBJ);
The client usage syntax I'm willing to achieve in case of multiple parameters to the deleter function is:
HdcRelRaii dc_to_release_in_dtor(::GetDC(hWnd), hWnd);
SelectObjRaii obj_to_reselect_in_dtor(::SelectObject(hBrush, hDC), hDC);
So, finally the question: how can I change
template<typename ResourceT, std::function<void(ResourceT)> &Deleter>
to something variadic like
template<
typename ResourceT,
std::function<void(ResourceT, Args&...) &Deleter,
typename... Args>
>
?
Obviousely, the non-type parameter Deleter depends on arguments that come after it, which is illegal. Hence: stuck...
Thanks in advance
P.S. any ideas for a better title, better usage syntax and so on, are welcome
Here's how it could work (using the make_index_sequence
of C++1y). Not sure of your design of using that reference parameter, though
template<typename Signature, std::function<Signature> &Deleter>
class Win32Raii;
template<typename ...Types, std::function<void(Types...)> &Deleter>
class Win32Raii<void(Types...), Deleter> {
public:
~Win32Raii() {
// do as proposed in http://stackoverflow.com/a/7858971/34509
callDelete(std::make_index_sequence<sizeof...(Types)>{});
}
private:
template<std::size_t ...I>
void callDelete(std::integer_sequence<std::size_t, I...>) {
Deleter(std::get<I>(args)...);
}
std::tuple<Types...> args;
};
This is how I would change it
template<typename Deleter, typename ...Params>
class Win32Raii {
public:
Win32Raii(Params... args, Deleter deleter = Deleter());
~Win32Raii() { /* copy most from above ... */ }
private:
Deleter deleter;
std::tuple<Params...> args;
};
This is completely compatible with a layer of indirection
template<typename Type, Type &Deleter>
struct ExoticDeleter {
template<typename ...T>
void operator()(T&&...t) const (
Deleter(std::forward<T>(t)...);
}
};
typedef Win32Raii<
ExoticDeleter<decltype(destroy_icon), destroy_icon>,
HICON
> HiconRaii;
I reversed the order, so that I multiple parameters could be given as simply a sequence without wrapping them. But that's just a matter of taste.