COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
DispatchMsgIdRetrieveHelper.h
1//
2// Copyright 2019 - 2024 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8#pragma once
9
10#include <type_traits>
11#include <limits>
12
13#include "comms/Message.h"
14#include "comms/MessageBase.h"
15#include "comms/util/Tuple.h"
19#include "comms/details/tag.h"
20
21namespace comms
22{
23
24namespace details
25{
26
27template <typename...>
28class DispatchMsgIdRetrieveHelper
29{
30public:
31
32 template <typename TMessage>
33 static typename TMessage::MsgIdParamType doGetId()
34 {
35 static_assert(TMessage::hasMsgIdType(), "Message interface class must define its id type");
36 static_assert(!std::is_same<Tag<TMessage>, NoIdTag<> >::value, "Must be able to retrieve ID");
37
38 return doGetIdInternal<TMessage>(Tag<TMessage>());
39 }
40
41private:
42 template <typename... TParams>
43 using HasStaticIdTag = comms::details::tag::Tag1<>;
44
45 template <typename... TParams>
46 using HasDynamicIdTag = comms::details::tag::Tag2<>;
47
48 template <typename... TParams>
49 using NoIdTag = comms::details::tag::Tag3<>;
50
51 template <typename TMessage, typename...>
52 using DynamicCheckTag =
53 typename comms::util::LazyShallowConditional<
54 comms::isMessage<TMessage>() && TMessage::hasGetId()
55 >::template Type<
56 HasDynamicIdTag,
57 NoIdTag
58 >;
59
60 template <typename TMessage>
61 using Tag =
62 typename comms::util::LazyShallowConditional<
63 messageHasStaticNumId<TMessage>()
64 >::template Type<
65 HasStaticIdTag,
66 DynamicCheckTag,
67 TMessage
68 >;
69
70 template <typename TMessage, typename... TParams>
71 static typename TMessage::MsgIdParamType doGetIdInternal(HasStaticIdTag<TParams...>)
72 {
73 static_assert(comms::isMessageBase<TMessage>(), "Must be actual message");
74 static_assert(messageHasStaticNumId<TMessage>(), "Message must define static numeric ID");
75
76 return TMessage::doGetId();
77 }
78
79 template <typename TMessage, typename... TParams>
80 static typename TMessage::MsgIdParamType doGetIdInternal(HasDynamicIdTag<TParams...>)
81 {
82 static_assert(comms::isMessage<TMessage>(), "Must be actual message");
83 static_assert(TMessage::hasGetId(), "Message interface must be able to return id polymorphically");
84
85 static const typename TMessage::MsgIdType Id = TMessage().getId();
86 return Id;
87 }
88};
89
90template <typename TMessage>
91auto dispatchMsgGetMsgId() -> decltype(DispatchMsgIdRetrieveHelper<>::template doGetId<TMessage>())
92{
93 return DispatchMsgIdRetrieveHelper<>::template doGetId<TMessage>();
94}
95
96} // namespace details
97
98} // namespace comms
Contains various compiler related definitions.
Provides common base class for the custom messages with default implementation.
Contains definition of Message object interface and various base classes for custom messages.
Contains various tuple type manipulation classes and functions.
Main namespace for all classes / functions of COMMS library.
Replacement to some types from standard type_traits.