]> git.tdb.fi Git - libs/core.git/commitdiff
Add a construct to create conditional RAII objects
authorMikko Rasa <tdb@tdb.fi>
Fri, 28 Oct 2016 11:24:40 +0000 (14:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 28 Oct 2016 11:24:40 +0000 (14:24 +0300)
source/core/raii.h

index fde7e3e9dde2e8f72635d63654d8857039951f49..6cb24a95dcb4f026a6dd68409dfee3b8c4f87b51 100644 (file)
@@ -25,6 +25,24 @@ public:
        SetFlag(bool &r, bool v = true): SetForScope<bool>(r, v) { }
 };
 
+
+template<typename T>
+class Conditional
+{
+private:
+       char buf[sizeof(T)];
+       T *obj;
+
+public:
+       template<typename A>
+       Conditional(bool c, const A &a): obj(c ? new(buf) T(a) : 0) { }
+
+       template<typename A0, typename A1>
+       Conditional(bool c, const A0 &a0, const A1 &a1): obj(c ? new(buf) T(a0, a1) : 0) { }
+
+       ~Conditional() { if(obj) obj->~T(); }
+};
+
 } // namespace Msp
 
 #endif