wip: feat: lsd
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
light7734 2025-11-16 14:29:03 +03:30
parent e5467124e1
commit b7ae2f7048
19 changed files with 350 additions and 236 deletions

View file

@ -1,11 +1,25 @@
add_module(
NAME logger INTERFACES logger.cppm TESTS logger.test.cpp
NAME
lsd
INTERFACES
lsd.cppm
ref.cppm
vec.cppm
arr.cppm
str.cppm
set.cppm
hash.cppm
scope.cppm
bitwise.cppm
thread.cppm
primitives.cppm
src_location.cppm
)
add_module(NAME logger INTERFACES logger.cppm DEPENDENCIES lsd)
return()
add_module(NAME bitwise INTERFACES operations.cppm)
add_module(NAME env INTERFACES constants.cppm)
add_module(
NAME memory INTERFACES null_on_move.cppm reference.cppm scope.cppm
)
add_module(NAME memory INTERFACES null_on_move.cppm reference.cppm scope.cppm)
add_module(NAME time INTERFACES timer.cppm)
add_module(
@ -206,6 +220,8 @@ add_module(
mirror
ROOT_DIR
${CMAKE_CURRENT_SOURCE_DIR}/mirror
ENTRYPOINT
entrypoint.cpp
INTERFACES
system.cppm
DEPENDENCIES
@ -216,23 +232,6 @@ add_module(
surface
renderer
camera
# TESTS
# system.test.cpp
TESTS
system.test.cpp
)
add_executable(exectest ${CMAKE_CURRENT_SOURCE_DIR}/mirror/entrypoint.cpp
)
target_link_libraries(exectest PRIVATE
mirror
app
time
input
surface
renderer
camera
)
# add_executable_module(mirror entrypoint/mirror.cpp)
# target_link_libraries(mirror PRIVATE libmirror input)

View file

@ -1,12 +0,0 @@
export module bitwise;
import std;
namespace lt::bitwise {
/* bit-wise */
export constexpr auto bit(std::uint32_t x) -> std::uint32_t
{
return 1u << x;
}
} // namespace lt::bitwise

View file

@ -1,50 +1,51 @@
export module logger;
import std;
import lsd;
namespace lt::log {
/** Severity of a log message. */
enum class Level : std::uint8_t
auto thread_hash_id() noexcept -> u64
{
/** Lowest and most vebose log level, for tracing execution paths and events */
trace = 0,
/** Vebose log level, for enabling temporarily to debug */
debug = 1,
/** General information */
info = 2,
/** Things we should to be aware of and edge cases */
warn = 3,
/** Defects, bugs and undesired behaviour */
error = 4,
/** Unrecoverable errors */
critical = 5,
/** No logging */
off = 6,
};
namespace details {
inline auto thread_hash_id() noexcept -> std::uint64_t
{
return static_cast<std::uint64_t>(std::hash<std::thread::id> {}(std::this_thread::get_id()));
return static_cast<u64>(lsd::hash<lsd::thread_id> {}(lsd::this_thread_id()));
}
} // namespace details
} // namespace lt::log
export namespace lt::log {
/** Severity of a log message. */
enum class Level : u8
{
/** Lowest and most vebose log level, for tracing execution paths and events */
trace = 0u,
/** Vebose log level, for enabling temporarily to debug */
debug = 1u,
/** General information */
info = 2u,
/** Things we should to be aware of and edge cases */
warn = 3u,
/** Defects, bugs and undesired behaviour */
error = 4u,
/** Unrecoverable errors */
critical = 5u,
/** No logging */
off = 6u,
};
template<typename... Args>
struct [[maybe_unused]] print
{
[[maybe_unused]] print(
Level level,
const std::source_location &location,
std::format_string<Args...> format,
const lsd::src_location &location,
lsd::format_str<Args...> format,
Args &&...arguments
) noexcept
{
@ -78,103 +79,103 @@ struct [[maybe_unused]] print
};
template<typename... Args>
print(Level, const std::source_location &, std::format_string<Args...>, Args &&...) noexcept
print(Level, const lsd::src_location &, lsd::format_str<Args...>, Args &&...) noexcept
-> print<Args...>;
export template<typename... Args>
template<typename... Args>
struct [[maybe_unused]] trace
{
[[maybe_unused]] trace(
std::format_string<Args...> format,
lsd::format_str<Args...> format,
Args &&...arguments,
const std::source_location &location = std::source_location::current()
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
print(Level::trace, location, format, std::forward<Args>(arguments)...);
}
};
export template<typename... Args>
trace(std::format_string<Args...>, Args &&...) noexcept -> trace<Args...>;
template<typename... Args>
trace(lsd::format_str<Args...>, Args &&...) noexcept -> trace<Args...>;
export template<typename... Args>
template<typename... Args>
struct [[maybe_unused]] debug
{
[[maybe_unused]] debug(
std::format_string<Args...> format,
lsd::format_str<Args...> format,
Args &&...arguments,
const std::source_location &location = std::source_location::current()
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
print(Level::debug, location, format, std::forward<Args>(arguments)...);
}
};
export template<typename... Args>
debug(std::format_string<Args...>, Args &&...) noexcept -> debug<Args...>;
template<typename... Args>
debug(lsd::format_str<Args...>, Args &&...) noexcept -> debug<Args...>;
export template<typename... Args>
template<typename... Args>
struct [[maybe_unused]] info
{
[[maybe_unused]] info(
std::format_string<Args...> format,
lsd::format_str<Args...> format,
Args &&...arguments,
const std::source_location &location = std::source_location::current()
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
print(Level::info, location, format, std::forward<Args>(arguments)...);
}
};
export template<typename... Args>
info(std::format_string<Args...>, Args &&...) noexcept -> info<Args...>;
template<typename... Args>
info(lsd::format_str<Args...>, Args &&...) noexcept -> info<Args...>;
export template<typename... Args>
template<typename... Args>
struct [[maybe_unused]] warn
{
[[maybe_unused]] warn(
std::format_string<Args...> format,
lsd::format_str<Args...> format,
Args &&...arguments,
const std::source_location &location = std::source_location::current()
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
print(Level::warn, location, format, std::forward<Args>(arguments)...);
}
};
export template<typename... Args>
warn(std::format_string<Args...>, Args &&...) noexcept -> warn<Args...>;
template<typename... Args>
warn(lsd::format_str<Args...>, Args &&...) noexcept -> warn<Args...>;
export template<typename... Args>
template<typename... Args>
struct [[maybe_unused]] error
{
[[maybe_unused]] error(
std::format_string<Args...> format,
lsd::format_str<Args...> format,
Args &&...arguments,
const std::source_location &location = std::source_location::current()
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
print(Level::error, location, format, std::forward<Args>(arguments)...);
}
};
export template<typename... Args>
error(std::format_string<Args...>, Args &&...) noexcept -> error<Args...>;
template<typename... Args>
error(lsd::format_str<Args...>, Args &&...) noexcept -> error<Args...>;
export template<typename... Args>
template<typename... Args>
struct [[maybe_unused]] critical
{
[[maybe_unused]] critical(
std::format_string<Args...> format,
lsd::format_str<Args...> format,
Args &&...arguments,
const std::source_location &location = std::source_location::current()
const lsd::src_location &location = lsd::src_location::current()
) noexcept
{
print(Level::critical, location, format, std::forward<Args>(arguments)...);
}
};
export template<typename... Args>
critical(std::format_string<Args...>, Args &&...) noexcept -> critical<Args...>;
template<typename... Args>
critical(lsd::format_str<Args...>, Args &&...) noexcept -> critical<Args...>;
} // namespace lt::log

9
modules/lsd/arr.cppm Normal file
View file

@ -0,0 +1,9 @@
export module lsd.arr;
import std;
export namespace lt::lsd {
template<typename T, size_t size>
using arr = std::array<T, size>;
}

11
modules/lsd/bitwise.cppm Normal file
View file

@ -0,0 +1,11 @@
export module lsd.bitwise;
export namespace lt::lsd {
/* bit-wise */
constexpr auto bit(auto x)
{
return 1u << x;
}
} // namespace lt::lsd

8
modules/lsd/hash.cppm Normal file
View file

@ -0,0 +1,8 @@
export module lsd.hash;
import std;
export namespace lt::lsd {
template<typename T>
using hash = std::hash<T>;
}

13
modules/lsd/lsd.cppm Normal file
View file

@ -0,0 +1,13 @@
export module lsd;
export import lsd.set;
export import lsd.vec;
export import lsd.arr;
export import lsd.str;
export import lsd.hash;
export import lsd.hash;
export import lsd.thread;
export import lsd.ref_ptr;
export import lsd.bitwise;
export import lsd.scope_ptr;
export import lsd.primitives;
export import lsd.src_location;

9
modules/lsd/map.cppm Normal file
View file

@ -0,0 +1,9 @@
export module lsd.bitwise;
import std;
export namespace lt::lsd {
template<typename T>
using set = std::set<T>;
}

View file

@ -0,0 +1,21 @@
export module lsd.primitives;
import std;
/** THIS IS AND SHALL BE THE ONLY NAMESPACE FROM LT
THAT HAS NO SUB-NAMESPACE IDENTIFIER. */
export namespace lt {
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using i8 = std::int8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using f32 = float;
using f64 = double;
} // namespace lt

View file

@ -1,32 +1,32 @@
export module memory.reference;
export module lsd.ref_ptr;
import std;
namespace lt::memory {
export namespace lt::lsd {
/** Wrapper around std::shared_ptr.
*
* @note Currently just an alias, might turn into an implementation later.
* @ref https://en.cppreference.com/w/cpp/memory/shared_ptr.html
*/
export template<typename T>
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.
*/
export template<typename Underlying_T, typename... Args>
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>`. */
export template<typename 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
} // namespace lt::lsd

View file

@ -1,32 +1,31 @@
export module memory.scope;
export module lsd.scope_ptr;
import std;
namespace lt::memory {
export namespace lt::lsd {
/** @brief Wrapper around std::unique_ptr.
/** Wrapper around std::unique_ptr.
*
* @note Currently just an alias, might turn into an implementation later.
* @ref https://en.cppreference.com/w/cpp/memory/unique_ptr.html
*/
export template<typename t>
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.
*/
export template<typename Underlying_T, typename... Args>
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>`. */
export template<typename 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
} // namespace lt::lsd

9
modules/lsd/set.cppm Normal file
View file

@ -0,0 +1,9 @@
export module lsd.set;
import std;
namespace lt::lsd {
template<typename T>
using set = std::set<T>;
}

View file

@ -0,0 +1,8 @@
export module lsd.src_location;
import std;
export namespace lt::lsd {
using src_location = std::source_location;
}

16
modules/lsd/str.cppm Normal file
View file

@ -0,0 +1,16 @@
export module lsd.str;
import std;
export namespace lt::lsd {
using c_str = const char *;
using str = std::string;
using str_view = std::string_view;
template<typename... T>
using format_str = std::format_string<T...>;
} // namespace lt::lsd

13
modules/lsd/thread.cppm Normal file
View file

@ -0,0 +1,13 @@
export module lsd.thread;
import std;
export namespace lt::lsd {
using thread_id = std::thread::id;
[[nodiscard]] auto this_thread_id() -> thread_id
{
return std::this_thread::get_id();
}
} // namespace lt::sd

9
modules/lsd/vec.cppm Normal file
View file

@ -0,0 +1,9 @@
export module lsd.vec;
import std;
export namespace lt::lsd {
template<typename T>
using vec = std::vector<T>;
}

View file

@ -1,101 +0,0 @@
export module memory.null_on_move;
import std;
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 handles. But may serve other purposes, hence why I kept the implementation generic.
*/
export 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 == std::addressof(other))
{
return *this;
}
m_value = other.m_value;
other.m_value = null_value;
return *this;
}
auto operator->() -> Underlying_T
{
return m_value;
}
// NOLINTNEXTLINE
auto operator->() const -> const Underlying_T
{
return m_value;
}
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;
}
operator std::uint64_t() const
{
return (std::uint64_t)m_value;
}
[[nodiscard]] auto get() -> Underlying_T &
{
return m_value;
}
[[nodiscard]] auto get() const -> const Underlying_T &
{
return m_value;
}
private:
Underlying_T m_value;
};
} // namespace lt::memory

