6#include <boat/detail/utility.hpp>
7#include <boost/functional/hash.hpp>
14struct blob_view : std::basic_string_view<std::byte> {
15 using std::basic_string_view<std::byte>::basic_string_view;
19struct blob : std::basic_string<std::byte> {
20 using std::basic_string<std::byte>::basic_string;
21 blob(std::byte
const*) =
delete;
22 operator blob_view()
const {
return {data(), size()}; }
27template <
class I,
class O>
28 requires std::same_as<std::remove_reference_t<I>, blob_view>
29decltype(
auto)
operator>>(I && in, O & out)
31 if constexpr (arithmetic<O>) {
32 check(in.size() >=
sizeof out,
"out of blob");
33 std::memcpy(&out, in.data(),
sizeof out);
34 in.remove_prefix(
sizeof out);
38 return std::forward<I>(in);
42template <
class O,
class I>
43 requires std::same_as<std::remove_reference_t<O>, blob>
44decltype(
auto)
operator<<(O && out, I
const& in)
46 if constexpr (arithmetic<I>)
47 out.append_range(std::as_bytes(as_span(in)));
49 static_cast<blob&
>(out) << in;
50 return std::forward<O>(out);
55 requires requires(T out) { in >> out; }
62template <arithmetic T>
64 requires(!mixed(std::endian::native))
66 check(!mixed(e),
"mixed endian");
69 if (std::endian::native != e)
80 using char_t = std::remove_cvref_t<
decltype(out)>::char_type;
81 auto os = std::basic_ostringstream<char_t>{};
82 os << std::uppercase << std::hex << std::setfill<char_t>(
'0');
83 for (
auto byte : in.
bytes)
84 os << std::setw(2) << static_cast<unsigned>(
byte);
85 return out << os.view();
92struct std::hash<
boat::blob> {
T get(blob_view &in)
Definition blob.hpp:54
blob_view(std::byte const *)=delete
friend auto hash_value(blob const &v)
Definition blob.hpp:23
blob(std::byte const *)=delete
Formats bytes as uppercase hex, two digits per byte.
Definition blob.hpp:75
blob_view bytes
Definition blob.hpp:76
friend auto & operator<<(ostream auto &out, hex const &in)
Definition blob.hpp:78
static auto operator()(boat::blob const &v)
Definition blob.hpp:93