I'm attempting to replace all of my "Acquire/Release" RAII classes (I have one for each kind of resource at the moment) with a single templated class. The general form of acquire is that some types are Acquire(), some are Acquire(p1), some are Acquire(p1, p2), etc. The same is true of Release. But if a resource is Acquired with parameters then it needs to be released with those same parameters.
I think I can do this with variadic templates, storing the arguments in a tuple. I've fallen down on the syntax of course. Can anyone assist?
#include <tuple>
template<class T, typename... Args>
class Raii
{
public:
Raii(T * s, Args&& ... a) : subect(s), arguments(a)
{
subject->Acquire(arguments);
}
~Raii()
{
subject->Release(arguments);
}
private:
T subject;
std::tuple<Args...> arguments;
};
class Framebuffer
{
public:
void Acquire() {}
void Release() {}
};
class Sampler
{
public:
void Acquire(int channel) {}
void Release(int channel) {}
};
class Buffer
{
public:
void Acquire(int target, int location) {}
void Release(int target, int location) {}
};
int main(void)
{
Framebuffer f;
Sampler s;
Buffer b;
auto b1 = Raii(&f);
{
auto b2 = Raii(&s, 10);
{
auto b3 = Raii(&b, 10, 20);
{
}
}
}
return 0;
}