View file

@ -50,6 +50,96 @@ struct overloads: Ts...
using Ts::operator()...;
};
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 == std::addressof(other))
{
return *this;
}
m_value = other.m_value;
other.m_value = null_value;
return *this;
}
auto operator->() -> Underlying_T
{
return m_value;
}
// NOLINTNEXTLINE
auto operator->() const -> const Underlying_T
{
return m_value;
}
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;
}
operator std::uint64_t() const
{
return (std::uint64_t)m_value;
}
[[nodiscard]] auto get() -> Underlying_T &
{
return m_value;
}
[[nodiscard]] auto get() const -> const Underlying_T &
{
return m_value;
}
private:
Underlying_T m_value;
};
export namespace lt::renderer::vk {
using Bool32 = VkBool32;
@ -750,7 +840,7 @@ private:
return m_instance;
}
memory::NullOnMove<VkInstance> m_instance {};
NullOnMove<VkInstance> m_instance {};
};
class Surface
@ -842,7 +932,7 @@ private:
return m_surface.get();
}
memory::NullOnMove<VkSurfaceKHR> m_surface {};
NullOnMove<VkSurfaceKHR> m_surface {};
VkInstance m_instance {};
};
@ -1329,7 +1419,7 @@ private:
return m_device.get();
}
memory::NullOnMove<VkDevice> m_device {};
NullOnMove<VkDevice> m_device {};
};
class Semaphore
@ -1366,7 +1456,7 @@ private:
return &m_semaphore;
}
memory::NullOnMove<VkDevice> m_device;
NullOnMove<VkDevice> m_device;
VkSemaphore m_semaphore;
};
@ -1417,7 +1507,7 @@ private:
return &m_fence;
}
memory::NullOnMove<VkDevice> m_device;
NullOnMove<VkDevice> m_device;
VkFence m_fence;
};
@ -1496,7 +1586,7 @@ private:
return m_buffer;
}
memory::NullOnMove<VkDevice> m_device {};
NullOnMove<VkDevice> m_device {};
VkBuffer m_buffer {};
};
@ -1751,7 +1841,7 @@ private:
return m_shader_module;
}
memory::NullOnMove<VkDevice> m_device {};
NullOnMove<VkDevice> m_device {};
VkShaderModule m_shader_module {};
};
@ -1854,7 +1944,7 @@ public:
}
private:
memory::NullOnMove<VkDevice> m_device;
NullOnMove<VkDevice> m_device;
VkDescriptorSetLayout m_layout;
};
@ -1984,7 +2074,7 @@ public:
~Pipeline();
private:
memory::NullOnMove<VkDevice> m_device {};
NullOnMove<VkDevice> m_device {};
VkPipeline m_pipeline {};
};
@ -2036,7 +2126,7 @@ private:
return m_layout;
}
memory::NullOnMove<VkDevice> m_device {};
NullOnMove<VkDevice> m_device {};
VkPipelineLayout m_layout {};
};
@ -2432,7 +2522,7 @@ private:
return m_queue;
}
memory::NullOnMove<VkDevice> m_device;
NullOnMove<VkDevice> m_device;
VkQueue m_queue;
};
@ -2486,7 +2576,7 @@ private:
return m_memory;
}
memory::NullOnMove<VkDevice> m_device {};
NullOnMove<VkDevice> m_device {};
VkDeviceMemory m_memory {};
};
@ -2540,7 +2630,7 @@ public:
auto operator=(const Messenger &) -> Messenger & = delete;
private:
memory::NullOnMove<VkInstance> m_instance {};
NullOnMove<VkInstance> m_instance {};
VkDebugUtilsMessengerEXT m_messenger {};
};

