Refactor to add move ops.

This commit is contained in:
Anish Bhobe 2024-07-11 09:11:15 +02:00
parent 52b3c671c6
commit ef6ca6a79e
10 changed files with 120 additions and 7 deletions

View File

@ -122,3 +122,19 @@ Context::~Context()
glfwTerminate(); glfwTerminate();
} }
Context::Context(Context &&other) noexcept
: m_Instance(Take(other.m_Instance))
, m_DebugMessenger(Take(other.m_DebugMessenger))
{
}
Context &
Context::operator=(Context &&other) noexcept
{
if (this == &other)
return *this;
m_Instance = Take(other.m_Instance);
m_DebugMessenger = Take(other.m_DebugMessenger);
return *this;
}

View File

@ -23,4 +23,10 @@ struct Context final
// Ctor/Dtor // Ctor/Dtor
Context(cstr appName, Version version, bool enableValidation = true); Context(cstr appName, Version version, bool enableValidation = true);
~Context(); ~Context();
// Move
Context(Context &&other) noexcept;
Context &operator=(Context &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(Context);
}; };

View File

@ -108,4 +108,24 @@ Device::GetQueue(const u32 familyIndex, const u32 queueIndex) const
vk::Queue queue; vk::Queue queue;
m_Device.getQueue(familyIndex, queueIndex, &queue); m_Device.getQueue(familyIndex, queueIndex, &queue);
return queue; return queue;
}
Device::Device(Device &&other) noexcept
: m_Name(std::move(other.m_Name))
, m_PhysicalDevice(Take(other.m_PhysicalDevice))
, m_Device(Take(other.m_Device))
, m_Allocator(Take(other.m_Allocator))
{
}
Device &
Device::operator=(Device &&other) noexcept
{
if (this == &other)
return *this;
m_Name = std::move(other.m_Name);
m_PhysicalDevice = Take(other.m_PhysicalDevice);
m_Device = Take(other.m_Device);
m_Allocator = Take(other.m_Allocator);
return *this;
} }

View File

@ -28,13 +28,20 @@ struct Device final
vk::Device m_Device = nullptr; vk::Device m_Device = nullptr;
VmaAllocator m_Allocator = nullptr; VmaAllocator m_Allocator = nullptr;
template <typename T>
requires vk::isVulkanHandleType<T>::value void SetName(const T &object, cstr name) const;
[[nodiscard]] vk::Queue GetQueue(u32 familyIndex, u32 queueIndex) const;
// Ctor/Dtor
Device(const Context *context, PhysicalDevice *physicalDevice, Features *enabledFeatures, Device(const Context *context, PhysicalDevice *physicalDevice, Features *enabledFeatures,
const eastl::vector<QueueAllocation> &queueAllocations, NameString &&name); const eastl::vector<QueueAllocation> &queueAllocations, NameString &&name);
~Device(); ~Device();
template <typename T> // Move
requires vk::isVulkanHandleType<T>::value void SetName(const T &object, cstr name) const; Device(Device &&other) noexcept;
[[nodiscard]] vk::Queue GetQueue(u32 familyIndex, u32 queueIndex) const; Device &operator=(Device &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(Device);
}; };
template <typename T> template <typename T>

View File

@ -34,7 +34,11 @@ constexpr u32 ASTER_API_VERSION = VK_API_VERSION_1_3;
#define CODE_LOC " @ " __FILE__ ":" VULKAN_HPP_STRINGIFY(__LINE__) #define CODE_LOC " @ " __FILE__ ":" VULKAN_HPP_STRINGIFY(__LINE__)
#define FORCE_TODO static_assert(false) #define DISALLOW_COPY_AND_ASSIGN(CLASS_NAME) \
CLASS_NAME(const CLASS_NAME &other) = delete; \
CLASS_NAME &operator=(const CLASS_NAME &other) = delete
#define Take(ELEMENT) eastl::exchange(ELEMENT, {})
[[nodiscard]] inline bool [[nodiscard]] inline bool
Failed(const vk::Result result) Failed(const vk::Result result)

View File

