From 1e84b30477ac1d1623989100acf92b3c56b52341 Mon Sep 17 00:00:00 2001 From: light7734 Date: Mon, 19 May 2025 21:03:13 +0330 Subject: [PATCH] wip --- modules/CMakeLists.txt | 3 +- modules/light/.gitkeep | 0 modules/light/CMakeLists.txt | 2 +- modules/light/src/entrypoint.cpp | 13 ++++- modules/universal_pch/CMakeLists.txt | 2 + .../universal_pch/error_handling/checks.hpp | 55 +++++++++++++++++++ .../universal_pch/error_handling/types.hpp | 31 +++++++++++ .../include/universal_pch/module_configs.hpp | 15 +++++ .../include/universal_pch/std_headers.hpp} | 8 ++- modules/universal_pch/src/pch.hpp | 7 +++ modules/utilities/preliminary/CMakeLists.txt | 3 - .../preliminary/include/preliminary/types.hpp | 21 ------- 12 files changed, 130 insertions(+), 30 deletions(-) delete mode 100644 modules/light/.gitkeep create mode 100644 modules/universal_pch/CMakeLists.txt create mode 100644 modules/universal_pch/include/universal_pch/error_handling/checks.hpp create mode 100644 modules/universal_pch/include/universal_pch/error_handling/types.hpp create mode 100644 modules/universal_pch/include/universal_pch/module_configs.hpp rename modules/{utilities/preliminary/src/pch.hpp => universal_pch/include/universal_pch/std_headers.hpp} (93%) create mode 100644 modules/universal_pch/src/pch.hpp delete mode 100644 modules/utilities/preliminary/CMakeLists.txt delete mode 100644 modules/utilities/preliminary/include/preliminary/types.hpp diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index a0025dd..3c807c5 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -1,2 +1,3 @@ -add_subdirectory(utilities/preliminary) +add_subdirectory(universal_pch) + add_subdirectory(light) diff --git a/modules/light/.gitkeep b/modules/light/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/modules/light/CMakeLists.txt b/modules/light/CMakeLists.txt index 75d113c..93c6b8c 100644 --- a/modules/light/CMakeLists.txt +++ b/modules/light/CMakeLists.txt @@ -1,3 +1,3 @@ add_executable(light ${CMAKE_CURRENT_SOURCE_DIR}/src/entrypoint.cpp) -target_link_libraries(light PRIVATE preliminary) +target_link_libraries(light PRIVATE universal_pch errors) diff --git a/modules/light/src/entrypoint.cpp b/modules/light/src/entrypoint.cpp index dd06efd..21bc165 100644 --- a/modules/light/src/entrypoint.cpp +++ b/modules/light/src/entrypoint.cpp @@ -1,6 +1,17 @@ -auto main() -> lt::i32 +auto main() -> int32_t +try { std::cout << "Light built and ran fine..."; return 0; } +catch (const std::exception &exp) +{ + std::cout << "\n\nUnhandled std exception: " << exp.what() << std::endl; // NOLINT + return -1; +} +catch (...) +{ + std::cout << "\n\nUnhandled exception" << std::endl; // NOLINT + return -1; +} diff --git a/modules/universal_pch/CMakeLists.txt b/modules/universal_pch/CMakeLists.txt new file mode 100644 index 0000000..d6cd2b0 --- /dev/null +++ b/modules/universal_pch/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library_module(universal_pch) +target_precompile_headers(universal_pch INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src/pch.hpp) diff --git a/modules/universal_pch/include/universal_pch/error_handling/checks.hpp b/modules/universal_pch/include/universal_pch/error_handling/checks.hpp new file mode 100644 index 0000000..97a280b --- /dev/null +++ b/modules/universal_pch/include/universal_pch/error_handling/checks.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include +#include + +namespace lt { + +namespace details { + +inline void throw_if_false(bool condition, std::source_location location) +{ + if (!condition) + { + throw Error { "Failed runtime assertion", location }; + } +} + +} // namespace details + +/** Throws lt::Error if \a condition is false, otherwise returns without side-effects. + * + * @throws lt::Error + * + * @note Only runs when LIGHT_ENABLE_CHECK is enabled, which is enabled by default on debug builds. + * + * @warn The successful execution of the contract of the using code should NOT depend on the + * execution of this function as it may or may NOT be executed--based on project configuration. + */ +inline void check(bool condition, std::source_location location = std::source_location::current()) +{ + if constexpr (light_enabled_check) + { + details::throw_if_false(condition, location); + } +} + +/** Throws lt::Error if \a condition is false, otherwise returns without side-effects. + * + * @throws lt::Error + * + * @note Only runs when LIGHT_ENABLE_ENSURE is enabled, which is enabled by default on debug and + * release builds. + * + * @warn The successful execution of the contract of the using code should NOT depend on the + * execution of this function as it may or may NOT be executed--based on project configuration. + */ +inline void ensure(bool condition, std::source_location location = std::source_location::current()) +{ + if constexpr (light_enabled_ensure) + { + details::throw_if_false(condition, location); + } +} + +} // namespace lt diff --git a/modules/universal_pch/include/universal_pch/error_handling/types.hpp b/modules/universal_pch/include/universal_pch/error_handling/types.hpp new file mode 100644 index 0000000..0cfed5c --- /dev/null +++ b/modules/universal_pch/include/universal_pch/error_handling/types.hpp @@ -0,0 +1,31 @@ +#pragma once + +namespace lt { + +class Error: public std::exception +{ +public: + Error(std::string_view reason, std::source_location location): m_reason(reason) + { + std::ignore = location; + } + + Error(const Error &prev, std::string_view reason, std::source_location location) + : m_reason(reason) + { + std::ignore = location; + std::ignore = prev; + } + + [[nodiscard]] auto what() const noexcept -> const char * override + { + return "Idk fuck all"; + } + +private: + std::string m_reason; +}; + +template +using Expected = std::expected; +} // namespace lt diff --git a/modules/universal_pch/include/universal_pch/module_configs.hpp b/modules/universal_pch/include/universal_pch/module_configs.hpp new file mode 100644 index 0000000..a10766a --- /dev/null +++ b/modules/universal_pch/include/universal_pch/module_configs.hpp @@ -0,0 +1,15 @@ +namespace lt { + +#ifdef LIGHT_ENABLE_CHECK +constexpr bool light_enabled_check = true; +#else +constexpr bool light_enabled_check = false; +#endif + +#ifdef LIGHT_ENABLE_ENSURE +constexpr bool light_enabled_ensure = true; +#else +constexpr bool light_enabled_ensure = false; +#endif + +} // namespace lt diff --git a/modules/utilities/preliminary/src/pch.hpp b/modules/universal_pch/include/universal_pch/std_headers.hpp similarity index 93% rename from modules/utilities/preliminary/src/pch.hpp rename to modules/universal_pch/include/universal_pch/std_headers.hpp index 4145db8..ee52730 100644 --- a/modules/utilities/preliminary/src/pch.hpp +++ b/modules/universal_pch/include/universal_pch/std_headers.hpp @@ -1,6 +1,4 @@ -#pragma once - -#include +// NOLINTBEGIN /* utilities */ #include @@ -10,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -64,3 +64,5 @@ /* filesystem */ #include + +// NOLINTEND diff --git a/modules/universal_pch/src/pch.hpp b/modules/universal_pch/src/pch.hpp new file mode 100644 index 0000000..08fb15a --- /dev/null +++ b/modules/universal_pch/src/pch.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include + +/* error_handling */ +#include +#include diff --git a/modules/utilities/preliminary/CMakeLists.txt b/modules/utilities/preliminary/CMakeLists.txt deleted file mode 100644 index 680a173..0000000 --- a/modules/utilities/preliminary/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_library(preliminary INTERFACE) -target_precompile_headers(preliminary INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src/pch.hpp) -target_include_directories(preliminary INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/) diff --git a/modules/utilities/preliminary/include/preliminary/types.hpp b/modules/utilities/preliminary/include/preliminary/types.hpp deleted file mode 100644 index d104ec7..0000000 --- a/modules/utilities/preliminary/include/preliminary/types.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace lt { - -using i8 = int8_t; -using i16 = int16_t; -using i32 = int32_t; -using i64 = int64_t; - -using u8 = uint8_t; -using u16 = uint16_t; -using u32 = uint32_t; -using u64 = uint64_t; - -template -using vec = std::vector; - -} // namespace lt