View file

@ -19,14 +19,22 @@ function(add_module)
set(module_directory "${ARGS_ROOT_DIR}")
endif()
# In this case, the module is an executable, so we prepend "lib" to the target name.
# And set the "executable_target" name to ARGS_NAME.
# The rationale here is to easily be able to write tests for an executable modules's interfaces...
# by splitting it into two targets: lib"executable_name" for the interface and "executable_name" for the "int main()" defining file (the entrypoint).
# the lib"executable_name" should not be disruptive since an executable module's library will not be dependent upon (except by the tests within the same module)
# In this case, the module is an executable, so we prepend "lib" to the
# target name. And set the "executable_target" name to ARGS_NAME.
#
# The rationale here is to easily be able to write tests for an executable
# modules's interfaces... by splitting it into two targets:
# lib"executable_name" for the interface and "executable_name" for the "int
# main()" defining file (the entrypoint).
# The lib"executable_name" should not be disruptive since an executable
# module's library will not be dependent upon (except by the tests within
# the same module)
if(ARGS_ENTRYPOINT)
set(target_library_name "lib_${ARGS_NAME}")
add_executable(${target_executable_name} ${module_directory}/${ARGS_ENTRYPOINT})
add_executable(
${target_executable_name} ${module_directory}/${ARGS_ENTRYPOINT}
)
endif()
add_library(${target_library_name})
@ -53,13 +61,15 @@ function(add_module)
list(APPEND files "${module_directory}/${file}")
endforeach()
target_sources(
${target_library_name} PUBLIC FILE_SET public_cxx_modules TYPE CXX_MODULES
FILES ${files}
${target_library_name} PUBLIC FILE_SET public_cxx_modules TYPE
CXX_MODULES FILES ${files}
)
endif()
target_link_libraries(${target_library_name} PUBLIC ${ARGS_DEPENDENCIES})
target_link_libraries(${target_library_name} PRIVATE ${ARGS_PRIVATE_DEPENDENCIES})
target_link_libraries(
${target_library_name} PRIVATE ${ARGS_PRIVATE_DEPENDENCIES}
)
if(ARGS_TESTS)
message("ADDING TESTS!!!")
@ -78,7 +88,9 @@ function(add_module)
endif()
if(ARGS_ENTRYPOINT)
target_link_libraries(${target_executable_name} PRIVATE ${target_library_name})
target_link_libraries(
${target_executable_name} PRIVATE ${target_library_name}
)
endif()
endfunction()