@ -37,7 +37,7 @@ GetSurfaceFormats(vk::PhysicalDevice physicalDevice, vk::SurfaceKHR surface);
[[nodiscard]] eastl::vector<vk::PresentModeKHR> [[nodiscard]] eastl::vector<vk::PresentModeKHR>
GetSurfacePresentModes(vk::PhysicalDevice physicalDevice, vk::SurfaceKHR surface); GetSurfacePresentModes(vk::PhysicalDevice physicalDevice, vk::SurfaceKHR surface);
struct PhysicalDevice struct PhysicalDevice final
{ {
vk::PhysicalDevice m_PhysicalDevice; vk::PhysicalDevice m_PhysicalDevice;
vk::PhysicalDeviceProperties m_DeviceProperties; vk::PhysicalDeviceProperties m_DeviceProperties;

View File

@ -21,6 +21,32 @@ Swapchain::~Swapchain()
this->Cleanup(); this->Cleanup();
} }
Swapchain::Swapchain(Swapchain &&other) noexcept
: m_Device(other.m_Device)
, m_Swapchain(Take(other.m_Swapchain))
, m_Name(std::move(other.m_Name))
, m_Extent(other.m_Extent)
, m_Format(other.m_Format)
, m_Images(std::move(other.m_Images))
, m_ImageViews(std::move(other.m_ImageViews))
{
}
Swapchain &
Swapchain::operator=(Swapchain &&other) noexcept
{
if (this == &other)
return *this;
m_Device = other.m_Device;
m_Swapchain = Take(other.m_Swapchain);
m_Name = std::move(other.m_Name);
m_Extent = other.m_Extent;
m_Format = other.m_Format;
m_Images = std::move(other.m_Images);
m_ImageViews = std::move(other.m_ImageViews);
return *this;
}
void void
Swapchain::Create(const Window *window) Swapchain::Create(const Window *window)
{ {

View File

@ -23,9 +23,17 @@ struct Swapchain final
eastl::fixed_vector<vk::Image, 4> m_Images; eastl::fixed_vector<vk::Image, 4> m_Images;
eastl::fixed_vector<vk::ImageView, 4> m_ImageViews; eastl::fixed_vector<vk::ImageView, 4> m_ImageViews;
void Create(const Window *window);
// Ctor/Dtor
Swapchain(const Window *window, const Device *device, NameString &&name); Swapchain(const Window *window, const Device *device, NameString &&name);
~Swapchain(); ~Swapchain();
void Create(const Window *window);
// Move
Swapchain(Swapchain &&other) noexcept;
Swapchain &operator=(Swapchain &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(Swapchain);
private: private:
void Cleanup(); void Cleanup();

View File

@ -79,7 +79,7 @@ Window::~Window()
DEBUG("Surface Destroyed"); DEBUG("Surface Destroyed");
} }
if (m_Window != nullptr) if (m_Window)
{ {
glfwDestroyWindow(m_Window); glfwDestroyWindow(m_Window);
m_Window = nullptr; m_Window = nullptr;
@ -87,3 +87,23 @@ Window::~Window()
DEBUG("Window '{}' Destroyed", m_Name); DEBUG("Window '{}' Destroyed", m_Name);
} }
Window::Window(Window &&other) noexcept
: m_Context(other.m_Context)
, m_Window(Take(other.m_Window))
, m_Surface(Take(other.m_Surface))
, m_Name(Take(other.m_Name))
{
}
Window &
Window::operator=(Window &&other) noexcept
{
if (this == &other)
return *this;
m_Context = other.m_Context;
m_Window = Take(other.m_Window);
m_Surface = Take(other.m_Surface);
m_Name = Take(other.m_Name);
return *this;
}

View File

@ -34,4 +34,10 @@ struct Window final
// Ctor/Dtor // Ctor/Dtor
Window(cstr title, Context *context, vk::Extent2D extent, b8 isFullScreen = false); Window(cstr title, Context *context, vk::Extent2D extent, b8 isFullScreen = false);
~Window(); ~Window();
// Move
Window(Window &&other) noexcept;
Window &operator=(Window &&other) noexcept;
DISALLOW_COPY_AND_ASSIGN(Window);
}; };