Skip to content

Commit

Permalink
feat(format): allow formatting of all types of pointers (#72)
Browse files Browse the repository at this point in the history
- const and volatile void pointers are directly formatted
- other pointer types (e.g. int*) can be formatted by using emio::ptr(...)
  • Loading branch information
Viatorus authored Oct 12, 2023
1 parent 1f161cf commit eec5d5f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
8 changes: 8 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,14 @@ emio::format("{}", spec.with(3.141592653)); // 3.1
There exists formatter for builtin types like bool, char, string, integers, floats, void* and non-scoped enums, ranges
and tuple like types. Support for other standard types (e.g. chrono duration, optional) is planned.

For formatting values of pointer-like types, simply use `emio::ptr(p)`.

*Example*
```cpp
int* value = get();
emio::format("{}", emio::ptr(value));
```
Use `is_formattable_v<Type>` to check if a type is formattable.
A formatter exists of one optional function `validate` and two mandatory functions `parse` and `format`. If `validate`
Expand Down
18 changes: 10 additions & 8 deletions include/emio/detail/format/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,16 +502,19 @@ constexpr result<void> write_arg(writer& out, format_specs& specs, const Arg arg
});
}

template <typename T>
inline constexpr bool is_void_pointer_v =
std::is_pointer_v<T> && std::is_same_v<std::remove_cv_t<std::remove_pointer_t<T>>, void>;

template <typename Arg>
requires(std::is_same_v<Arg, void*> || std::is_same_v<Arg, std::nullptr_t>)
requires(is_void_pointer_v<Arg> || std::is_null_pointer_v<Arg>)
constexpr result<void> write_arg(writer& out, format_specs& specs, Arg arg) noexcept {
specs.alternate_form = true;
specs.type = 'x';
if constexpr (std::is_same_v<Arg, std::nullptr_t>) {
if constexpr (std::is_null_pointer_v<Arg>) {
return write_arg(out, specs, uintptr_t{0});
} else {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): valid cast
return write_arg(out, specs, reinterpret_cast<uintptr_t>(arg));
return write_arg(out, specs, std::bit_cast<uintptr_t>(arg));
}
}

Expand Down Expand Up @@ -794,7 +797,7 @@ template <typename T>
inline constexpr bool is_core_type_v =
std::is_same_v<T, bool> || std::is_same_v<T, char> || std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t> ||
std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t> || std::is_same_v<T, double> ||
std::is_same_v<T, std::nullptr_t> || std::is_same_v<T, void*> || std::is_same_v<T, std::string_view>;
std::is_null_pointer_v<T> || is_void_pointer_v<T> || std::is_same_v<T, std::string_view>;

template <typename T>
concept has_format_as = requires(T arg) { format_as(arg); };
Expand All @@ -812,7 +815,7 @@ struct unified_type {
};

template <typename T>
requires(!std::is_integral_v<T> && !std::is_same_v<T, std::nullptr_t> && std::is_constructible_v<std::string_view, T>)
requires(!std::is_integral_v<T> && !std::is_null_pointer_v<T> && std::is_constructible_v<std::string_view, T>)
struct unified_type<T> {
using type = std::string_view;
};
Expand All @@ -830,8 +833,7 @@ struct unified_type<T> {
};

template <typename T>
requires(std::is_same_v<T, char> || std::is_same_v<T, bool> || std::is_same_v<T, void*> ||
std::is_same_v<T, std::nullptr_t>)
requires(std::is_same_v<T, char> || std::is_same_v<T, bool> || is_void_pointer_v<T> || std::is_null_pointer_v<T>)
struct unified_type<T> {
using type = T;
};
Expand Down
29 changes: 28 additions & 1 deletion include/emio/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#pragma once

#include <memory>

#include "detail/format/formatter.hpp"

namespace emio {
Expand Down Expand Up @@ -79,7 +81,7 @@ class formatter<T> {
EMIO_TRYV(check_bool_specs(specs));
} else if constexpr (std::is_same_v<T, char>) {
EMIO_TRYV(check_char_specs(specs));
} else if constexpr (std::is_same_v<T, std::nullptr_t> || std::is_same_v<T, void*>) {
} else if constexpr (detail::format::is_void_pointer_v<T> || std::is_null_pointer_v<T>) {
EMIO_TRYV(check_pointer_specs(specs));
} else if constexpr (std::is_integral_v<T>) {
EMIO_TRYV(check_integral_specs(specs));
Expand Down Expand Up @@ -277,4 +279,29 @@ class formatter<detail::format_spec_with_value<T>> {
formatter<T> underlying_{};
};

/**
* Converts a value of a pointer-like type to const void * for pointer formatting.
* @param p The value of the pointer.
* @return The const void* version of the pointer.
*/
template <typename T>
requires(std::is_pointer_v<T>)
constexpr auto ptr(T p) noexcept {
if constexpr (std::is_volatile_v<std::remove_pointer_t<T>>) {
return static_cast<const volatile void*>(p);
} else {
return static_cast<const void*>(p);
}
}

template <typename T, typename Deleter>
constexpr const void* ptr(const std::unique_ptr<T, Deleter>& p) {
return p.get();
}

template <typename T>
const void* ptr(const std::shared_ptr<T>& p) {
return p.get();
}

} // namespace emio
21 changes: 21 additions & 0 deletions test/unit_test/test_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,27 @@ TEST_CASE("format_pointer") {
// EXPECT_EQ(fmt::format("{}", fmt::detail::bit_cast<const void*>(&function_pointer_test)), fmt::format("{}",
// fmt::ptr(function_pointer_test)));
CHECK(emio::format("{}", nullptr) == "0x0");

// Const and/or volatile.
static_assert(std::is_same_v<std::remove_cv_t<void* const>, void*>);
CHECK(emio::format("{}", reinterpret_cast<const void*>(0x456)) == "0x456");
CHECK(emio::format("{}", reinterpret_cast<volatile void*>(0x789)) == "0x789");
CHECK(emio::format("{}", reinterpret_cast<const volatile void*>(0x101112)) == "0x101112");

// With emio::ptr
int* i1{};
volatile int* i2{};
const int* i3{};
const volatile int* i4{};
std::unique_ptr<int> u;
std::shared_ptr<int> s;

CHECK(emio::format("{}", emio::ptr(i1)) == "0x0");
CHECK(emio::format("{}", emio::ptr(i2)) == "0x0");
CHECK(emio::format("{}", emio::ptr(i3)) == "0x0");
CHECK(emio::format("{}", emio::ptr(i4)) == "0x0");
CHECK(emio::format("{}", emio::ptr(u)) == "0x0");
CHECK(emio::format("{}", emio::ptr(s)) == "0x0");
}

enum color { red, green, blue };
Expand Down

0 comments on commit eec5d5f

Please sign in to comment.