diff --git a/net/adc_netmessage.h b/net/adc_netmessage.h index 335827b..346e4dd 100644 --- a/net/adc_netmessage.h +++ b/net/adc_netmessage.h @@ -35,8 +35,12 @@ concept adc_from_bytes_func_c = std::invocable && std::convertible template concept adc_netmsg_converter_c = requires(T t) { - { t.template serialize(std::declval()) } -> std::same_as; - { t.template deserialize(std::declval()) } -> std::same_as; + { + t.template serialize(std::declval()) + } -> std::same_as; + { + t.template deserialize(std::declval()) + } -> std::same_as; }; struct AdcTheSameTypeTag { @@ -45,6 +49,31 @@ struct AdcTheSameTypeTag { } // namespace traits +namespace utils +{ + +template OutR, + traits::adc_input_char_range InR, + traits::adc_input_char_range DelimT = std::string_view> +size_t adcSplitRangeToTokens(OutR& result, const InR& input, const DelimT& delim = std::string_view(" ")) +{ + decltype(std::ranges::split_view(input, delim)) vv; + + if constexpr (std::is_array_v) { + vv = input | std::views::split(std::string_view(delim)); + } else { + vv = input | std::views::split(delim); + } + + std::ranges::copy(vv, std::back_inserter(result)); + + return std::ranges::distance(vv.begin(), vv.end()); +} + +} // namespace utils + + template ByteStorageT, std::ranges::output_range> StorageSeqT> class AdcNetMessageInterface @@ -53,7 +82,7 @@ protected: ByteStorageT _byteStorage; StorageSeqT _storageSequence; - AdcNetMessageInterface() : _byteStorage(), _storageSequence({_byteStorage}) {}; + AdcNetMessageInterface() : _byteStorage(), _storageSequence({_byteStorage}){}; template AdcNetMessageInterface(const T& v, const Ts&... vs) : AdcNetMessageInterface() @@ -85,30 +114,61 @@ public: } - template + template void appendBytes(const T& v, const Ts&... vs) { if constexpr (std::is_array_v>) { appendBytes(std::string_view(v), vs...); } else { - std::ranges::copy(v, std::back_inserter(_byteStorage)); + if constexpr (traits::adc_input_char_range) { + std::ranges::copy(v, std::back_inserter(_byteStorage)); + } else if constexpr (traits::formattable) { + std::format_to(std::back_inserter(_byteStorage), v); + } else { + static_assert(false, "UNSUPPORTED TYPE!!!"); + } + updateState(); } + if constexpr (sizeof...(Ts)) { appendBytes(vs...); } } - template - void appendBytes(const T& v, const Ts&... vs) + template + void setBytes(const T& v, const Ts&... vs) { - std::format_to(std::back_inserter(_byteStorage), v); - - if constexpr (sizeof...(Ts)) { - appendBytes(vs...); - } + _byteStorage = ByteStorageT(); + appendBytes(v, vs...); } + + + // template + // void appendBytes(const T& v, const Ts&... vs) + // { + // if constexpr (std::is_array_v>) { + // appendBytes(std::string_view(v), vs...); + // } else { + // std::ranges::copy(v, std::back_inserter(_byteStorage)); + // } + // if constexpr (sizeof...(Ts)) { + // appendBytes(vs...); + // } + // } + + + // template + // void appendBytes(const T& v, const Ts&... vs) + // { + // std::format_to(std::back_inserter(_byteStorage), v); + + // if constexpr (sizeof...(Ts)) { + // appendBytes(vs...); + // } + // } + template void appendBytes(IterT begin, IterT end) { @@ -118,24 +178,24 @@ public: } - template - void setBytes(const T& v, const Ts&... vs) - { - _byteStorage = ByteStorageT(); - appendBytes(v, vs...); + // template + // void setBytes(const T& v, const Ts&... vs) + // { + // _byteStorage = ByteStorageT(); + // appendBytes(v, vs...); - updateState(); - } + // updateState(); + // } - template - void setBytes(const T& v, const Ts&... vs) - { - _byteStorage = ByteStorageT(); - appendBytes(v, vs...); + // template + // void setBytes(const T& v, const Ts&... vs) + // { + // _byteStorage = ByteStorageT(); + // appendBytes(v, vs...); - updateState(); - } + // updateState(); + // } template