Skip to content

Commit

Permalink
refactor: make intermediate_char a comon type-trait uint_with_size
Browse files Browse the repository at this point in the history
  • Loading branch information
DNKpp committed Oct 22, 2024
1 parent c039fc5 commit 078d923
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 28 deletions.
16 changes: 16 additions & 0 deletions include/mimic++/Fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ namespace mimicpp
template <typename First, typename... Others>
inline constexpr bool is_overload_set_v = is_overload_set<First, Others...>::value;

/**
* \brief Primary template, purposely undefined.
* \ingroup TYPE_TRAITS_UINT_WITH_SIZE
* \tparam byteCount The expected size.
*/
template <std::size_t byteCount>
struct uint_with_size;

/**
* \brief Convenience constant, exposing the ``value`` member of the actual type-trait.
* \ingroup TYPE_TRAITS_UINT_WITH_SIZE
* \tparam byteCount The expected size.
*/
template <std::size_t byteCount>
using uint_with_size_t = typename uint_with_size<byteCount>::type;

template <typename T>
struct is_character;

Expand Down
29 changes: 1 addition & 28 deletions include/mimic++/Printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,33 +523,6 @@ namespace mimicpp::detail
}
};

template <std::size_t byteCount>
struct intermediate_char;

template <>
struct intermediate_char<1u>
{
using type = std::uint8_t;
};

template <>
struct intermediate_char<2u>
{
using type = std::uint16_t;
};

template <>
struct intermediate_char<4u>
{
using type = std::uint32_t;
};

template <>
struct intermediate_char<8u>
{
using type = std::uint64_t;
};

template <string String>
class Printer<String>
{
Expand Down Expand Up @@ -589,7 +562,7 @@ namespace mimicpp::detail
}
else
{
using intermediate_t = typename intermediate_char<sizeof(string_char_t<String>)>::type;
using intermediate_t = uint_with_size_t<sizeof(string_char_t<String>)>;

auto view = string_traits<std::remove_cvref_t<T>>::view(std::forward<T>(str));
auto iter = std::ranges::begin(view);
Expand Down
49 changes: 49 additions & 0 deletions include/mimic++/TypeTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,55 @@ namespace mimicpp
{
};

/**
* \}
*/

/**
* \defgroup TYPE_TRAITS_UINT_WITH_SIZE unit_with_size
* \ingroup TYPE_TRAITS
* \brief Provides the member alias ``type`` with the expected uint-type.
* \details This trait always yields a type with the exact size. If no such type exists, the member ``type`` is undefined.
*
*\{
*/

/**
* \brief 1-byte specialization.
*/
template <>
struct uint_with_size<1u>
{
using type = std::uint8_t;
};

/**
* \brief 2-byte specialization.
*/
template <>
struct uint_with_size<2u>
{
using type = std::uint16_t;
};

/**
* \brief 4-byte specialization.
*/
template <>
struct uint_with_size<4u>
{
using type = std::uint32_t;
};

/**
* \brief 8-byte specialization.
*/
template <>
struct uint_with_size<8u>
{
using type = std::uint64_t;
};

/**
* \}
*/
Expand Down
15 changes: 15 additions & 0 deletions test/unit-tests/TypeTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,18 @@ TEMPLATE_TEST_CASE_SIG(
STATIC_REQUIRE(expected == mimicpp::is_overload_set<Others..., First>::value);
STATIC_REQUIRE(expected == mimicpp::is_overload_set_v<Others..., First>);
}

TEMPLATE_TEST_CASE_SIG(
"uint_with_size yields a uint type with the expected size.",
"[type_traits]",
((typename Expected, std::size_t size), Expected, size),
(std::uint8_t, 1),
(std::uint16_t, 2),
(std::uint32_t, 4),
(std::uint64_t, 8)
)
{
STATIC_CHECK(size == sizeof(Expected));
STATIC_REQUIRE(std::same_as<Expected, typename mimicpp::uint_with_size<size>::type>);
STATIC_REQUIRE(std::same_as<Expected, mimicpp::uint_with_size_t<size>>);
}

0 comments on commit 078d923

Please sign in to comment.