From: Mikko Rasa Date: Fri, 28 Oct 2016 11:24:40 +0000 (+0300) Subject: Add a construct to create conditional RAII objects X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=dd522da4782ec2dab70bc80f7edab89db0ad826b;p=libs%2Fcore.git Add a construct to create conditional RAII objects --- diff --git a/source/core/raii.h b/source/core/raii.h index fde7e3e..6cb24a9 100644 --- a/source/core/raii.h +++ b/source/core/raii.h @@ -25,6 +25,24 @@ public: SetFlag(bool &r, bool v = true): SetForScope(r, v) { } }; + +template +class Conditional +{ +private: + char buf[sizeof(T)]; + T *obj; + +public: + template + Conditional(bool c, const A &a): obj(c ? new(buf) T(a) : 0) { } + + template + 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