From b012964099ec26b37a2f41ba871e575d352bb51e Mon Sep 17 00:00:00 2001 From: DH Date: Thu, 31 Oct 2024 22:57:08 +0300 Subject: [PATCH] utilities: fix refl::calcFieldCount for enum add unordered_vector utility modernize hexdump --- rx/include/rx/refl.hpp | 6 +++--- rx/include/rx/unordered_vector.hpp | 29 +++++++++++++++++++++++++++++ rx/src/hexdump.cpp | 29 +++++++++++++++-------------- 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 rx/include/rx/unordered_vector.hpp diff --git a/rx/include/rx/refl.hpp b/rx/include/rx/refl.hpp index b2c38a8..e829fea 100644 --- a/rx/include/rx/refl.hpp +++ b/rx/include/rx/refl.hpp @@ -139,11 +139,11 @@ constexpr auto calcFieldCount() { } else { constexpr auto c = getNameOf()[0]; if constexpr (requires { EnumT::Count; }) { - return EnumT::Count; + return static_cast(EnumT::Count); } else if constexpr (requires { EnumT::_count; }) { - return EnumT::_count; + return static_cast(EnumT::_count); } else if constexpr (requires { EnumT::count; }) { - return EnumT::count; + return static_cast(EnumT::count); } else if constexpr (!requires { getNameOf()[0]; }) { return N; } else if constexpr (c >= '0' && c <= '9') { diff --git a/rx/include/rx/unordered_vector.hpp b/rx/include/rx/unordered_vector.hpp new file mode 100644 index 0000000..8984e01 --- /dev/null +++ b/rx/include/rx/unordered_vector.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +namespace rx { +template +void unordered_vector_erase(VectorT &vector, std::size_t pos) { + if (pos + 1 == vector.size()) { + vector.pop_back(); + return; + } + + std::swap(vector[pos], vector.back()); + vector.pop_back(); +} + +template +void unordered_vector_insert(VectorT &vector, std::size_t pos, Value &&value) { + bool isLast = pos + 1 == vector.size(); + vector.push_back(std::move(value)); + + if (isLast) { + return; + } + + std::swap(vector[pos], vector.back()); +} +} // namespace rx diff --git a/rx/src/hexdump.cpp b/rx/src/hexdump.cpp index 0ee68c8..0917912 100644 --- a/rx/src/hexdump.cpp +++ b/rx/src/hexdump.cpp @@ -1,6 +1,7 @@ #include "hexdump.hpp" #include #include +#include #include void rx::hexdump(std::span bytes) { @@ -16,13 +17,13 @@ void rx::hexdump(std::span bytes) { } flockfile(stderr); - std::fprintf(stderr, "%s ", std::string(sizeWidth, ' ').c_str()); + std::print(stderr, "{} ", std::string(sizeWidth, ' ')); for (unsigned i = 0; i < 16; ++i) { - std::fprintf(stderr, " %02x", i); + std::print(stderr, " {:02x}", i); } - std::fprintf(stderr, "\n%s ", std::string(sizeWidth, ' ').c_str()); + std::print(stderr, "\n{} ", std::string(sizeWidth, ' ')); for (unsigned i = 0; i < 16; ++i) { - std::fprintf(stderr, " --"); + std::print(stderr, " --"); } std::byte zeros[16]{}; @@ -36,7 +37,7 @@ void rx::hexdump(std::span bytes) { if (std::memcmp(bytes.data() + i, zeros, 16) == 0) { if (!dotsPrinted) { dotsPrinted = true; - std::printf("\n..."); + std::print(stderr, "\n..."); } i += 15; continue; @@ -44,22 +45,22 @@ void rx::hexdump(std::span bytes) { } if (!dotsPrinted) { - std::fprintf(stderr, " | "); + std::print(stderr, " | "); for (std::size_t j = i - 16; j < i; ++j) { auto c = unsigned(bytes[j]); - std::fprintf(stderr, "%c", + std::print(stderr, "{:c}", (std::isprint(c) && c != '\n') ? c : '.'); } } } - std::fprintf(stderr, "\n"); - std::fprintf(stderr, "%0*zx ", sizeWidth, i); + std::println(stderr, ""); + std::print(stderr, "{:0{}x} ", i, sizeWidth); wasAllZeros = true; dotsPrinted = false; } - std::fprintf(stderr, " %02x", unsigned(bytes[i])); + std::print(stderr, " {:02x}", unsigned(bytes[i])); if (bytes[i] != std::byte{0}) { wasAllZeros = false; @@ -68,18 +69,18 @@ void rx::hexdump(std::span bytes) { if (!bytes.empty()) { for (std::size_t i = 0; i < (16 - (bytes.size() % 16)) % 16; ++i) { - std::fprintf(stderr, " "); + std::print(stderr, " "); } - std::fprintf(stderr, " | "); + std::print(stderr, " | "); for (std::size_t j = bytes.size() - std::min(bytes.size(), (bytes.size() % 16 ? bytes.size() % 16 : 16)); j < bytes.size(); ++j) { auto c = unsigned(bytes[j]); - std::fprintf(stderr, "%c", (std::isprint(c) && c != '\n') ? c : '.'); + std::print(stderr, "{:c}", (std::isprint(c) && c != '\n') ? c : '.'); } } - std::fprintf(stderr, "\n"); + std::println(stderr, ""); funlockfile(stderr); }