From 79620980ea3880f6512a35b9d688a60a02ff8b98 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 5 Oct 2024 08:33:39 -0500 Subject: Formatting changes. Refactoring out detail::CopyMove from Result. --- CMakeLists.txt | 2 +- cmake/CheckGit.cmake | 14 +++++++------- cmake/fud_version.hpp.in | 15 +++++++++++++++ cmake/git_version.hpp.in | 15 --------------- include/fud_assert.hpp | 12 ++++++++---- include/fud_result.hpp | 45 ++++++++++++--------------------------------- include/libfud.hpp | 3 ++- source/fud_assert.cpp | 45 ++++++++++++++++++++++++++++++++++++--------- source/libfud.cpp | 4 ++-- test/test_fud.cpp | 2 +- 10 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 cmake/fud_version.hpp.in delete mode 100644 cmake/git_version.hpp.in diff --git a/CMakeLists.txt b/CMakeLists.txt index f372f97..cea07dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,5 +114,5 @@ install(TARGETS fud include(cmake/CheckGit.cmake) CheckGitSetup(GIT_HASH) -configure_file(cmake/git_version.hpp.in include/git_version.hpp @ONLY) +configure_file(cmake/fud_version.hpp.in include/fud_version.hpp @ONLY) add_dependencies(fud AlwaysCheckGit) diff --git a/cmake/CheckGit.cmake b/cmake/CheckGit.cmake index bd17447..f7d8bce 100644 --- a/cmake/CheckGit.cmake +++ b/cmake/CheckGit.cmake @@ -7,8 +7,8 @@ if (NOT DEFINED post_configure_dir) set(post_configure_dir ${CMAKE_BINARY_DIR}/include) endif () -set(pre_configure_file ${pre_configure_dir}/git_version.hpp.in) -set(post_configure_file ${post_configure_dir}/git_version.hpp) +set(pre_configure_file ${pre_configure_dir}/fud_version.hpp.in) +set(post_configure_file ${post_configure_dir}/fud_version.hpp) function(CheckGitWrite git_hash) file(WRITE ${CMAKE_BINARY_DIR}/git-state.txt ${git_hash}) @@ -37,15 +37,15 @@ function(CheckGitVersion git_hash) file(MAKE_DIRECTORY ${post_configure_dir}) endif () - # if (NOT EXISTS ${post_configure_dir}/git_version.h) - # file(COPY ${pre_configure_dir}/git_version.h DESTINATION ${post_configure_dir}) + # if (NOT EXISTS ${post_configure_dir}/fud_version.h) + # file(COPY ${pre_configure_dir}/fud_version.h DESTINATION ${post_configure_dir}) # endif() if (NOT DEFINED GIT_HASH_CACHE) set(GIT_HASH_CACHE "INVALID") endif () - # Only update the git_version.cpp if the hash has changed. This will + # Only update the fud_version.cpp if the hash has changed. This will # prevent us from rebuilding the project more than we need to. if (NOT ${GIT_HASH} STREQUAL ${GIT_HASH_CACHE} OR NOT EXISTS ${post_configure_file}) # Set che GIT_HASH_CACHE variable the next build won't have @@ -70,8 +70,8 @@ function(CheckGitSetup top_git_hash) BYPRODUCTS ${post_configure_file} ) - add_library(git_version INTERFACE ${CMAKE_BINARY_DIR}/include/git_version.hpp) - add_dependencies(git_version AlwaysCheckGit) + add_library(fud_version INTERFACE ${CMAKE_BINARY_DIR}/include/fud_version.hpp) + add_dependencies(fud_version AlwaysCheckGit) CheckGitVersion(git_hash) set(${top_git_hash} ${git_hash} PARENT_SCOPE) diff --git a/cmake/fud_version.hpp.in b/cmake/fud_version.hpp.in new file mode 100644 index 0000000..5cce0e4 --- /dev/null +++ b/cmake/fud_version.hpp.in @@ -0,0 +1,15 @@ +#ifndef FUD_VERSION_HPP +#define FUD_VERSION_HPP + +#include + +namespace fud { + +constexpr uint8_t FudVersionMajor = @PROJECT_VERSION_MAJOR@; +constexpr uint8_t FudVersionMinor = @PROJECT_VERSION_MINOR@; +constexpr uint8_t FudVersionPatch = @PROJECT_VERSION_PATCH@; +constexpr const char GitHash[] = "@GIT_HASH@"; + +} // namespace fud + +#endif diff --git a/cmake/git_version.hpp.in b/cmake/git_version.hpp.in deleted file mode 100644 index ede9623..0000000 --- a/cmake/git_version.hpp.in +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef GIT_VERSION_HPP -#define GIT_VERSION_HPP - -#include - -namespace fud { - -constexpr uint8_t FudVersionMajor = @PROJECT_VERSION_MAJOR@; -constexpr uint8_t FudVersionMinor = @PROJECT_VERSION_MINOR@; -constexpr uint8_t FudVersionPatch = @PROJECT_VERSION_PATCH@; -constexpr const char GitHash[] = "@GIT_HASH@"; - -} // namespace fud - -#endif diff --git a/include/fud_assert.hpp b/include/fud_assert.hpp index fd861ea..8caf751 100644 --- a/include/fud_assert.hpp +++ b/include/fud_assert.hpp @@ -20,11 +20,15 @@ namespace fud { -void assertFail(const char* assertion, const char* file, unsigned int line, const char* function) noexcept(false) - __attribute__((__noreturn__)); +// clang-format off +[[noreturn]] void assertFail( + const char* assertion, + const char* file, + unsigned int line, + const char* function) noexcept(false); +// clang-format on -#define fudAssert(expr) \ - ((expr) ? static_cast(0) : assertFail(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) +#define fudAssert(expr) ((expr) ? static_cast(0) : assertFail(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) } // namespace fud diff --git a/include/fud_result.hpp b/include/fud_result.hpp index 076af21..74954df 100644 --- a/include/fud_result.hpp +++ b/include/fud_result.hpp @@ -23,30 +23,6 @@ namespace fud { -namespace detail { -template -class CopyMove { - public: - explicit constexpr CopyMove(T value) : m_value{std::move(value)} - { - } - - constexpr T&& take() - { - return std::move(m_value); - } - - constexpr T copy() const - { - return m_value; - } - - private: - T m_value; -}; - -} // namespace detail - /** \brief A result type which contains either a T on success or an E on error. */ template class [[nodiscard]] Result { @@ -84,43 +60,46 @@ class [[nodiscard]] Result { [[nodiscard]] T getOkay() const { - return std::get>(m_value).copy(); + return std::get(m_value); } [[nodiscard]] E getError() const { - return std::get>(m_value).copy(); + return std::get(m_value); } [[nodiscard]] T&& getOkay() { - return std::get>(m_value).take(); + return std::move(std::get(m_value)); } [[nodiscard]] E&& getError() { - return std::get>(m_value).take(); + return std::move(std::get(m_value)); } private: explicit Result() : m_value() { } - explicit Result(const T& value) : m_value{detail::CopyMove{value}} + + explicit Result(const T& value) : m_value{value} { } - explicit Result(const E& value) : m_value{detail::CopyMove{value}} + + explicit Result(const E& value) : m_value{value} { } - explicit Result(T&& value) : m_value{detail::CopyMove{std::move(value)}} + explicit Result(T&& value) : m_value{std::move(value)} { } - explicit Result(E&& value) : m_value{detail::CopyMove{std::move(value)}} + + explicit Result(E&& value) : m_value{std::move(value)} { } - std::variant, detail::CopyMove> m_value; + std::variant m_value; }; } // namespace fud diff --git a/include/libfud.hpp b/include/libfud.hpp index 70165b3..c670ed4 100644 --- a/include/libfud.hpp +++ b/include/libfud.hpp @@ -18,10 +18,10 @@ #ifndef LIBFUD_HPP #define LIBFUD_HPP +#include "fud_array.hpp" #include "fud_result.hpp" #include "fud_status.hpp" #include "fud_string.hpp" -#include "fud_array.hpp" #include @@ -36,6 +36,7 @@ struct FUD { Array revision; }; +/** \brief Get the version of FUD including git revision. */ FUD fud(); /** diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index f3358df..9c2ec77 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -1,26 +1,53 @@ #include "fud_assert.hpp" #include "fud_array.hpp" +#include "fud_string_view.hpp" +#include #include -#include #include +#include namespace fud { void assertFail(const char* assertion, const char* file, unsigned int line, const char* function) noexcept(false) { - constexpr size_t ASSERT_MSG_SIZE = 1024; + constexpr size_t MAX_FILE_CHARS = 256; + constexpr size_t MAX_FUNCTION_CHARS = 256; + constexpr size_t BITS_PER_OCTAL = 3; + constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(line)) + 3; + constexpr size_t MAX_ASSERT_CHARS = 512 - MAX_LINE_CHARS; + constexpr size_t ASSERT_MSG_SIZE = MAX_FILE_CHARS + MAX_LINE_CHARS + MAX_FUNCTION_CHARS + MAX_ASSERT_CHARS; + + auto lengthResult = cStringLength(file); + size_t filenameLength = 0; + auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX; + if (!badLength) { + filenameLength = static_cast(lengthResult); + } + const char* filename = nullptr; + if (badLength) { + constexpr const char invalidFile[] = "INVALID FILE"; + static_assert(sizeof(invalidFile) < MAX_FILE_CHARS); + filenameLength = sizeof(invalidFile); + filename = invalidFile; + } else if (filenameLength > MAX_LINE_CHARS) { + filename = file + filenameLength - MAX_LINE_CHARS; + filenameLength = MAX_FILE_CHARS; + } + Array buffer{}; + // clang-format off static_cast(std::format_to_n( - buffer.data(), - buffer.size() - 1U, - "{}:{}: {}:Assertion `{}` failed", - file, + buffer.data(), buffer.size() - 1U, + "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed", + filename, filenameLength, line, - function, - assertion)); + function, MAX_FUNCTION_CHARS, + assertion, MAX_ASSERT_CHARS)); + // clang-format on + throw std::runtime_error(buffer.data()); } -} +} // namespace fud diff --git a/source/libfud.cpp b/source/libfud.cpp index 252eae0..8c962ba 100644 --- a/source/libfud.cpp +++ b/source/libfud.cpp @@ -18,7 +18,7 @@ #include "libfud.hpp" #include "fud_assert.hpp" -#include "git_version.hpp" +#include "fud_version.hpp" #include @@ -27,10 +27,10 @@ namespace fud { FUD fud() { FUD fudInfo{}; + static_assert(sizeof(GitHash) >= sizeof(fudInfo.revision)); fudInfo.major = FudVersionMajor; fudInfo.minor = FudVersionMinor; fudInfo.patch = FudVersionPatch; - static_assert(sizeof(GitHash) >= sizeof(fudInfo.revision)); auto copyResult = copyMem(fudInfo.revision.data(), fudInfo.revision.size(), GitHash, fudInfo.revision.size() - 1); fudAssert(copyResult == FudStatus::Success); fudInfo.revision[fudInfo.revision.size() - 1] = '\0'; diff --git a/test/test_fud.cpp b/test/test_fud.cpp index a6519fb..0778f98 100644 --- a/test/test_fud.cpp +++ b/test/test_fud.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "git_version.hpp" +#include "fud_version.hpp" #include "libfud.hpp" #include "gtest/gtest.h" -- cgit v1.2.3