diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | cmake/CheckGit.cmake | 14 | ||||
-rw-r--r-- | cmake/fud_version.hpp.in (renamed from cmake/git_version.hpp.in) | 4 | ||||
-rw-r--r-- | include/fud_assert.hpp | 12 | ||||
-rw-r--r-- | include/fud_result.hpp | 45 | ||||
-rw-r--r-- | include/libfud.hpp | 3 | ||||
-rw-r--r-- | source/fud_assert.cpp | 45 | ||||
-rw-r--r-- | source/libfud.cpp | 4 | ||||
-rw-r--r-- | test/test_fud.cpp | 2 |
9 files changed, 71 insertions, 60 deletions
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/git_version.hpp.in b/cmake/fud_version.hpp.in index ede9623..5cce0e4 100644 --- a/cmake/git_version.hpp.in +++ b/cmake/fud_version.hpp.in @@ -1,5 +1,5 @@ -#ifndef GIT_VERSION_HPP -#define GIT_VERSION_HPP +#ifndef FUD_VERSION_HPP +#define FUD_VERSION_HPP #include <cstdint> 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<void>(0) : assertFail(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) +#define fudAssert(expr) ((expr) ? static_cast<void>(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 <typename T> -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 <typename T, typename E> class [[nodiscard]] Result { @@ -84,43 +60,46 @@ class [[nodiscard]] Result { [[nodiscard]] T getOkay() const { - return std::get<detail::CopyMove<T>>(m_value).copy(); + return std::get<T>(m_value); } [[nodiscard]] E getError() const { - return std::get<detail::CopyMove<E>>(m_value).copy(); + return std::get<E>(m_value); } [[nodiscard]] T&& getOkay() { - return std::get<detail::CopyMove<T>>(m_value).take(); + return std::move(std::get<T>(m_value)); } [[nodiscard]] E&& getError() { - return std::get<detail::CopyMove<E>>(m_value).take(); + return std::move(std::get<E>(m_value)); } private: explicit Result() : m_value() { } - explicit Result(const T& value) : m_value{detail::CopyMove<T>{value}} + + explicit Result(const T& value) : m_value{value} { } - explicit Result(const E& value) : m_value{detail::CopyMove<E>{value}} + + explicit Result(const E& value) : m_value{value} { } - explicit Result(T&& value) : m_value{detail::CopyMove<T>{std::move(value)}} + explicit Result(T&& value) : m_value{std::move(value)} { } - explicit Result(E&& value) : m_value{detail::CopyMove<E>{std::move(value)}} + + explicit Result(E&& value) : m_value{std::move(value)} { } - std::variant<detail::CopyMove<T>, detail::CopyMove<E>> m_value; + std::variant<T, E> 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 <cstdint> @@ -36,6 +36,7 @@ struct FUD { Array<char, GIT_REV_CHARS> 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 <climits> #include <cstdio> -#include <stdexcept> #include <format> +#include <stdexcept> 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<size_t>(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<char, ASSERT_MSG_SIZE> buffer{}; + // clang-format off static_cast<void>(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 <cstdlib> @@ -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" |