/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright: * 2016-2017, Danny Robson */ #ifndef CRUFT_VK_OBJECT_HPP #define CRUFT_VK_OBJECT_HPP #include "./traits.hpp" #include "./except.hpp" #include #include namespace cruft::vk { /// the base class for all non-trivial vulkan objects requiring lifetime /// management. template struct object { using id_t = typename id_traits::id_t; object (id_t); object (object&&); object (const object&) = delete; const id_t& id (void) const&; id_t& id (void) &; protected: id_t m_id; }; template struct root : public object { using id_t = typename object::id_t; template root (Args &&...args): object (make (std::forward (args)...)) { ; } ~root () { life_traits::destroy (this->id (), nullptr); } private: template static id_t make (Args &&...args) { id_t id; auto res = life_traits::create (std::forward (args)..., &id); error::try_code (res); return id; } }; /// a vulkan object that must be directly instantiated through /// constructor arguments, rather than being enumerated and owned by /// another object. template struct descendant : public object { using id_t = typename object::id_t; template descendant (Args &&...args): object (make (std::forward (args)...)) { ; } ~descendant () { life_traits::destroy (this->id (), nullptr); } private: template static id_t make (Base &&base, Args &&...args) { id_t id; auto res = life_traits::create (base.id (), std::forward (args)..., &id); error::try_code (res); return id; } }; /// a vulkan object that is obtained by listings from a parent object. template struct enumerated : public object { using object::object; static std::vector find (const instance &parent); }; /// a vulkan object that is directly owned by a parent object. /// /// typically implies that this object must be freed using a reference to /// the parent object. template struct owned : public object { using id_t = typename object::id_t; using owner_t = OwnerT; using object::object; template owned (Args &&...args): object (make (std::forward (args)...)) { ; } ~owned () { CHECK_EQ (this->id (), VK_NULL_HANDLE); } void destroy (OwnerT &parent) { life_traits::destroy (parent.id (), this->id (), nullptr); this->m_id = VK_NULL_HANDLE; } private: template static id_t make (OwnerT &owner, Args &&...args) { id_t id; life_traits::get (owner.id (), std::forward (args)..., &id); return id; } }; } #endif