libcruft-util/serialise/ops.hpp

51 lines
1.2 KiB
C++
Raw Normal View History

/*
* 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 <danny@nerdcruft.net>
*/
#pragma once
#include "./converter.hpp"
#include <cruft/util/view.hpp>
#include <cstddef>
namespace cruft::serialise {
template <typename ValueT>
cruft::view<std::byte*>
to_bytes [[nodiscard]] (cruft::view<std::byte*> dst, ValueT const &src)
{
return converter<ValueT>::to_bytes (dst, src);
}
template <typename HeadT, typename ...TailT>
cruft::view<std::byte*>
to_bytes [[nodiscard]] (cruft::view<std::byte*> dst, HeadT const &head, TailT&& ...tail)
{
dst = to_bytes (dst, head);
if constexpr (sizeof... (TailT))
dst = to_bytes (dst, std::forward<TailT> (tail)...);
return dst;
}
template <typename ValueT>
ValueT
from_bytes (cruft::view<std::byte*> &src)
{
return converter<ValueT>::from_bytes (src);
}
template <typename ...ArgsT>
std::size_t size (ArgsT &&...args)
{
return (converter<ArgsT>::size (args) + ... + 0uz);
}
}