diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 9ece596..db1762e 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -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) diff --git a/modules/bitwise/operations.cppm b/modules/bitwise/operations.cppm deleted file mode 100644 index dd66a76..0000000 --- a/modules/bitwise/operations.cppm +++ /dev/null @@ -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 diff --git a/modules/logger/logger.cppm b/modules/logger/logger.cppm index 4e39d8c..9a419cc 100644 --- a/modules/logger/logger.cppm +++ b/modules/logger/logger.cppm @@ -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::hash {}(std::this_thread::get_id())); + return static_cast(lsd::hash {}(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 struct [[maybe_unused]] print { [[maybe_unused]] print( Level level, - const std::source_location &location, - std::format_string format, + const lsd::src_location &location, + lsd::format_str format, Args &&...arguments ) noexcept { @@ -78,103 +79,103 @@ struct [[maybe_unused]] print }; template -print(Level, const std::source_location &, std::format_string, Args &&...) noexcept +print(Level, const lsd::src_location &, lsd::format_str, Args &&...) noexcept -> print; -export template +template struct [[maybe_unused]] trace { [[maybe_unused]] trace( - std::format_string format, + lsd::format_str 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(arguments)...); } }; -export template -trace(std::format_string, Args &&...) noexcept -> trace; +template +trace(lsd::format_str, Args &&...) noexcept -> trace; -export template +template struct [[maybe_unused]] debug { [[maybe_unused]] debug( - std::format_string format, + lsd::format_str 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(arguments)...); } }; -export template -debug(std::format_string, Args &&...) noexcept -> debug; +template +debug(lsd::format_str, Args &&...) noexcept -> debug; -export template +template struct [[maybe_unused]] info { [[maybe_unused]] info( - std::format_string format, + lsd::format_str 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(arguments)...); } }; -export template -info(std::format_string, Args &&...) noexcept -> info; +template +info(lsd::format_str, Args &&...) noexcept -> info; -export template +template struct [[maybe_unused]] warn { [[maybe_unused]] warn( - std::format_string format, + lsd::format_str 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(arguments)...); } }; -export template -warn(std::format_string, Args &&...) noexcept -> warn; +template +warn(lsd::format_str, Args &&...) noexcept -> warn; -export template +template struct [[maybe_unused]] error { [[maybe_unused]] error( - std::format_string format, + lsd::format_str 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(arguments)...); } }; -export template -error(std::format_string, Args &&...) noexcept -> error; +template +error(lsd::format_str, Args &&...) noexcept -> error; -export template +template struct [[maybe_unused]] critical { [[maybe_unused]] critical( - std::format_string format, + lsd::format_str 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(arguments)...); } }; -export template -critical(std::format_string, Args &&...) noexcept -> critical; +template +critical(lsd::format_str, Args &&...) noexcept -> critical; } // namespace lt::log diff --git a/modules/lsd/arr.cppm b/modules/lsd/arr.cppm new file mode 100644 index 0000000..769ee02 --- /dev/null +++ b/modules/lsd/arr.cppm @@ -0,0 +1,9 @@ +export module lsd.arr; +import std; + +export namespace lt::lsd { + +template +using arr = std::array; + +} diff --git a/modules/lsd/bitwise.cppm b/modules/lsd/bitwise.cppm new file mode 100644 index 0000000..66cf2d7 --- /dev/null +++ b/modules/lsd/bitwise.cppm @@ -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 diff --git a/modules/lsd/hash.cppm b/modules/lsd/hash.cppm new file mode 100644 index 0000000..592b4bc --- /dev/null +++ b/modules/lsd/hash.cppm @@ -0,0 +1,8 @@ +export module lsd.hash; +import std; + +export namespace lt::lsd { + +template +using hash = std::hash; +} diff --git a/modules/lsd/lsd.cppm b/modules/lsd/lsd.cppm new file mode 100644 index 0000000..0d58e1b --- /dev/null +++ b/modules/lsd/lsd.cppm @@ -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; diff --git a/modules/lsd/map.cppm b/modules/lsd/map.cppm new file mode 100644 index 0000000..3fd1da6 --- /dev/null +++ b/modules/lsd/map.cppm @@ -0,0 +1,9 @@ +export module lsd.bitwise; +import std; + +export namespace lt::lsd { + +template +using set = std::set; + +} diff --git a/modules/lsd/primitives.cppm b/modules/lsd/primitives.cppm new file mode 100644 index 0000000..f7377c8 --- /dev/null +++ b/modules/lsd/primitives.cppm @@ -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 diff --git a/modules/memory/reference.cppm b/modules/lsd/ref.cppm similarity index 77% rename from modules/memory/reference.cppm rename to modules/lsd/ref.cppm index 2b41e70..603d268 100644 --- a/modules/memory/reference.cppm +++ b/modules/lsd/ref.cppm @@ -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 +template using Ref = std::shared_ptr; /** Allocates memory for an `Underlying_T` and directly constructs it there. * * @return A Ref to the constructed object. */ -export template +template constexpr Ref create_ref(Args &&...args) { return std::make_shared(std::forward(args)...); } /** Converts c-style pointer of type `Underlying_T` to a `Ref`. */ -export template +template constexpr Ref make_ref(Underlying_T *raw_pointer) { return Ref(raw_pointer); } -} // namespace lt::memory +} // namespace lt::lsd diff --git a/modules/memory/scope.cppm b/modules/lsd/scope.cppm similarity index 73% rename from modules/memory/scope.cppm rename to modules/lsd/scope.cppm index 2a22635..b3d1d31 100644 --- a/modules/memory/scope.cppm +++ b/modules/lsd/scope.cppm @@ -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 +template using Scope = std::unique_ptr; /** Allocates memory for an `Underlying_T` and directly constructs it there. * * @return A Scope to the constructed object. */ -export template +template constexpr Scope create_scope(Args &&...args) { return std::make_unique(std::forward(args)...); } /** Converts c-style pointer of type `Underlying_T` to a `Scope`. */ -export template +template constexpr Scope make_scope(Underlying_T *raw_pointer) { return Scope(raw_pointer); } -} // namespace lt::memory +} // namespace lt::lsd diff --git a/modules/lsd/set.cppm b/modules/lsd/set.cppm new file mode 100644 index 0000000..11a1415 --- /dev/null +++ b/modules/lsd/set.cppm @@ -0,0 +1,9 @@ +export module lsd.set; +import std; + +namespace lt::lsd { + +template +using set = std::set; + +} diff --git a/modules/lsd/src_location.cppm b/modules/lsd/src_location.cppm new file mode 100644 index 0000000..181e53d --- /dev/null +++ b/modules/lsd/src_location.cppm @@ -0,0 +1,8 @@ +export module lsd.src_location; +import std; + +export namespace lt::lsd { + +using src_location = std::source_location; + +} diff --git a/modules/lsd/str.cppm b/modules/lsd/str.cppm new file mode 100644 index 0000000..add1fe5 --- /dev/null +++ b/modules/lsd/str.cppm @@ -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 +using format_str = std::format_string; + +} // namespace lt::lsd diff --git a/modules/lsd/thread.cppm b/modules/lsd/thread.cppm new file mode 100644 index 0000000..fb75024 --- /dev/null +++ b/modules/lsd/thread.cppm @@ -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 diff --git a/modules/lsd/vec.cppm b/modules/lsd/vec.cppm new file mode 100644 index 0000000..d38803c --- /dev/null +++ b/modules/lsd/vec.cppm @@ -0,0 +1,9 @@ +export module lsd.vec; +import std; + +export namespace lt::lsd { + +template +using vec = std::vector; + +} diff --git a/modules/memory/null_on_move.cppm b/modules/memory/null_on_move.cppm deleted file mode 100644 index 4ba612f..0000000 --- a/modules/memory/null_on_move.cppm +++ /dev/null @@ -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 -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 diff --git a/modules/renderer/vk/api_wrapper.cppm b/modules/renderer/vk/api_wrapper.cppm index 1e3db76..b958263 100644 --- a/modules/renderer/vk/api_wrapper.cppm +++ b/modules/renderer/vk/api_wrapper.cppm @@ -50,6 +50,96 @@ struct overloads: Ts... using Ts::operator()...; }; +template +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 m_instance {}; + NullOnMove m_instance {}; }; class Surface @@ -842,7 +932,7 @@ private: return m_surface.get(); } - memory::NullOnMove m_surface {}; + NullOnMove m_surface {}; VkInstance m_instance {}; }; @@ -1329,7 +1419,7 @@ private: return m_device.get(); } - memory::NullOnMove m_device {}; + NullOnMove m_device {}; }; class Semaphore @@ -1366,7 +1456,7 @@ private: return &m_semaphore; } - memory::NullOnMove m_device; + NullOnMove m_device; VkSemaphore m_semaphore; }; @@ -1417,7 +1507,7 @@ private: return &m_fence; } - memory::NullOnMove m_device; + NullOnMove m_device; VkFence m_fence; }; @@ -1496,7 +1586,7 @@ private: return m_buffer; } - memory::NullOnMove m_device {}; + NullOnMove m_device {}; VkBuffer m_buffer {}; }; @@ -1751,7 +1841,7 @@ private: return m_shader_module; } - memory::NullOnMove m_device {}; + NullOnMove m_device {}; VkShaderModule m_shader_module {}; }; @@ -1854,7 +1944,7 @@ public: } private: - memory::NullOnMove m_device; + NullOnMove m_device; VkDescriptorSetLayout m_layout; }; @@ -1984,7 +2074,7 @@ public: ~Pipeline(); private: - memory::NullOnMove m_device {}; + NullOnMove m_device {}; VkPipeline m_pipeline {}; }; @@ -2036,7 +2126,7 @@ private: return m_layout; } - memory::NullOnMove m_device {}; + NullOnMove m_device {}; VkPipelineLayout m_layout {}; }; @@ -2432,7 +2522,7 @@ private: return m_queue; } - memory::NullOnMove m_device; + NullOnMove m_device; VkQueue m_queue; }; @@ -2486,7 +2576,7 @@ private: return m_memory; } - memory::NullOnMove m_device {}; + NullOnMove m_device {}; VkDeviceMemory m_memory {}; }; @@ -2540,7 +2630,7 @@ public: auto operator=(const Messenger &) -> Messenger & = delete; private: - memory::NullOnMove m_instance {}; + NullOnMove m_instance {}; VkDebugUtilsMessengerEXT m_messenger {}; }; diff --git a/tools/cmake/functions.cmake b/tools/cmake/functions.cmake index 64e4a8c..3215559 100644 --- a/tools/cmake/functions.cmake +++ b/tools/cmake/functions.cmake @@ -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()