COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
message_check.h
Go to the documentation of this file.
1//
2// Copyright 2017 - 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
10
11#pragma once
12
13#include <type_traits>
14
15#include "comms/Assert.h"
16#include "comms/MessageBase.h"
17#include "comms/util/Tuple.h"
19
20namespace comms
21{
22
23namespace details
24{
25
26template <typename TMessage>
27using MessageStaticNumIdBoolType =
28 typename comms::util::LazyDeepConditional<
29 comms::isMessageBase<TMessage>()
30 >::template Type<
31 comms::util::MessageCheckHasStaticId,
32 comms::util::FalseType,
33 TMessage
34 >;
35
36template <typename...>
37struct MessageStaticNumIdCheckHelper
38{
39 template <typename TMessage>
40 constexpr bool operator()(bool value) const
41 {
42 return value && MessageStaticNumIdBoolType<TMessage>::value;
43 }
44};
45
46template <typename... TMessages>
47using AllMessagesHaveStaticNumIdBoolType =
48 std::integral_constant<
49 bool,
50 comms::util::tupleTypeAccumulate<std::tuple<TMessages...> >(true, MessageStaticNumIdCheckHelper<>())
51 >;
52
53template <typename TAllMessages>
54struct AllMessagesHaveStaticNumIdCheckHelper;
55
56template <typename... TMessages>
57struct AllMessagesHaveStaticNumIdCheckHelper<std::tuple<TMessages...> >
58{
59 static constexpr bool Value = AllMessagesHaveStaticNumIdBoolType<TMessages...>::value;
60};
61
62template <typename TAllMessages>
63constexpr bool allMessagesHaveStaticNumId()
64{
65 return AllMessagesHaveStaticNumIdCheckHelper<TAllMessages>::Value;
66}
67
68template <typename TMessage>
69constexpr bool messageHasStaticNumId()
70{
71 return MessageStaticNumIdBoolType<TMessage>::value;
72}
73
74template <bool TAllSorted, bool TMoreThanOne, typename...>
75class AllMessagesStrongSortedCheckHelper;
76
77template <bool TMoreThanOne, typename... TParams>
78class AllMessagesStrongSortedCheckHelper<false, TMoreThanOne, TParams...>
79{
80public:
81 template <typename...>
82 using Type = std::false_type;
83};
84
85template <typename... TParams>
86class AllMessagesStrongSortedCheckHelper<true, false, TParams...>
87{
88public:
89 template <typename...>
90 using Type = std::true_type;
91};
92
93template <typename... TParams>
94class AllMessagesStrongSortedCheckHelper<true, true, TParams...>
95{
96public:
97 template <typename TMsg1, typename TMsg2, typename... TRest>
98 using Type =
100 (TMsg1::staticMsgId() < TMsg2::staticMsgId())
101 >::template Type<
102 typename AllMessagesStrongSortedCheckHelper<true, (0U < sizeof...(TRest))>::template Type<TMsg2, TRest...>,
103 std::false_type
104 >;
105};
106
107template <bool TAllSorted, bool TMoreThanOne, typename...>
108class AllMessagesWeakSortedCheckHelper;
109
110template <bool TMoreThanOne, typename... TParams >
111class AllMessagesWeakSortedCheckHelper<false, TMoreThanOne, TParams...>
112{
113public:
114 template <typename... TRest>
115 using Type = std::false_type;
116};
117
118template <typename... TParams>
119class AllMessagesWeakSortedCheckHelper<true, false, TParams...>
120{
121public:
122 template <typename... TRest>
123 using Type = std::true_type;
124};
125
126template <typename... TParams>
127class AllMessagesWeakSortedCheckHelper<true, true, TParams...>
128{
129public:
130 template <typename TMsg1, typename TMsg2, typename... TRest>
131 using Type =
133 (TMsg1::staticMsgId() <= TMsg2::staticMsgId())
134 >::template Type<
135 typename AllMessagesWeakSortedCheckHelper<true, (0U < sizeof...(TRest))>::template Type<TMsg2, TRest...>,
136 std::false_type
137 >;
138};
139
140template <typename TAllMessages>
141struct AllMessagesStrongSortedCheckHelperWrap;
142
143template <typename... TAllMessages>
144struct AllMessagesStrongSortedCheckHelperWrap<std::tuple<TAllMessages...> >
145{
146 static constexpr bool Value =
147 AllMessagesStrongSortedCheckHelper<
148 (AllMessagesHaveStaticNumIdBoolType<TAllMessages...>::value),
149 (1U < sizeof...(TAllMessages))
150 >::template Type<TAllMessages...>::value;
151};
152
153template <typename TAllMessages>
154struct AllMessagesWeakSortedCheckHelperWrap;
155
156template <typename... TAllMessages>
157struct AllMessagesWeakSortedCheckHelperWrap<std::tuple<TAllMessages...> >
158{
159 static constexpr bool Value =
160 AllMessagesWeakSortedCheckHelper<
161 (AllMessagesHaveStaticNumIdBoolType<TAllMessages...>::value),
162 (1U < sizeof...(TAllMessages))
163 >::template Type<TAllMessages...>::value;
164};
165
166template <typename TAllMessages>
167constexpr bool allMessagesAreStrongSorted()
168{
169 return AllMessagesStrongSortedCheckHelperWrap<TAllMessages>::Value;
170}
171
172template <typename TAllMessages>
173constexpr bool allMessagesAreWeakSorted()
174{
175 return AllMessagesWeakSortedCheckHelperWrap<TAllMessages>::Value;
176}
177
178} // namespace details
179
180} // namespace comms
This file contains classes required for generic custom assertion functionality.
Provides common base class for the custom messages with default implementation.
Contains various tuple type manipulation classes and functions.
constexpr TValue tupleTypeAccumulate(const TValue &value, TFunc &&func)
Performs "accumulate" algorithm on every type of the tuple.
Definition Tuple.h:685
Main namespace for all classes / functions of COMMS library.
STL namespace.
Replacement to std::conditional.
Definition type_traits.h:28
Replacement to some types from standard type_traits.