Files
snipplib/include/snipplib/network/snplib_netproto.h

64 lines
1.8 KiB
C++

#pragma once
#include "../concepts/snplib_concepts.h"
#include "../utils/snplib_string.h"
/*
Various simple network session protocols
*/
namespace snplib
{
static constexpr char SNPLIB_DEFAULT_NETPROTO_STOP_SEQ[] = "\n";
template <const char* STOP_SEQ = SNPLIB_DEFAULT_NETPROTO_STOP_SEQ>
struct snplib_netproto_stopseq_t {
static constexpr std::string_view NETPROTO_STOP_SEQ{
snplib_char_array_size(STOP_SEQ) ? STOP_SEQ : SNPLIB_DEFAULT_NETPROTO_STOP_SEQ};
// returns 2-element range of the input messsage and stop-sequence
template <std::ranges::range OR, snplib_input_char_range_c IR>
requires snplib_char_range_c<std::ranges::range_value_t<OR>> // range of char ranges
OR toBytes(IR&& message)
{
OR res;
std::back_inserter(res) = std::forward<IR>(message);
std::back_inserter(res) = std::remove_cvref_t<IR>{NETPROTO_STOP_SEQ.begin(), NETPROTO_STOP_SEQ.end()};
return res;
}
// returns a copy of the input message + tralling stop-sequence
template <snplib_output_char_range_c OR, snplib_input_char_range_c IR>
OR toBytes(IR&& message)
{
OR res;
std::ranges::copy(std::forward<IR>(message), std::back_inserter(res));
std::ranges::copy(NETPROTO_STOP_SEQ, std::back_inserter(res));
return res;
}
template <snplib_input_char_range_c IR>
auto toBytes(IR&& message)
{
if constexpr (std::ranges::contiguous_range<IR>) {
return std::array{std::span<const char>{message}, std::span<const char>{NETPROTO_STOP_SEQ}};
} else {
return std::array{std::ranges::subrange{message}, std::ranges::subrange{NETPROTO_STOP_SEQ}};
}
}
template <std::ranges::range OR, std::ranges::range IR>
OR fromBytes(IR&& bytes)
{
}
};
} // namespace snplib