diff --git a/include/sp/Dispatcher.h b/include/sp/Dispatcher.h deleted file mode 100644 index 02a963f..0000000 --- a/include/sp/Dispatcher.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -/** - * \file PacketDispatcher.h - * \brief File containing the td::protocol::PacketDispatcher class - */ - -#include - -#include -#include - -namespace sp { -namespace protocol { - -/** - * \class Dispatcher - * \brief Class used to dispatch things - */ -template -class Dispatcher : private NonCopyable { - private: - std::map> m_Handlers; - - public: - /** - * \brief Constructor - */ - Dispatcher() {} - - /** - * \brief Dispatch a packet - * \param packet The packet to dispatch - */ - void Dispatch(const T& packet); - - /** - * \brief Register a packet handler - * \param type The packet type - * \param handler The packet handler - */ - void RegisterHandler(T_Enum type, T_Handler& handler); - - /** - * \brief Unregister a packet handler - * \param type The packet type - * \param handler The packet handler - */ - void UnregisterHandler(T_Enum type, T_Handler& handler); - - /** - * \brief Unregister a packet handler - * \param handler The packet handler - */ - void UnregisterHandler(T_Handler& handler); -}; - -} // namespace protocol -} // namespace td - -#include "Dispatcher.inl" \ No newline at end of file diff --git a/include/sp/Dispatcher.inl b/include/sp/Dispatcher.inl deleted file mode 100644 index c7e4a80..0000000 --- a/include/sp/Dispatcher.inl +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -namespace sp { -namespace protocol { - -template -void Dispatcher::Dispatch(const T& packet) { - T_Enum type = packet.GetType(); - for (auto* handler : m_Handlers[type]) - handler->Check(packet); -} - -template -void Dispatcher::RegisterHandler(T_Enum type, T_Handler& handler) { - auto found = std::find(m_Handlers[type].begin(), m_Handlers[type].end(), &handler); - if (found == m_Handlers[type].end()) - m_Handlers[type].push_back(&handler); -} - -template -void Dispatcher::UnregisterHandler(T_Enum type, T_Handler& handler) { - m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end()); -} - -template -void Dispatcher::UnregisterHandler(T_Handler& handler) { - for (auto& pair : m_Handlers) { - if (pair.second.empty()) - continue; - - auto type = pair.first; - - m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end()); - } -} - -} // namespace protocol -} // namespace td diff --git a/include/sp/Field.h b/include/sp/Field.h index 23861ec..fd8309a 100644 --- a/include/sp/Field.h +++ b/include/sp/Field.h @@ -8,8 +8,12 @@ template class Field { public: // Provide an access to the stored value - ValueType& GetValue(); - const ValueType& GetValue() const; + ValueType& GetValue() { + return m_Value; + } + const ValueType& GetValue() const { + return m_Value; + } // Read (deserialise) and update internal value void Read(DataBuffer& buffer) { diff --git a/include/sp/MessageBase.h b/include/sp/MessageBase.h index 40d5fab..e39c7e8 100644 --- a/include/sp/MessageBase.h +++ b/include/sp/MessageBase.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace sp { namespace option { @@ -100,6 +101,11 @@ class MessageImplFieldsBase : public TBase { return m_Fields; } + template + auto& GetField() { + return std::get(GetFields()).GetValue(); + } + private: TFields m_Fields; }; @@ -108,9 +114,9 @@ template class MessageImplFieldsReadBase : public TBase { protected: void ReadImpl(DataBuffer& buffer) override { - // Access fields via interface provided in previous chunk + //TODO: add endianess auto& allFields = TBase::GetFields(); - //... // read all the fields + tupleForEach(allFields, FieldReader{buffer}); } }; @@ -118,9 +124,9 @@ template class MessageImplFieldsWriteBase : public TBase { protected: void WriteImpl(DataBuffer& buffer) override { - // Access fields via interface provided in previous chunk + //TODO: add endianess auto& allFields = TBase::GetFields(); - //... // write all the fields + tupleForEach(allFields, FieldWriter{buffer}); } }; diff --git a/src/main.cpp b/src/main.cpp index cf67f56..7686ec7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ using MyMessage = sp::Message, // add id() opera sp::option::ReadOperations, // add read() operation sp::option::WriteOperations, // add write() operation sp::option::Handler, // add dispatch() operation - sp::option::ValidCheckInterface, // add valid() operation + sp::option::ValidCheckInterface, // add valid() operation sp::option::LittleEndian // use little endian for serialisation >; @@ -52,7 +52,19 @@ class MyHandler : public sp::GenericHandler { int main() { MyMessage::MsgIdType test; auto yes = std::make_unique>(); + auto& fields = yes->GetFields(); + + std::get<0>(fields) = 69; + std::get<1>(fields) = 42; + MyHandler handlerTest; yes->Dispatch(handlerTest); + sp::DataBuffer buffer; + yes->Write(buffer); + + auto yesyes = std::make_unique>(); + yesyes->Read(buffer); + std::cout << "Field 1 : " << yesyes->GetField<0>() << std::endl; + std::cout << "Field 2 : " << (unsigned) yesyes->GetField<1>() << std::endl; return 0; } \ No newline at end of file