feat: add memory module
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
light7734 2025-09-22 18:53:35 +03:30
parent f465f152e3
commit 8063903344
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
5 changed files with 132 additions and 1 deletions

View file

@ -1,5 +1,6 @@
# engine
add_subdirectory(./base)
add_subdirectory(./memory)
add_subdirectory(./time)
add_subdirectory(./logger)
add_subdirectory(./debug)
@ -14,7 +15,7 @@ add_subdirectory(./input)
# add_subdirectory(./ui)
#
add_subdirectory(./surface)
# add_subdirectory(./renderer)
add_subdirectory(./renderer)
add_subdirectory(./ecs)
#
add_subdirectory(./app)

View file

@ -0,0 +1 @@
add_library_module(memory)

View file

@ -0,0 +1,73 @@
#pragma once
namespace lt::memory {
/** Holds an `Underlying_T`, assigns it to `null_value` when this object is moved.
*
* @note For avoiding the need to explicitly implement the move constructor for objects that hold
* Vulkan objects. But may server other purposes, hence why I kept the implementation generic.
*/
template<typename Underlying_T, Underlying_T null_value = nullptr>
class NullOnMove
{
public:
NullOnMove() = default;
NullOnMove(Underlying_T value): m_value(value)
{
}
~NullOnMove() = default;
NullOnMove(const NullOnMove &) = delete;
auto operator=(const NullOnMove &) -> NullOnMove & = delete;
NullOnMove(NullOnMove &&other) noexcept
{
*this = std::move(other);
}
auto operator=(NullOnMove &&other) noexcept -> NullOnMove &
{
if (this->m_value == other.m_value)
{
return *this;
}
m_value = other.m_value;
other.m_value = null_value;
return *this;
}
auto operator&() const -> const Underlying_T *
{
return &m_value;
}
auto operator&() -> Underlying_T *
{
return &m_value;
}
operator bool() const
{
return m_value != null_value;
}
operator Underlying_T() const
{
return m_value;
}
operator Underlying_T()
{
return m_value;
}
private:
Underlying_T m_value;
};
} // namespace lt::memory

View file

@ -0,0 +1,28 @@
#pragma once
#include <memory>
namespace lt::memory {
/** Wrapper around std::shared_ptr. */
template<typename t>
using Ref = std::shared_ptr<t>;
/** Allocates memory for an `Underlying_T` and directly constructs it there.
*
* @return A Ref<Underlying_T> to the constructed object.
*/
template<typename Underlying_T, typename... Args>
constexpr Ref<Underlying_T> create_ref(Args &&...args)
{
return std::make_shared<Underlying_T>(std::forward<Args>(args)...);
}
/** Converts c-style pointer of type `Underlying_T` to a `Ref<Underlying_T>`. */
template<typename Underlying_T>
constexpr Ref<Underlying_T> make_ref(Underlying_T *raw_pointer)
{
return Ref<Underlying_T>(raw_pointer);
}
} // namespace lt::memory

View file

@ -0,0 +1,28 @@
#pragma once
#include <memory>
namespace lt::memory {
/** Wrapper around std::unique_ptr. */
template<typename t>
using Scope = std::unique_ptr<t>;
/** Allocates memory for an `Underlying_T` and directly constructs it there.
*
* @return A Scope<Underlying_T> to the constructed object.
*/
template<typename Underlying_T, typename... Args>
constexpr Scope<Underlying_T> create_scope(Args &&...args)
{
return std::make_unique<Underlying_T>(std::forward<Args>(args)...);
}
/** Converts c-style pointer of type `Underlying_T` to a `Scope<Underlying_T>`. */
template<typename Underlying_T>
constexpr Scope<Underlying_T> make_scope(Underlying_T *raw_pointer)
{
return Scope<Underlying_T>(raw_pointer);
}
} // namespace lt::memory