From 05e53b91617e592cd5e25e5b64b2908b6bdb902b Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 8 Sep 2017 17:12:16 +1000 Subject: [PATCH] object: ensure make_owned destroys its object --- object.hpp | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/object.hpp b/object.hpp index 9a6248a..d86d462 100644 --- a/object.hpp +++ b/object.hpp @@ -158,15 +158,45 @@ namespace cruft::vk { }; - private: - template - static - id_t make (OwnerT &owner, Args &&...args) - { - id_t id; - life_traits::get (owner.id (), std::forward (args)..., &id); - return id; - } + template + auto + make_owned (OwnerT &owner, Args &&...args) + { + class destroyer { + public: + destroyer (SelfT &&_self, OwnerT &_owner): + m_self (std::move (_self)), + m_owner (_owner) + { ; } + + destroyer (destroyer &&rhs): + m_self (std::move (rhs.m_self)), + m_owner (rhs.m_owner) + { ; } + + ~destroyer () + { + m_self.destroy (m_owner); + } + + // it's unclear whether we want to work around reseating m_owner, + // or if this operation just isn't sufficiently prevalent to + // justify the work. + destroyer& operator= (destroyer &&rhs) = delete; + + SelfT* + operator-> ()& + { return &m_self; } + + private: + SelfT m_self; + OwnerT &m_owner; + }; + + return destroyer { + SelfT { owner, std::forward (args)... }, + owner + }; }; };