I am trying to replace all my RAII Acquisition / Release classes (I have one for each type of resource at the moment) with one template. The general form of acquisition is that some types are Acquire (), some are Acquire (p1), some are Acquire (p1, p2), etc. The same applies to Release. But if a resource is acquired with parameters, it must be released with the same parameters.
I think I can do this with variable templates, storing the arguments in a tuple. Of course, I fell for the syntax. Can anyone help?
#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; }
source share