139 lines
3.7 KiB
C++
139 lines
3.7 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 T>
|
|
struct object {
|
|
using id_t = typename id_traits<T>::id_t;
|
|
|
|
object (id_t);
|
|
object (object&&);
|
|
object (const object&) = delete;
|
|
|
|
const id_t& id (void) const&;
|
|
id_t& id (void) &;
|
|
|
|
private:
|
|
id_t m_id;
|
|
};
|
|
|
|
|
|
template <typename SelfT>
|
|
struct root : public object<SelfT> {
|
|
using id_t = typename object<SelfT>::id_t;
|
|
|
|
template <typename ...Args>
|
|
root (Args &&...args):
|
|
object<SelfT> (make (std::forward<Args> (args)...))
|
|
{ ; }
|
|
|
|
~root ()
|
|
{
|
|
life_traits<SelfT>::destroy (this->id (), nullptr);
|
|
}
|
|
|
|
private:
|
|
template <typename ...Args>
|
|
static id_t
|
|
make (Args &&...args)
|
|
{
|
|
id_t id;
|
|
auto res = life_traits<SelfT>::create (std::forward<Args> (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 <typename T>
|
|
struct descendant : public object<T> {
|
|
using id_t = typename object<T>::id_t;
|
|
|
|
template <typename ...Args>
|
|
descendant (Args &&...args):
|
|
object<T> (make (std::forward<Args> (args)...))
|
|
{ ; }
|
|
|
|
~descendant ()
|
|
{
|
|
life_traits<T>::destroy (this->id (), nullptr);
|
|
}
|
|
|
|
private:
|
|
template <typename Base, typename ...Args>
|
|
static
|
|
id_t make (Base &&base, Args &&...args)
|
|
{
|
|
id_t id;
|
|
auto res = life_traits<T>::create (base.id (), std::forward<Args> (args)..., &id);
|
|
error::try_code (res);
|
|
return id;
|
|
}
|
|
};
|
|
|
|
|
|
/// a vulkan object that is obtained by listings from a parent object.
|
|
template <typename T>
|
|
struct enumerated : public object<T> {
|
|
using object<T>::object;
|
|
|
|
static std::vector<T> find (const instance&);
|
|
};
|
|
|
|
|
|
/// 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 id_t = typename object<SelfT>::id_t;
|
|
using object<SelfT>::object;
|
|
|
|
template <typename ...Args>
|
|
owned (Args &&...args):
|
|
object<SelfT> (make (std::forward<Args> (args)...))
|
|
{ ; }
|
|
|
|
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;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif
|