217 lines
6.1 KiB
C++
217 lines
6.1 KiB
C++
/*
|
|
* 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 <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#ifndef CRUFT_VK_OBJECT_HPP
|
|
#define CRUFT_VK_OBJECT_HPP
|
|
|
|
#include "./traits.hpp"
|
|
#include "./except.hpp"
|
|
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace cruft::vk {
|
|
/// the base class for all non-trivial vulkan objects requiring lifetime
|
|
/// management.
|
|
template <typename SelfT>
|
|
struct object {
|
|
using native_type = native_t<SelfT>;
|
|
|
|
object (const native_t<SelfT> &_native):
|
|
m_native (_native)
|
|
{
|
|
CHECK_NEQ (m_native, VK_NULL_HANDLE);
|
|
}
|
|
|
|
object (object &&rhs):
|
|
m_native (VK_NULL_HANDLE)
|
|
{ std::swap (m_native, rhs.m_native); }
|
|
|
|
object (const object&) = delete;
|
|
|
|
const auto& native (void) const& { return m_native; }
|
|
auto& native (void)& { return m_native; }
|
|
|
|
protected:
|
|
native_t<SelfT> m_native;
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
template <typename SelfT>
|
|
struct root : public object<SelfT> {
|
|
template <typename ...Args>
|
|
root (Args &&...args):
|
|
object<SelfT> (
|
|
cruft::vk::error::try_query (
|
|
wrapper_traits<SelfT>::create,
|
|
std::forward<Args> (args)...
|
|
)
|
|
)
|
|
{ ; }
|
|
|
|
~root ()
|
|
{
|
|
wrapper_traits<SelfT>::destroy (this->native (), nullptr);
|
|
}
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
/// a vulkan object that must be directly instantiated through
|
|
/// constructor arguments, rather than being enumerated and owned by
|
|
/// another object.
|
|
template <typename SelfT>
|
|
struct descendant : public object<SelfT> {
|
|
template <typename ParentT, typename ...Args>
|
|
descendant (ParentT &parent, Args &&...args):
|
|
object<SelfT> (
|
|
cruft::vk::error::try_query(
|
|
wrapper_traits<SelfT>::create,
|
|
parent.native (),
|
|
std::forward<Args> (args)...
|
|
)
|
|
)
|
|
{ ; }
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
~descendant ()
|
|
{
|
|
wrapper_traits<SelfT>::destroy (this->native (), nullptr);
|
|
}
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
/// a vulkan object that is obtained by listings from a parent object.
|
|
template <typename SelfT, typename ParentT>
|
|
struct enumerated : public object<SelfT> {
|
|
using object<SelfT>::object;
|
|
|
|
static std::vector<SelfT>
|
|
find (const ParentT &parent)
|
|
{
|
|
return error::try_handles<SelfT> (
|
|
enum_traits<native_t<SelfT>>::enumerate, parent.native ()
|
|
);
|
|
}
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
/// 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 <typename SelfT, typename OwnerT>
|
|
struct owned : public object<SelfT> {
|
|
using owner_t = OwnerT;
|
|
|
|
using object<SelfT>::object;
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
template <typename ...Args>
|
|
owned (OwnerT &owner, Args &&...args):
|
|
object<SelfT> (
|
|
error::try_query (
|
|
wrapper_traits<SelfT>::create,
|
|
owner.native (),
|
|
std::forward<Args> (args)...
|
|
)
|
|
)
|
|
{ ; }
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
owned (owned &&rhs):
|
|
object<SelfT> (std::move (rhs))
|
|
{ ; }
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
~owned ()
|
|
{
|
|
CHECK_EQ (this->native (), VK_NULL_HANDLE);
|
|
}
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
void
|
|
destroy (OwnerT &owner)
|
|
{
|
|
wrapper_traits<SelfT>::destroy (owner.native (), this->native (), nullptr);
|
|
this->m_native = VK_NULL_HANDLE;
|
|
}
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
template <typename SelfT>
|
|
class owned_ptr {
|
|
public:
|
|
owned_ptr (SelfT &&_self, owner_t<SelfT> &_owner):
|
|
m_self (std::move (_self)),
|
|
m_owner (_owner)
|
|
{ ; }
|
|
|
|
owned_ptr (owned_ptr &&rhs):
|
|
m_self (std::move (rhs.m_self)),
|
|
m_owner (rhs.m_owner)
|
|
{ ; }
|
|
|
|
~owned_ptr ()
|
|
{
|
|
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.
|
|
owned_ptr& operator= (owned_ptr &&rhs) = delete;
|
|
|
|
const SelfT& get (void) const& { return m_self; }
|
|
SelfT& get (void)& { return m_self; }
|
|
|
|
SelfT& operator* ()&
|
|
{ return m_self; }
|
|
|
|
SelfT*
|
|
operator-> ()&
|
|
{ return &m_self; }
|
|
|
|
private:
|
|
SelfT m_self;
|
|
owner_t<SelfT> &m_owner;
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
template <typename SelfT, typename OwnerT, typename ...Args>
|
|
auto
|
|
make_owned (OwnerT &owner, Args &&...args)
|
|
{
|
|
return owned_ptr<SelfT> {
|
|
SelfT { owner, std::forward<Args> (args)... },
|
|
owner
|
|
};
|
|
};
|
|
};
|
|
|
|
#endif
|