COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
FrameLayerDetails.h
1//
2// Copyright 2014 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#pragma once
11
12#include "comms/details/detect.h"
13
14#include <cstddef>
15#include <type_traits>
16
17namespace comms
18{
19
20namespace frame
21{
22
23namespace details
24{
25
26template <typename T, bool THasImpl>
27struct FrameLayerHasFieldsImplHelper;
28
29template <typename T>
30struct FrameLayerHasFieldsImplHelper<T, true>
31{
32 static const bool Value = T::hasFields();
33};
34
35template <typename T>
36struct FrameLayerHasFieldsImplHelper<T, false>
37{
38 static const bool Value = false;
39};
40
41template <typename T>
42struct FrameLayerHasFieldsImpl
43{
44 static const bool Value =
45 FrameLayerHasFieldsImplHelper<T, comms::details::hasImplOptions<T>()>::Value;
46};
47
48template <class T>
49constexpr bool frameLayerHasFieldsImpl()
50{
51 return FrameLayerHasFieldsImpl<T>::Value;
52}
53
54template <typename T, bool THasImpl>
55struct FrameLayerHasDoGetIdHelper;
56
57template <typename T>
58struct FrameLayerHasDoGetIdHelper<T, true>
59{
60 static const bool Value = T::hasDoGetId();
61};
62
63template <typename T>
64struct FrameLayerHasDoGetIdHelper<T, false>
65{
66 static const bool Value = false;
67};
68
69template <typename T>
70struct FrameLayerHasDoGetId
71{
72 static const bool Value =
73 FrameLayerHasDoGetIdHelper<T, comms::details::hasImplOptions<T>()>::Value;
74};
75
76template <typename T>
77constexpr bool frameLayerHasDoGetId()
78{
79 return FrameLayerHasDoGetId<T>::Value;
80}
81
82template <class T, class R = void>
83struct FrameLayerEnableIfHasMsgPtr { using Type = R; };
84
85template <class T, class Enable = void>
86struct FrameLayerMsgPtr
87{
88 using Type = void;
89};
90
91template <class T>
92struct FrameLayerMsgPtr<T, typename FrameLayerEnableIfHasMsgPtr<typename T::MsgPtr>::Type>
93{
94 using Type = typename T::MsgPtr;
95};
96
97template<typename...>
98class MissingSizeRetriever
99{
100public:
101 MissingSizeRetriever(std::size_t& val) : m_value(val) {}
102
103 void setValue(std::size_t val)
104 {
105 m_value = val;
106 }
107
108private:
109 std::size_t& m_value;
110};
111
112template <typename T>
113struct IsMissingSizeRetrieverHelper
114{
115 static const bool Value = false;
116};
117
118template <typename... TParams>
119struct IsMissingSizeRetrieverHelper<MissingSizeRetriever<TParams...> >
120{
121 static const bool Value = true;
122};
123
124template <typename T>
125constexpr bool isMissingSizeRetriever()
126{
127 return IsMissingSizeRetrieverHelper<T>::Value;
128}
129
130template <typename TId>
131class MsgIdRetriever
132{
133public:
134 MsgIdRetriever(TId& val) : m_value(val) {}
135
136 template <typename U>
137 void setValue(U&& val)
138 {
139 m_value = static_cast<TId>(val);
140 }
141
142private:
143 TId& m_value;
144};
145
146template <typename T>
147struct IsMsgIdRetrieverHelper
148{
149 static const bool Value = false;
150};
151
152template <typename TId>
153struct IsMsgIdRetrieverHelper<MsgIdRetriever<TId> >
154{
155 static const bool Value = true;
156};
157
158template <typename T>
159constexpr bool isMsgIdRetriever()
160{
161 return IsMsgIdRetrieverHelper<T>::Value;
162}
163
164class MsgIndexRetriever
165{
166public:
167 MsgIndexRetriever(std::size_t& val) : m_value(val) {}
168
169 void setValue(std::size_t val)
170 {
171 m_value = val;
172 }
173
174private:
175 std::size_t& m_value;
176};
177
178template <typename T>
179struct IsMsgIndexRetrieverHelper
180{
181 static const bool Value = false;
182};
183
184template <>
185struct IsMsgIndexRetrieverHelper<MsgIndexRetriever>
186{
187 static const bool Value = true;
188};
189
190template <typename T>
191constexpr bool isMsgIndexRetriever()
192{
193 return IsMsgIndexRetrieverHelper<T>::Value;
194}
195
196template <typename... TRetrievers>
197struct MsgIndexRetrieverDetector;
198
199template <typename T, typename... TRetrievers>
200struct MsgIndexRetrieverDetector<T, TRetrievers...>
201{
202 static const bool Detected = isMsgIndexRetriever<typename std::decay<T>::type>() || MsgIndexRetrieverDetector<TRetrievers...>::Detected;
203};
204
205template <typename... TRetrievers>
206constexpr bool hasMsgIndexRetriever()
207{
208 return MsgIndexRetrieverDetector<TRetrievers...>::Detected;
209}
210
211template <>
212struct MsgIndexRetrieverDetector<>
213{
214 static const bool Detected = false;
215};
216
217template <typename TIter>
218class MsgPayloadRetriever
219{
220public:
221 MsgPayloadRetriever(TIter& iter, std::size_t& len) : m_iter(iter), m_len(len) {}
222
223 template <typename TOtherIter>
224 void setValue(TOtherIter iter, std::size_t len)
225 {
226 m_iter = static_cast<TIter>(iter);
227 m_len = len;
228 }
229
230private:
231 TIter& m_iter;
232 std::size_t& m_len;
233};
234
235template <typename TITer>
236struct IsMsgPayloadRetrieverHelper
237{
238 static const bool Value = false;
239};
240
241template <typename TIter>
242struct IsMsgPayloadRetrieverHelper<MsgPayloadRetriever<TIter> >
243{
244 static const bool Value = true;
245};
246
247template <typename T>
248constexpr bool isMsgPayloadRetriever()
249{
250 return IsMsgPayloadRetrieverHelper<T>::Value;
251}
252
253} // namespace details
254
255} // namespace frame
256
257} // namespace comms
Main namespace for all classes / functions of COMMS library.