[COMMIT seastar master] util/print_safe: use concept for type constraints

0 views
Skip to first unread message

Commit Bot

<bot@cloudius-systems.com>
unread,
Jan 30, 2023, 1:59:51 PM1/30/23
to seastar-dev@googlegroups.com, Kefu Chai
From: Kefu Chai <kefu...@scylladb.com>
Committer: Kefu Chai <kefu...@scylladb.com>
Branch: master

util/print_safe: use concept for type constraints

for better error messages, if the constraits is not fulfilled.

Signed-off-by: Kefu Chai <kefu...@scylladb.com>

---
diff --git a/include/seastar/util/print_safe.hh b/include/seastar/util/print_safe.hh
--- a/include/seastar/util/print_safe.hh
+++ b/include/seastar/util/print_safe.hh
@@ -21,7 +21,11 @@

#pragma once

+#include <seastar/util/concepts.hh>
#include <stdio.h>
+#if __cplusplus > 201703L
+#include <concepts>
+#endif

namespace seastar {

@@ -60,7 +64,7 @@ void print_safe(const char *str) noexcept {
// Fills a buffer with a zero-padded hexadecimal representation of an integer.
// For example, convert_zero_padded_hex_safe(buf, 4, uint16_t(12)) fills the buffer with "000c".
template<typename Integral>
-SEASTAR_CONCEPT( requires std::is_integral_v<Integral> )
+SEASTAR_CONCEPT( requires std::integral<Integral> )
void convert_zero_padded_hex_safe(char *buf, size_t bufsz, Integral n) noexcept {
const char *digits = "0123456789abcdef";
memset(buf, '0', bufsz);
@@ -76,9 +80,8 @@ void convert_zero_padded_hex_safe(char *buf, size_t bufsz, Integral n) noexcept
// For example, print_zero_padded_hex_safe(uint16_t(12)) prints "000c".
// Async-signal safe.
template<typename Integral>
+SEASTAR_CONCEPT ( requires std::signed_integral<Integral> )
void print_zero_padded_hex_safe(Integral n) noexcept {
- static_assert(std::is_integral<Integral>::value && !std::is_signed<Integral>::value, "Requires unsigned integrals");
-
char buf[sizeof(n) * 2];
convert_zero_padded_hex_safe(buf, sizeof(buf), n);
print_safe(buf, sizeof(buf));
@@ -88,10 +91,8 @@ void print_zero_padded_hex_safe(Integral n) noexcept {
// The argument bufsz is the maximum size of the buffer.
// For example, print_decimal_safe(buf, 16, 12) prints "12".
template<typename Integral>
-SEASTAR_CONCEPT( requires std::is_integral_v<Integral> )
+SEASTAR_CONCEPT( requires std::unsigned_integral<Integral> )
size_t convert_decimal_safe(char *buf, size_t bufsz, Integral n) noexcept {
- static_assert(std::is_integral<Integral>::value && !std::is_signed<Integral>::value, "Requires unsigned integrals");
-
char tmp[sizeof(n) * 3];
unsigned i = bufsz;
do {
Reply all
Reply to author
Forward
0 new messages