libcruft-vk/object.hpp

174 lines
4.6 KiB
C++
Raw Normal View History

2016-02-26 13:39:01 +11:00
/*
* 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:
2017-09-01 12:33:41 +10:00
* 2016-2017, Danny Robson <danny@nerdcruft.net>
2016-02-26 13:39:01 +11:00
*/
#ifndef CRUFT_VK_OBJECT_HPP
#define CRUFT_VK_OBJECT_HPP
2016-02-24 11:11:41 +11:00
#include "./traits.hpp"
#include "./except.hpp"
#include <utility>
#include <vector>
2016-02-24 11:11:41 +11:00
namespace cruft::vk {
2017-09-01 12:33:41 +10:00
/// the base class for all non-trivial vulkan objects requiring lifetime
/// management.
2016-02-24 11:11:41 +11:00
template <typename T>
struct object {
2017-09-08 17:11:35 +10:00
using native_t = id_t<T>;
2016-02-24 11:11:41 +11:00
2017-09-08 17:11:35 +10:00
object (native_t);
object (object&&);
2017-09-08 17:11:35 +10:00
object (const object&) = delete;
2016-02-24 11:11:41 +11:00
2017-09-08 17:11:35 +10:00
const native_t& native (void) const&;
native_t& native (void) &;
2016-02-24 11:11:41 +11:00
protected:
2017-09-08 17:11:35 +10:00
native_t m_native;
2016-02-24 11:11:41 +11:00
};
2017-09-05 17:20:17 +10:00
template <typename SelfT>
struct root : public object<SelfT> {
2017-09-08 17:11:35 +10:00
using native_t = typename object<SelfT>::native_t;
2017-09-05 17:20:17 +10:00
template <typename ...Args>
root (Args &&...args):
object<SelfT> (make (std::forward<Args> (args)...))
{ ; }
~root ()
{
2017-09-08 17:11:35 +10:00
life_traits<SelfT>::destroy (this->native (), nullptr);
2017-09-05 17:20:17 +10:00
}
private:
template <typename ...Args>
2017-09-08 17:11:35 +10:00
static native_t
2017-09-05 17:20:17 +10:00
make (Args &&...args)
{
2017-09-08 17:11:35 +10:00
native_t id;
2017-09-05 17:20:17 +10:00
auto res = life_traits<SelfT>::create (std::forward<Args> (args)..., &id);
error::try_code (res);
return id;
}
};
2017-09-01 12:33:41 +10:00
/// a vulkan object that must be directly instantiated through
/// constructor arguments, rather than being enumerated and owned by
/// another object.
2016-02-24 11:11:41 +11:00
template <typename T>
2017-09-05 17:20:17 +10:00
struct descendant : public object<T> {
2017-09-08 17:11:35 +10:00
using native_t = typename object<T>::native_t;
2016-02-24 11:11:41 +11:00
template <typename ...Args>
2017-09-05 17:20:17 +10:00
descendant (Args &&...args):
2016-02-24 11:11:41 +11:00
object<T> (make (std::forward<Args> (args)...))
{ ; }
2017-09-05 17:20:17 +10:00
~descendant ()
2016-02-24 11:11:41 +11:00
{
2017-09-08 17:11:35 +10:00
life_traits<T>::destroy (this->native (), nullptr);
2016-02-24 11:11:41 +11:00
}
private:
2017-09-05 17:20:17 +10:00
template <typename Base, typename ...Args>
2016-02-24 11:11:41 +11:00
static
2017-09-08 17:11:35 +10:00
native_t make (Base &&base, Args &&...args)
2016-02-24 11:11:41 +11:00
{
2017-09-08 17:11:35 +10:00
native_t id;
auto res = life_traits<T>::create (base.native (), std::forward<Args> (args)..., &id);
2016-02-24 11:11:41 +11:00
error::try_code (res);
return id;
}
};
2017-09-01 12:33:41 +10:00
/// a vulkan object that is obtained by listings from a parent object.
2016-02-24 11:11:41 +11:00
template <typename T>
struct enumerated : public object<T> {
using object<T>::object;
static std::vector<T>
find (const instance &parent);
};
2016-02-24 11:11:41 +11:00
2017-09-01 12:33:41 +10:00
/// 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.
2017-09-05 17:20:17 +10:00
template <typename SelfT, typename OwnerT>
struct owned : public object<SelfT> {
2017-09-08 17:11:35 +10:00
using native_t = typename object<SelfT>::native_t;
using owner_t = OwnerT;
2017-09-05 17:20:17 +10:00
using object<SelfT>::object;
2017-09-08 17:11:35 +10:00
///////////////////////////////////////////////////////////////////////
2017-09-05 17:20:17 +10:00
template <typename ...Args>
2017-09-08 17:11:35 +10:00
owned (OwnerT &owner, Args &&...args):
object<SelfT> (
error::try_query (
life_traits<SelfT>::create,
owner.native (),
std::forward<Args> (args)...
)
)
2017-09-05 17:20:17 +10:00
{ ; }
2017-09-08 17:11:35 +10:00
//---------------------------------------------------------------------
owned (owned &&rhs):
object<SelfT> (std::move (rhs))
{ ; }
//---------------------------------------------------------------------
~owned ()
{
2017-09-08 17:11:35 +10:00
CHECK_EQ (this->native (), VK_NULL_HANDLE);
}
2017-09-08 17:11:35 +10:00
//---------------------------------------------------------------------
void
2017-09-08 17:11:35 +10:00
destroy (OwnerT &owner)
{
2017-09-08 17:11:35 +10:00
life_traits<SelfT>::destroy (owner.native (), this->native (), nullptr);
this->m_native = VK_NULL_HANDLE;
}
2017-09-08 17:11:35 +10:00
};
2017-09-05 17:20:17 +10:00
private:
template <typename ...Args>
static
id_t make (OwnerT &owner, Args &&...args)
{
id_t id;
life_traits<SelfT>::get (owner.id (), std::forward<Args> (args)..., &id);
return id;
}
};
2017-09-08 17:11:35 +10:00
};
2016-02-24 11:11:41 +11:00
#endif