From 77356ce7497dffc382d113a73b5c2e047e76a189 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 4 Mar 2025 22:45:50 +0100 Subject: [PATCH] better print override --- include/sp/common/Reflection.h | 38 ++++++++++++- include/sp/common/Templates.h | 45 +++++++++++++++ include/sp/common/VarInt.h | 12 ++-- include/sp/protocol/MessagePrinter.h | 55 +++++++++++++++++++ .../sp/protocol/message/MessagePrinterImpl.h | 27 ++------- src/sp/common/VarInt.cpp | 5 -- 6 files changed, 147 insertions(+), 35 deletions(-) create mode 100644 include/sp/protocol/MessagePrinter.h diff --git a/include/sp/common/Reflection.h b/include/sp/common/Reflection.h index 0fc4150..d0fcc4c 100644 --- a/include/sp/common/Reflection.h +++ b/include/sp/common/Reflection.h @@ -6,7 +6,7 @@ namespace sp { template -std::string GetClassName(const T& a_Value) { +std::string GetBasicClassName(const T& a_Value) { int status; char* demangled = abi::__cxa_demangle(typeid(a_Value).name(), 0, 0, &status); if (status != 0) @@ -15,7 +15,7 @@ std::string GetClassName(const T& a_Value) { } template -std::string GetClassName() { +std::string GetBasicClassName() { int status; char* demangled = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status); if (status != 0) @@ -23,4 +23,38 @@ std::string GetClassName() { return std::string(demangled); } +template +struct Reflector { + static std::string GetClassName() { + return GetBasicClassName(); + } +}; + +template <> +struct Reflector { + static std::string GetClassName() { + return "std::string"; + } +}; + +template +struct Reflector> { + static std::string GetClassName(); +}; + +template +struct Reflector> { + static std::string GetClassName(); +}; + +template +std::string Reflector>::GetClassName() { + return "std::vector<" + Reflector::GetClassName() + ">"; +} + +template +std::string Reflector>::GetClassName() { + return "std::map<" + Reflector::GetClassName() + ", " + Reflector::GetClassName() + ">"; +} + } // namespace sp diff --git a/include/sp/common/Templates.h b/include/sp/common/Templates.h index c7b2875..fefd2f2 100644 --- a/include/sp/common/Templates.h +++ b/include/sp/common/Templates.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace sp { @@ -50,4 +51,48 @@ void TupleForEach(TFunc&& func, TTuple&& tuple) { details::TupleForEachHelper::Exec(std::forward(tuple), std::forward(func)); } + + + + + + + + +namespace details { + +template +struct is_mappish_impl : std::false_type {}; + +template +struct is_mappish_impl()[std::declval()])>> : std::true_type {}; + +template +struct is_vectish_impl : std::false_type {}; + +template +struct is_vectish_impl()[std::declval()])>> + : std::true_type {}; + +template +struct is_pairish_impl : std::false_type {}; + +template +struct is_pairish_impl()[std::declval()])>> + : std::true_type {}; + +template +using is_general_t = std::integral_constant || (!std::is_same_v && !std::is_same_v && !std::is_arithmetic_v && + !is_pairish_impl::value && !is_mappish_impl::value && !is_vectish_impl::value))>; + +template +using is_primitive = + std::integral_constant || std::is_same_v || std::is_arithmetic_v>; + +} // namespace details + } // namespace sp diff --git a/include/sp/common/VarInt.h b/include/sp/common/VarInt.h index 91e2673..df194f1 100644 --- a/include/sp/common/VarInt.h +++ b/include/sp/common/VarInt.h @@ -7,7 +7,7 @@ #include #include -#include +#include namespace sp { @@ -56,11 +56,11 @@ class VarInt { * \param var The variable integer to deserialize */ friend DataBuffer& operator>>(DataBuffer& in, VarInt& var); - - /** - * \brief overriding stream operator - */ - friend std::ostream& operator<<(std::ostream& a_Stream, const sp::VarInt& a_VarInt); }; +template<> +inline std::string PrintData(const VarInt& a_VarInt) { + return PrintData(a_VarInt.GetValue()); +} + } // namespace sp diff --git a/include/sp/protocol/MessagePrinter.h b/include/sp/protocol/MessagePrinter.h new file mode 100644 index 0000000..5faa02d --- /dev/null +++ b/include/sp/protocol/MessagePrinter.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include +#include + +namespace sp { + +template ::value, bool> = true> +inline std::string PrintData(const T& a_Data); + +template ::value, bool> = true> +inline std::string PrintData(T a_Data) { + return std::to_string(a_Data); +} + +template <> +inline std::string PrintData(const std::string& a_Data) { + return "\"" + a_Data + "\""; +} + +template +std::string PrintData(const std::pair& a_Data); + +template +std::string PrintData(const std::map& a_Data); + +template +std::string PrintData(const std::vector& a_Data); + +template +std::string PrintData(const std::pair& a_Data) { + return "{" + PrintData(a_Data.first) + " => " + PrintData(a_Data.second) + "}"; +} + +template +std::string PrintData(const std::map& a_Data) { + std::string result = "{"; + for (const auto& pair : a_Data) { + result += PrintData(pair) + ", "; + } + return result.substr(0, result.size() - 2) + "}"; +} + +template +std::string PrintData(const std::vector& a_Data) { + std::string result = "{"; + for (const T& value : a_Data) { + result += PrintData(value) + ", "; + } + return result.substr(0, result.size() - 2) + "}"; +} + +} // namespace sp \ No newline at end of file diff --git a/include/sp/protocol/message/MessagePrinterImpl.h b/include/sp/protocol/message/MessagePrinterImpl.h index 2637735..10c82a4 100644 --- a/include/sp/protocol/message/MessagePrinterImpl.h +++ b/include/sp/protocol/message/MessagePrinterImpl.h @@ -1,8 +1,8 @@ #pragma once -#include -#include #include +#include +#include namespace sp { namespace details { @@ -31,37 +31,20 @@ struct IdPrinter, TOptions...> { } }; -template -std::string PrintData(const T& a_Data) { - std::stringstream sStream; - sStream << a_Data; - return sStream.str(); -} - -template <> -std::string PrintData(const char& a_Data) { - return std::to_string(a_Data); -} - -template <> -std::string PrintData(const unsigned char& a_Data) { - return std::to_string(a_Data); -} - template std::string PrintFields(const std::tuple& a_Fields); template struct FieldPrinter { static std::string PrintField(const sp::Field& a_Field) { - return GetClassName() + "=" + PrintData(a_Field.GetValue()); + return Reflector::GetClassName() + "=" + PrintData(a_Field.GetValue()); } }; template struct FieldPrinter, IAlignment> { static std::string PrintField(const Field, IAlignment>& a_Field) { - return "BitField<" + GetClassName() + ">[" + PrintFields(a_Field.GetValue().GetFields()) + "]"; + return "BitField<" + Reflector::GetClassName() + ">[" + PrintFields(a_Field.GetValue().GetFields()) + "]"; } }; @@ -80,7 +63,7 @@ std::string PrintFields(const std::tuple& a_Fields) { template std::string PrintMessage(const MessageBase& a_Message) { - return sp::GetClassName(a_Message) + sp::details::IdPrinter::PrintMessageId() + "[" + + return sp::GetBasicClassName(a_Message) + sp::details::IdPrinter::PrintMessageId() + "[" + sp::details::PrintFields(a_Message.GetFields()) + "]"; } diff --git a/src/sp/common/VarInt.cpp b/src/sp/common/VarInt.cpp index 4e6ea27..a02bf6f 100644 --- a/src/sp/common/VarInt.cpp +++ b/src/sp/common/VarInt.cpp @@ -49,9 +49,4 @@ DataBuffer& operator>>(DataBuffer& in, VarInt& var) { return in; } -std::ostream& operator<<(std::ostream& a_Stream, const sp::VarInt& a_VarInt) { - a_Stream << a_VarInt.GetValue(); - return a_Stream; -} - } // namespace sp