aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>2023-12-11 22:33:56 +0100
committerMarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>2023-12-15 15:20:54 +0100
commitfa898e6836a8fc2c7b6c8c15ad21818b16a89863 (patch)
tree67528e7f1366e6addf601d095c8c891e7758c304 /src
parent9776186e9f8ee00f912fc9d0ee8e634bb9401bfe (diff)
refactor: Print verbose serialize compiler error messages
Diffstat (limited to 'src')
-rw-r--r--src/.clang-format2
-rw-r--r--src/serialize.h23
-rw-r--r--src/span.h6
3 files changed, 21 insertions, 10 deletions
diff --git a/src/.clang-format b/src/.clang-format
index 2e5d5c6449..f20e5ee2d4 100644
--- a/src/.clang-format
+++ b/src/.clang-format
@@ -43,5 +43,7 @@ SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
+BreakBeforeConceptDeclarations: Always
+RequiresExpressionIndentation: OuterScope
Standard: c++20
UseTab: Never
diff --git a/src/serialize.h b/src/serialize.h
index 263b781f21..bbe6f54682 100644
--- a/src/serialize.h
+++ b/src/serialize.h
@@ -273,8 +273,8 @@ template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_wri
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); }
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
-template <typename Stream, typename B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { (void)/* force byte-type */UCharCast(a.data()); s.write(MakeByteSpan(a)); }
-template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
+template <typename Stream, BasicByte B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
+template <typename Stream, BasicByte B> void Serialize(Stream& s, Span<B> span) { s.write(AsBytes(span)); }
#ifndef CHAR_EQUALS_INT8
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
@@ -290,8 +290,8 @@ template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a =
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
-template <typename Stream, typename B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { (void)/* force byte-type */UCharCast(a.data()); s.read(MakeWritableByteSpan(a)); }
-template <typename Stream, typename B> void Unserialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); }
+template <typename Stream, BasicByte B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
+template <typename Stream, BasicByte B> void Unserialize(Stream& s, Span<B> span) { s.read(AsWritableBytes(span)); }
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
@@ -755,18 +755,23 @@ template<typename Stream, typename T> void Serialize(Stream& os, const std::uniq
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
-
/**
* If none of the specialized versions above matched, default to calling member function.
*/
-template<typename Stream, typename T>
-inline void Serialize(Stream& os, const T& a)
+template <class T, class Stream>
+concept Serializable = requires(T a, Stream s) { a.Serialize(s); };
+template <typename Stream, typename T>
+ requires Serializable<T, Stream>
+void Serialize(Stream& os, const T& a)
{
a.Serialize(os);
}
-template<typename Stream, typename T>
-inline void Unserialize(Stream& is, T&& a)
+template <class T, class Stream>
+concept Unserializable = requires(T a, Stream s) { a.Unserialize(s); };
+template <typename Stream, typename T>
+ requires Unserializable<T, Stream>
+void Unserialize(Stream& is, T&& a)
{
a.Unserialize(is);
}
diff --git a/src/span.h b/src/span.h
index 2e8da27cde..2c27a54fc7 100644
--- a/src/span.h
+++ b/src/span.h
@@ -8,6 +8,7 @@
#include <algorithm>
#include <cassert>
#include <cstddef>
+#include <span>
#include <type_traits>
#ifdef DEBUG
@@ -283,13 +284,16 @@ Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
return AsWritableBytes(Span{std::forward<V>(v)});
}
-// Helper functions to safely cast to unsigned char pointers.
+// Helper functions to safely cast basic byte pointers to unsigned char pointers.
inline unsigned char* UCharCast(char* c) { return reinterpret_cast<unsigned char*>(c); }
inline unsigned char* UCharCast(unsigned char* c) { return c; }
inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast<unsigned char*>(c); }
inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast<const unsigned char*>(c); }
inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }
+// Helper concept for the basic byte types.
+template <typename B>
+concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
// Helper function to safely convert a Span to a Span<[const] unsigned char>.
template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; }