Skip to content

Commit

Permalink
Saving work of message hierarchy restructuring.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Oct 3, 2024
1 parent 80f9ffb commit 10eada0
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 95 deletions.
2 changes: 1 addition & 1 deletion demo/cc_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function (cc_plugin_demo)
# DemoPlugin.h
# DemoPlugin.cpp
# DemoProtocol.cpp
# DemoTransportMessage.cpp
DemoTransportMessage.cpp
DemoMessage.cpp
message/IntValues.cpp
message/EnumValues.cpp
Expand Down
1 change: 0 additions & 1 deletion demo/cc_plugin/DemoMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace cc_plugin

class DemoMessage : public cc_tools_qt::Message
{
using Base = cc_tools_qt::Message;
public:
DemoMessage();
virtual ~DemoMessage() noexcept;
Expand Down
22 changes: 4 additions & 18 deletions demo/cc_plugin/DemoTransportMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

#include "DemoTransportMessage.h"

#include <cassert>
#include "cc_tools_qt/property/field.h"

#include <QtCore/QVariantMap>

#include <cassert>

namespace cc = cc_tools_qt;

namespace demo
Expand Down Expand Up @@ -75,7 +77,7 @@ QVariantList createFieldsProperties()
props.append(cc::property::field::ForField<demo::VersionField>().name("VERSION").asMap());
props.append(cc::property::field::ForField<demo::DataField<> >().name("PAYLOAD").asMap());
props.append(cc::property::field::ForField<demo::ChecksumField>().name("CHECKSUM").asMap());
assert(props.size() == DemoTransportMessage::FieldIdx_NumOfValues);
assert(props.size() == DemoTransportProtMessage::FieldIdx_numOfValues);
return props;
}

Expand All @@ -87,22 +89,6 @@ const QVariantList& DemoTransportMessage::fieldsPropertiesImpl() const
return Props;
}

comms::ErrorStatus DemoTransportMessage::readImpl(ReadIterator& iter, std::size_t size)
{
static const auto ChecksumLen =
sizeof(demo::ChecksumField::ValueType);

size -= ChecksumLen;
auto es = doReadUntilAndUpdateLen<FieldIdx_Checksum>(iter, size);
if (es == comms::ErrorStatus::Success) {
size += ChecksumLen;
es = doReadFrom<FieldIdx_Checksum>(iter, size);
}

return es;
}


} // namespace cc_plugin

} // namespace demo
Expand Down
39 changes: 7 additions & 32 deletions demo/cc_plugin/DemoTransportMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,26 @@

#pragma once

#include "cc_tools_qt/cc_tools_qt.h"
#include "cc_tools_qt/ToolsTransportMessageBase.h"
#include "demo/DemoMessage.h"
#include "DemoTransportProtMessage.h"
#include "DemoMessage.h"
#include "DemoStack.h"

