COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
ProtocolLayerDetails.h
1//
2// Copyright 2014 - 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 "comms/details/detect.h"
11
12namespace comms
13{
14
15namespace protocol
16{
17
18namespace details
19{
20
21template <typename T, bool THasImpl>
22struct ProtocolLayerHasFieldsImplHelper;
23
24template <typename T>
25struct ProtocolLayerHasFieldsImplHelper<T, true>
26{
27 static const bool Value = T::hasFields();
28};
29
30template <typename T>
31struct ProtocolLayerHasFieldsImplHelper<T, false>
32{
33 static const bool Value = false;
34};
35
36template <typename T>
37struct ProtocolLayerHasFieldsImpl
38{
39 static const bool Value =
40 ProtocolLayerHasFieldsImplHelper<T, comms::details::hasImplOptions<T>()>::Value;
41};
42
43template <class T>
44constexpr bool protocolLayerHasFieldsImpl()
45{
46 return ProtocolLayerHasFieldsImpl<T>::Value;
47}
48
49template <typename T, bool THasImpl>
50struct ProtocolLayerHasDoGetIdHelper;
51
52template <typename T>
53struct ProtocolLayerHasDoGetIdHelper<T, true>
54{
55 static const bool Value = T::hasStaticMsgId();
56};
57
58template <typename T>
59struct ProtocolLayerHasDoGetIdHelper<T, false>
60{
61 static const bool Value = false;
62};
63
64template <typename T>
65struct ProtocolLayerHasDoGetId
66{
67 static const bool Value =
68 ProtocolLayerHasDoGetIdHelper<T, comms::details::hasImplOptions<T>()>::Value;
69};
70
71template <typename T>
72constexpr bool protocolLayerHasDoGetId()
73{
74 return ProtocolLayerHasDoGetId<T>::Value;
75}
76
77template <class T, class R = void>
78struct ProtocolLayerEnableIfHasMsgPtr { using Type = R; };
79
80template <class T, class Enable = void>
81struct ProtocolLayerMsgPtr
82{
83 using Type = void;
84};
85
86template <class T>
87struct ProtocolLayerMsgPtr<T, typename ProtocolLayerEnableIfHasMsgPtr<typename T::MsgPtr>::Type>
88{
89 using Type = typename T::MsgPtr;
90};
91
92template<typename...>
93class MissingSizeRetriever
94{
95public:
96 MissingSizeRetriever(std::size_t& val) : value_(val) {}
97
98 void setValue(std::size_t val)
99 {
100 value_ = val;
101 }
102
103private:
104 std::size_t& value_;
105};
106
107template <typename T>
108struct IsMissingSizeRetrieverHelper
109{
110 static const bool Value = false;
111};
112
113template <typename... TParams>
114struct IsMissingSizeRetrieverHelper<MissingSizeRetriever<TParams...> >
115{
116 static const bool Value = true;
117};
118
119template <typename T>
120constexpr bool isMissingSizeRetriever()
121{
122 return IsMissingSizeRetrieverHelper<T>::Value;
123}
124
125template <typename TId>
126class MsgIdRetriever
127{
128public:
129 MsgIdRetriever(TId& val) : value_(val) {}
130
131 template <typename U>
132 void setValue(U&& val)
133 {
134 value_ = static_cast<TId>(val);
135 }
136
137private:
138 TId& value_;
139};
140
141template <typename T>
142struct IsMsgIdRetrieverHelper
143{
144 static const bool Value = false;
145};
146
147template <typename TId>
148struct IsMsgIdRetrieverHelper<MsgIdRetriever<TId> >
149{
150 static const bool Value = true;
151};
152
153template <typename T>
154constexpr bool isMsgIdRetriever()
155{
156 return IsMsgIdRetrieverHelper<T>::Value;
157}
158
159class MsgIndexRetriever
160{
161public:
162 MsgIndexRetriever(std::size_t& val) : value_(val) {}
163
164 void setValue(std::size_t val)
165 {
166 value_ = val;
167 }
168
169private:
170 std::size_t& value_;
171};
172
173template <typename T>
174struct IsMsgIndexRetrieverHelper
175{
176 static const bool Value = false;
177};
178
179template <>
180struct IsMsgIndexRetrieverHelper<MsgIndexRetriever>
181{
182 static const bool Value = true;
183};
184
185template <typename T>
186constexpr bool isMsgIndexRetriever()
187{
188 return IsMsgIndexRetrieverHelper<T>::Value;
189}
190
191template <typename TIter>
192class MsgPayloadRetriever
193{
194public:
195 MsgPayloadRetriever(TIter& iter, std::size_t& len) : iter_(iter), len_(len) {}
196
197 template <typename TOtherIter>
198 void setValue(TOtherIter iter, std::size_t len)
199 {
200 iter_ = static_cast<TIter>(iter);
201 len_ = len;
202 }
203
204private:
205 TIter& iter_;
206 std::size_t& len_;
207};
208
209template <typename TITer>
210struct IsMsgPayloadRetrieverHelper
211{
212 static const bool Value = false;
213};
214
215template <typename TIter>
216struct IsMsgPayloadRetrieverHelper<MsgPayloadRetriever<TIter> >
217{
218 static const bool Value = true;
219};
220
221template <typename T>
222constexpr bool isMsgPayloadRetriever()
223{
224 return IsMsgPayloadRetrieverHelper<T>::Value;
225}
226
227} // namespace details
228
229} // namespace protocol
230
231} // namespace comms
Main namespace for all classes / functions of COMMS library.