/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2022, Danny Robson */ #pragma once #include #include namespace cruft::serialise { template struct converter { static cruft::view to_bytes [[nodiscard]] ( cruft::view dst, ValueT const &src ); static ValueT from_bytes (cruft::view &src); static std::size_t size (ValueT const&); }; template struct converter : public converter {}; template struct converter : public converter {}; template requires (std::is_trivial_v) struct converter { static cruft::view to_bytes [[nodiscard]] ( cruft::view dst, ValueT const &src ) { if (dst.size () < sizeof (ValueT)) throw std::bad_alloc (); return write (dst, src); } static ValueT from_bytes (cruft::view &src) { return read (src); } static std::size_t size (ValueT const&) { return sizeof (ValueT); } }; }