Skip to content

Commit 2b96cd8

Browse files
MarcoFalkejanus
MarcoFalke
authored andcommitted
refactor: Print verbose serialize compiler error messages
1 parent 9f97eb0 commit 2b96cd8

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

Diff for: src/.clang-format

+2
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ SpacesInAngles: false
4343
SpacesInContainerLiterals: true
4444
SpacesInCStyleCastParentheses: false
4545
SpacesInParentheses: false
46+
BreakBeforeConceptDeclarations: Always
47+
RequiresExpressionIndentation: OuterScope
4648
Standard: c++20
4749
UseTab: Never

Diff for: src/serialize.h

+14-9
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_wri
273273
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
274274
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); }
275275
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
276-
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)); }
277-
template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
276+
template <typename Stream, BasicByte B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
277+
template <typename Stream, BasicByte B> void Serialize(Stream& s, Span<B> span) { s.write(AsBytes(span)); }
278278

279279
#ifndef CHAR_EQUALS_INT8
280280
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 =
290290
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
291291
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
292292
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
293-
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)); }
294-
template <typename Stream, typename B> void Unserialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); }
293+
template <typename Stream, BasicByte B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
294+
template <typename Stream, BasicByte B> void Unserialize(Stream& s, Span<B> span) { s.read(AsWritableBytes(span)); }
295295

296296
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
297297
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
755755
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
756756

757757

758-
759758
/**
760759
* If none of the specialized versions above matched, default to calling member function.
761760
*/
762-
template<typename Stream, typename T>
763-
inline void Serialize(Stream& os, const T& a)
761+
template <class T, class Stream>
762+
concept Serializable = requires(T a, Stream s) { a.Serialize(s); };
763+
template <typename Stream, typename T>
764+
requires Serializable<T, Stream>
765+
void Serialize(Stream& os, const T& a)
764766
{
765767
a.Serialize(os);
766768
}
767769

768-
template<typename Stream, typename T>
769-
inline void Unserialize(Stream& is, T&& a)
770+
template <class T, class Stream>
771+
concept Unserializable = requires(T a, Stream s) { a.Unserialize(s); };
772+
template <typename Stream, typename T>
773+
requires Unserializable<T, Stream>
774+
void Unserialize(Stream& is, T&& a)
770775
{
771776
a.Unserialize(is);
772777
}

Diff for: src/span.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#ifndef BGL_SPAN_H
66
#define BGL_SPAN_H
77

8+
#include <algorithm>
9+
#include <cassert>
10+
#include <cstddef>
11+
#include <span>
812
#include <type_traits>
913
#include <cstddef>
1014
#include <algorithm>
@@ -288,13 +292,11 @@ Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
288292
return AsWritableBytes(Span{std::forward<V>(v)});
289293
}
290294

291-
// Helper functions to safely cast to unsigned char pointers.
292-
inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; }
293-
inline unsigned char* UCharCast(unsigned char* c) { return c; }
294-
inline unsigned char* UCharCast(std::byte* c) { return (unsigned char*)c; }
295295
inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; }
296296
inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
297-
inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }
297+
// Helper concept for the basic byte types.
298+
template <typename B>
299+
concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
298300

299301
// Helper function to safely convert a Span to a Span<[const] unsigned char>.
300302
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()}; }

0 commit comments

Comments
 (0)