From 9c143246542e56d313c92565cd7212136a218e04 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 14 Dec 2017 20:21:24 -0800 Subject: [PATCH] Allow devices to be stored in AutoObj --- include/AL/alure2.h | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/include/AL/alure2.h b/include/AL/alure2.h index 75a0dde..b7119e2 100644 --- a/include/AL/alure2.h +++ b/include/AL/alure2.h @@ -296,6 +296,7 @@ public: // Tag type to disctate which types are allowed in AutoObj. template struct IsAutoable : std::false_type { }; +template<> struct IsAutoable : std::true_type { }; template<> struct IsAutoable : std::true_type { }; template<> struct IsAutoable : std::true_type { }; template<> struct IsAutoable : std::true_type { }; @@ -327,6 +328,21 @@ class AutoObj { T mObj; + template + EnableIfT::value,AutoObj&> do_reset(const U &obj={}) + { + if(mObj) mObj.destroy(); + mObj = obj; + return *this; + } + template + EnableIfT::value,AutoObj&> do_reset(const U &obj={}) + { + if(mObj) mObj.close(); + mObj = obj; + return *this; + } + public: using element_type = T; @@ -335,23 +351,17 @@ public: AutoObj(AutoObj &&rhs) noexcept : mObj(rhs.mObj) { rhs.mObj = nullptr; } AutoObj(std::nullptr_t) noexcept : mObj(nullptr) { } explicit AutoObj(const element_type &rhs) noexcept : mObj(rhs) { } - ~AutoObj() { if(mObj) mObj.destroy(); } + ~AutoObj() { do_reset(); } AutoObj& operator=(const AutoObj&) = delete; AutoObj& operator=(AutoObj &&rhs) { - if(mObj) mObj.destroy(); - mObj = rhs.mObj; + do_reset(rhs.mObj); rhs.mObj = nullptr; return *this; } - AutoObj& reset(const element_type &obj) - { - if(mObj) mObj.destroy(); - mObj = obj; - return *this; - } + AutoObj& reset(const element_type &obj) { return do_reset(obj); } element_type release() noexcept { -- 2.11.4.GIT