namespace demo
{

namespace cc_plugin
{

using DemoTransportMessageFields =
std::tuple<
demo::SyncField,
demo::LengthField,
demo::MsgIdField,
demo::VersionField,
demo::DataField<>,
demo::ChecksumField
>;


class DemoTransportMessage : public
cc_tools_qt::TransportMessageBase<
cc_plugin::DemoMessage,
DemoTransportMessageFields
cc_tools_qt::ToolsTransportMessageBase<
DemoTransportProtMessage,
DemoTransportMessage,
demo::cc_plugin::DemoMessage
>
{
public:
enum FieldIdx
{
FieldIdx_Sync,
FieldIdx_Len,
FieldIdx_Id,
FieldIdx_Version,
FieldIdx_Payload,
FieldIdx_Checksum,
FieldIdx_NumOfValues
};

static_assert(FieldIdx_NumOfValues == std::tuple_size<DemoTransportMessageFields>::value,
"Wrong indices");

protected:
virtual const QVariantList& fieldsPropertiesImpl() const override;
virtual comms::ErrorStatus readImpl(ReadIterator& iter, std::size_t size) override;
};

} // namespace cc_plugin
Expand Down
77 changes: 77 additions & 0 deletions demo/cc_plugin/DemoTransportProtMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// Copyright 2015 - 2019 (C). Alex Robenko. All rights reserved.
//

// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.


#pragma once

#include "cc_tools_qt/ToolsTransportProtMessageBase.h"
#include "demo/DemoMessage.h"
#include "demo/Stack.h"

namespace demo
{

namespace cc_plugin
{

using DemoTransportMessageFields =
std::tuple<
demo::SyncField,
demo::LengthField,
demo::MsgIdField,
demo::VersionField,
demo::DataField<>,
demo::ChecksumField
>;

class DemoTransportProtMessage : public
cc_tools_qt::ToolsTransportProtMessageBase<
demo::DemoMessage,
DemoTransportMessageFields,
DemoTransportProtMessage
>
{
using Base =
cc_tools_qt::ToolsTransportProtMessageBase<
demo::DemoMessage,
DemoTransportMessageFields,
DemoTransportProtMessage
>;
public:
COMMS_MSG_FIELDS_NAMES(sync, len, id, version, data, checksum);

template <typename TIter>
comms::ErrorStatus doRead(TIter& iter, std::size_t size)
{
static const auto ChecksumLen =
sizeof(demo::ChecksumField::ValueType);

size -= ChecksumLen;
auto es = doReadUntilAndUpdateLen<FieldIdx_checksum>(iter, size);
if (es == comms::ErrorStatus::Success) {
size += ChecksumLen;
es = doReadFrom<FieldIdx_checksum>(iter, size);
}

return es;
}
};

} // namespace cc_plugin

} // namespace demo

68 changes: 55 additions & 13 deletions lib/include/cc_tools_qt/ToolsMessageBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ namespace cc_tools_qt
/// @brief Helper class used to define protocol message class
/// in <b>CommsChampion Tools</b> plugin environment.
/// @tparam TProtMsg Type of the message class used for the plugin.
/// @tparam TActualMessage Type of the actual message class inheriting from
/// this one
/// @headerfile cc_tools_qt/MessageBase.h
/// @tparam TActualMsg Type of the actual message class inheriting from this one
/// @tparam TBase Base class that this class is expected to inherit. Expected to be cc_tools_qt::Message or derivative.
/// @headerfile cc_tools_qt/ToolMessageBase.h
template <typename TProtMsg, typename TActualMsg, typename TBase = cc_tools_qt::Message>
class ToolsMessageBase : public TBase
{
Expand Down Expand Up @@ -82,10 +82,14 @@ class ToolsMessageBase : public TBase
/// @brief Overriding polymorphic name retrieval functionality
virtual const char* nameImpl() const override
{
static_assert(comms::isMessageBase<TProtMsg>(), "TProtMsg is expected to be proper message");
static_assert(TProtMsg::hasCustomName(), "TProtMsg is expected to define message name");

return m_msg.doName();
using Tag =
std::conditional_t<
TProtMsg::hasCustomName(),
HasNameTag,
NoNameTag
>;

return nameInternal(Tag());
}

/// @brief Overriding polymorphic dispatch functionality.
Expand All @@ -105,12 +109,14 @@ class ToolsMessageBase : public TBase
/// @brief Overriding polymorphic retrieval of the numeric id
virtual qlonglong numericIdImpl() const override
{
static const bool IsNumeric =
std::is_enum<typename TProtMsg::MsgIdType>::value ||
std::is_integral<typename TProtMsg::MsgIdType>::value;

static_assert(IsNumeric, "Only numeric message IDs are supported");
return static_cast<qlonglong>(m_msg.doGetId());
using Tag =
std::conditional_t<
TProtMsg::hasGetId(),
HasIdTag,
NoIdTag
>;

return numericIdInternal(Tag());
}

/// @brief Overriding implementation to cc_tools_qt::Message::resetImpl()
Expand Down Expand Up @@ -166,6 +172,42 @@ class ToolsMessageBase : public TBase
}

private:
struct HasIdTag {};
struct NoIdTag {};
struct HasNameTag {};
struct NoNameTag {};

qlonglong numericIdInternal(HasIdTag) const
{
static const bool IsNumeric =
std::is_enum<typename TProtMsg::MsgIdType>::value ||
std::is_integral<typename TProtMsg::MsgIdType>::value;

static_assert(IsNumeric, "Only numeric message IDs are supported");
return static_cast<qlonglong>(m_msg.doGetId());
}

qlonglong numericIdInternal(NoIdTag) const
{
assert(false); // Should not be called
return static_cast<qlonglong>(0);
}

const char* nameInternal(HasNameTag) const
{
static_assert(comms::isMessageBase<TProtMsg>(), "TProtMsg is expected to be proper message");
static_assert(TProtMsg::hasCustomName(), "TProtMsg is expected to define message name");

return m_msg.doName();
}

const char* nameInternal(NoNameTag) const
{
assert(false); // Should not be called
static const char* NoName = "NO-NAME";
return NoName;
}

TProtMsg m_msg;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,34 @@

#pragma once

#include <cassert>
#include "cc_tools_qt/ToolsMessageBase.h"
#include "cc_tools_qt/ToolsMessageInterface.h"

#include <QtCore/QString>

#include "comms/comms.h"
#include "ProtocolMessageBase.h"
#include "cc_tools_qt.h"

namespace cc_tools_qt
{
#include <cassert>

namespace details
{

template <typename TMsgBase, typename TFields>
class TransportMessageImpl : public
comms::MessageBase<
TMsgBase,
comms::option::NoIdImpl,
comms::option::FieldsImpl<TFields>,
comms::option::MsgType<TransportMessageImpl<TMsgBase, TFields> >
>
namespace cc_tools_qt
{

};

} // namespace details

/// @brief Base class for @b TransportMessage definition in @b protocol
/// plugin.
/// @tparam TMessage Common interface class for @b protocol plugin.
/// @tparam TAllFields All transport fields difined by <b>protocol stack</b>.
/// @headerfile cc_tools_qt/TransportMessageBase.h
template <typename TMessage, typename TAllFields>
class TransportMessageBase : public
ProtocolMessageBase<
details::TransportMessageImpl<TMessage, TAllFields>,
TransportMessageBase<TMessage, TAllFields>
/// @tparam TProtMsg TODO
/// @tparam TActualMsg Type of the actual message class inheriting from this one
/// @tparam TBase Base class that this class is expected to inherit. Expected to be cc_tools_qt::Message or derivative.
/// @headerfile cc_tools_qt/ToolTransportMessageBase.h
template <typename TProtMsg, typename TActualMsg, typename TBase = cc_tools_qt::Message>
class ToolsTransportMessageBase : public
cc_tools_qt::ToolsMessageBase<
TProtMsg,
TActualMsg,
TBase
>
{
public:
/// @brief Destructor
virtual ~TransportMessageBase() noexcept = default;
virtual ~ToolsTransportMessageBase() noexcept = default;

protected:
/// @brief Overriding virtual cc_tools_qt::Message::nameImpl()
Expand Down
Loading

0 comments on commit 10eada0

Please sign in to comment.