object: ensure make_owned destroys its object

This commit is contained in:
Danny Robson 2017-09-08 17:12:16 +10:00
parent 665a61a98e
commit 05e53b9161

View File

@ -158,15 +158,45 @@ namespace cruft::vk {
}; };
private: template <typename SelfT, typename OwnerT, typename ...Args>
template <typename ...Args> auto
static make_owned (OwnerT &owner, Args &&...args)
id_t make (OwnerT &owner, Args &&...args) {
{ class destroyer {
id_t id; public:
life_traits<SelfT>::get (owner.id (), std::forward<Args> (args)..., &id); destroyer (SelfT &&_self, OwnerT &_owner):
return id; 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> (args)... },
owner
};
}; };
}; };