COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
protocol_layers_access.h
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
8#pragma once
9
10#include "comms/details/macro_common.h"
11#include "comms/details/reverse_macro_args.h"
12
13#define COMMS_LAYER_TYPE_1 COMMS_EXPAND(::ThisLayer)
14#define COMMS_LAYER_TYPE_2 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_1
15#define COMMS_LAYER_TYPE_3 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_2
16#define COMMS_LAYER_TYPE_4 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_3
17#define COMMS_LAYER_TYPE_5 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_4
18#define COMMS_LAYER_TYPE_6 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_5
19#define COMMS_LAYER_TYPE_7 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_6
20#define COMMS_LAYER_TYPE_8 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_7
21#define COMMS_LAYER_TYPE_9 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_8
22#define COMMS_LAYER_TYPE_10 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_9
23#define COMMS_LAYER_TYPE_11 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_10
24#define COMMS_LAYER_TYPE_12 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_11
25#define COMMS_LAYER_TYPE_13 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_12
26#define COMMS_LAYER_TYPE_14 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_13
27#define COMMS_LAYER_TYPE_15 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_14
28#define COMMS_LAYER_TYPE_16 COMMS_EXPAND(::NextLayer) COMMS_LAYER_TYPE_15
29
30#define COMMS_DO_LAYER_TYPE_INTERNAL_(N, B_) typename B_ COMMS_EXPAND(COMMS_LAYER_TYPE_ ## N)
31#define COMMS_DO_LAYER_TYPE_INTERNAL(N, B_) COMMS_EXPAND(COMMS_DO_LAYER_TYPE_INTERNAL_(N, B_))
32#define COMMS_DO_LAYER_TYPE(B_, ...) \
33 COMMS_EXPAND(COMMS_DO_LAYER_TYPE_INTERNAL(COMMS_NUM_ARGS(__VA_ARGS__), B_))
34
35#define COMMS_LAYER_TYPE_ALIAS(c_, B_, n_) \
36 using COMMS_CONCATENATE(Layer_, n_) = COMMS_DO_LAYER_TYPE_INTERNAL(c_, B_);
37
38#define COMMS_LAYER_TYPE_ALIAS_1(B_, n_) COMMS_LAYER_TYPE_ALIAS(1, B_, n_)
39#define COMMS_LAYER_TYPE_ALIAS_2(B_, n_, ...) \
40 COMMS_LAYER_TYPE_ALIAS(2, B_, n_) \
41 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_1(B_, __VA_ARGS__))
42#define COMMS_LAYER_TYPE_ALIAS_3(B_, n_, ...) \
43 COMMS_LAYER_TYPE_ALIAS(3, B_, n_) \
44 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_2(B_, __VA_ARGS__))
45#define COMMS_LAYER_TYPE_ALIAS_4(B_, n_, ...) \
46 COMMS_LAYER_TYPE_ALIAS(4, B_, n_) \
47 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_3(B_, __VA_ARGS__))
48#define COMMS_LAYER_TYPE_ALIAS_5(B_, n_, ...) \
49 COMMS_LAYER_TYPE_ALIAS(5, B_, n_) \
50 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_4(B_, __VA_ARGS__))
51#define COMMS_LAYER_TYPE_ALIAS_6(B_, n_, ...) \
52 COMMS_LAYER_TYPE_ALIAS(6, B_, n_) \
53 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_5(B_, __VA_ARGS__))
54#define COMMS_LAYER_TYPE_ALIAS_7(B_, n_, ...) \
55 COMMS_LAYER_TYPE_ALIAS(7, B_, n_) \
56 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_6(B_, __VA_ARGS__))
57#define COMMS_LAYER_TYPE_ALIAS_8(B_, n_, ...) \
58 COMMS_LAYER_TYPE_ALIAS(8, B_, n_) \
59 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_7(B_, __VA_ARGS__))
60#define COMMS_LAYER_TYPE_ALIAS_9(B_, n_, ...) \
61 COMMS_LAYER_TYPE_ALIAS(9, B_, n_) \
62 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_8(B_, __VA_ARGS__))
63#define COMMS_LAYER_TYPE_ALIAS_10(B_, n_, ...) \
64 COMMS_LAYER_TYPE_ALIAS(10, B_, n_) \
65 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_9(B_, __VA_ARGS__))
66#define COMMS_LAYER_TYPE_ALIAS_11(B_, n_, ...) \
67 COMMS_LAYER_TYPE_ALIAS(11, B_, n_) \
68 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_10(B_, __VA_ARGS__))
69#define COMMS_LAYER_TYPE_ALIAS_12(B_, n_, ...) \
70 COMMS_LAYER_TYPE_ALIAS(12, B_, n_) \
71 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_11(B_, __VA_ARGS__))
72#define COMMS_LAYER_TYPE_ALIAS_13(B_, n_, ...) \
73 COMMS_LAYER_TYPE_ALIAS(13, B_, n_) \
74 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_12(B_, __VA_ARGS__))
75#define COMMS_LAYER_TYPE_ALIAS_14(B_, n_, ...) \
76 COMMS_LAYER_TYPE_ALIAS(14, B_, n_) \
77 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_13(B_, __VA_ARGS__))
78#define COMMS_LAYER_TYPE_ALIAS_15(B_, n_, ...) \
79 COMMS_LAYER_TYPE_ALIAS(15, B_, n_) \
80 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_14(B_, __VA_ARGS__))
81#define COMMS_LAYER_TYPE_ALIAS_16(B_, n_, ...) \
82 COMMS_LAYER_TYPE_ALIAS(16, B_, n_) \
83 COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_15(B_, __VA_ARGS__))
84
85#define COMMS_DO_LAYER_TYPE_ALIAS_INTERNAL_(N, B_, ...) COMMS_EXPAND(COMMS_LAYER_TYPE_ALIAS_ ## N(B_, __VA_ARGS__))
86#define COMMS_DO_LAYER_TYPE_ALIAS_INTERNAL(N, B_, ...) COMMS_EXPAND(COMMS_DO_LAYER_TYPE_ALIAS_INTERNAL_(N, B_, __VA_ARGS__))
87#define COMMS_DO_LAYER_TYPE_ALIAS(B_, ...) \
88 COMMS_EXPAND(COMMS_DO_LAYER_TYPE_ALIAS_INTERNAL(COMMS_NUM_ARGS(__VA_ARGS__), B_, __VA_ARGS__))
89
90#define COMMS_LAYER_CALL_1 COMMS_EXPAND(.thisLayer())
91#define COMMS_LAYER_CALL_2 COMMS_EXPAND(.nextLayer())
92#define COMMS_LAYER_CALL_3 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_2
93#define COMMS_LAYER_CALL_4 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_3
94#define COMMS_LAYER_CALL_5 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_4
95#define COMMS_LAYER_CALL_6 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_5
96#define COMMS_LAYER_CALL_7 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_6
97#define COMMS_LAYER_CALL_8 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_7
98#define COMMS_LAYER_CALL_9 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_8
99#define COMMS_LAYER_CALL_10 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_9
100#define COMMS_LAYER_CALL_11 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_10
101#define COMMS_LAYER_CALL_12 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_11
102#define COMMS_LAYER_CALL_13 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_12
103#define COMMS_LAYER_CALL_14 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_13
104#define COMMS_LAYER_CALL_15 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_14
105#define COMMS_LAYER_CALL_16 COMMS_EXPAND(.nextLayer()) COMMS_LAYER_CALL_15
106
107#define COMMS_DO_LAYER_CALL_INTERNAL_(N, B_) COMMS_EXPAND(B_) COMMS_EXPAND(COMMS_LAYER_CALL_ ## N)
108#define COMMS_DO_LAYER_CALL_INTERNAL(N, B_) COMMS_EXPAND(COMMS_DO_LAYER_CALL_INTERNAL_(N, B_))
109#define COMMS_DO_LAYER_CALL(B_, ...) \
110 COMMS_EXPAND(COMMS_DO_LAYER_CALL_INTERNAL(COMMS_NUM_ARGS(__VA_ARGS__), B_))
111
112#ifdef COMMS_MUST_DEFINE_BASE
113
114#define COMMS_ACCESS_LAYER_FUNC(c_, n_) \
115 COMMS_DO_LAYER_TYPE_INTERNAL(c_, Base)& COMMS_CONCATENATE(layer_, n_)()
116#define COMMS_ACCESS_LAYER_CONST_FUNC(c_, n_) \
117 const COMMS_DO_LAYER_TYPE_INTERNAL(c_, Base)& COMMS_CONCATENATE(layer_, n_)() const
118
119#else // #ifdef COMMS_MUST_DEFINE_BASE
120#define COMMS_ACCESS_LAYER_FUNC(c_, n_) FUNC_AUTO_REF_RETURN(COMMS_CONCATENATE(layer_, n_), decltype(COMMS_DO_LAYER_CALL_INTERNAL(c_, COMMS_EXPAND(comms::protocol::toProtocolLayerBase(*this)))))
121#define COMMS_ACCESS_LAYER_CONST_FUNC(c_, n_) FUNC_AUTO_REF_RETURN_CONST(COMMS_CONCATENATE(layer_, n_), decltype(COMMS_DO_LAYER_CALL_INTERNAL(c_, COMMS_EXPAND(comms::protocol::toProtocolLayerBase(*this)))))
122
123#endif // #ifdef COMMS_MUST_DEFINE_BASE
124
125#define COMMS_ACCESS_LAYER_ACC_FUNC(c_, n_) \
126 COMMS_ACCESS_LAYER_FUNC(c_, n_) {\
127 return COMMS_DO_LAYER_CALL_INTERNAL(c_, comms::protocol::toProtocolLayerBase(*this)); \
128 } \
129 COMMS_ACCESS_LAYER_CONST_FUNC(c_, n_) {\
130 return COMMS_DO_LAYER_CALL_INTERNAL(c_, comms::protocol::toProtocolLayerBase(*this)); \
131 }
132
133#define COMMS_ACCESS_LAYER_ACC_FUNC_1(n_) COMMS_ACCESS_LAYER_ACC_FUNC(1, n_)
134#define COMMS_ACCESS_LAYER_ACC_FUNC_2(n_, ...) \
135 COMMS_ACCESS_LAYER_ACC_FUNC(2, n_) \
136 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_1(__VA_ARGS__))
137#define COMMS_ACCESS_LAYER_ACC_FUNC_3(n_, ...) \
138 COMMS_ACCESS_LAYER_ACC_FUNC(3, n_) \
139 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_2(__VA_ARGS__))
140#define COMMS_ACCESS_LAYER_ACC_FUNC_4(n_, ...) \
141 COMMS_ACCESS_LAYER_ACC_FUNC(4, n_) \
142 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_3(__VA_ARGS__))
143#define COMMS_ACCESS_LAYER_ACC_FUNC_5(n_, ...) \
144 COMMS_ACCESS_LAYER_ACC_FUNC(5, n_) \
145 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_4(__VA_ARGS__))
146#define COMMS_ACCESS_LAYER_ACC_FUNC_6(n_, ...) \
147 COMMS_ACCESS_LAYER_ACC_FUNC(6, n_) \
148 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_5(__VA_ARGS__))
149#define COMMS_ACCESS_LAYER_ACC_FUNC_7(n_, ...) \
150 COMMS_ACCESS_LAYER_ACC_FUNC(7, n_) \
151 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_6(__VA_ARGS__))
152#define COMMS_ACCESS_LAYER_ACC_FUNC_8(n_, ...) \
153 COMMS_ACCESS_LAYER_ACC_FUNC(8, n_) \
154 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_7(__VA_ARGS__))
155#define COMMS_ACCESS_LAYER_ACC_FUNC_9(n_, ...) \
156 COMMS_ACCESS_LAYER_ACC_FUNC(9, n_) \
157 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_8(__VA_ARGS__))
158#define COMMS_ACCESS_LAYER_ACC_FUNC_10(n_, ...) \
159 COMMS_ACCESS_LAYER_ACC_FUNC(10, n_) \
160 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_9(__VA_ARGS__))
161#define COMMS_ACCESS_LAYER_ACC_FUNC_11(n_, ...) \
162 COMMS_ACCESS_LAYER_ACC_FUNC(11, n_) \
163 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_10(__VA_ARGS__))
164#define COMMS_ACCESS_LAYER_ACC_FUNC_12(n_, ...) \
165 COMMS_ACCESS_LAYER_ACC_FUNC(12, n_) \
166 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_11(__VA_ARGS__))
167#define COMMS_ACCESS_LAYER_ACC_FUNC_13(n_, ...) \
168 COMMS_ACCESS_LAYER_ACC_FUNC(13, n_) \
169 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_12(__VA_ARGS__))
170#define COMMS_ACCESS_LAYER_ACC_FUNC_14(n_, ...) \
171 COMMS_ACCESS_LAYER_ACC_FUNC(14, n_) \
172 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_13(__VA_ARGS__))
173#define COMMS_ACCESS_LAYER_ACC_FUNC_15(n_, ...) \
174 COMMS_ACCESS_LAYER_ACC_FUNC(15, n_) \
175 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_14(__VA_ARGS__))
176#define COMMS_ACCESS_LAYER_ACC_FUNC_16(n_, ...) \
177 COMMS_ACCESS_LAYER_ACC_FUNC(16, n_) \
178 COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_15(c_, __VA_ARGS__))
179
180
181#define COMMS_CHOOSE_ACCESS_LAYER_ACC_FUNC_(N, ...) COMMS_EXPAND(COMMS_ACCESS_LAYER_ACC_FUNC_ ## N(__VA_ARGS__))
182#define COMMS_CHOOSE_ACCESS_LAYER_ACC_FUNC(N, ...) COMMS_EXPAND(COMMS_CHOOSE_ACCESS_LAYER_ACC_FUNC_(N, __VA_ARGS__))
183#define COMMS_DO_ACCESS_LAYER_ACC_FUNC(...) \
184 COMMS_EXPAND(COMMS_CHOOSE_ACCESS_LAYER_ACC_FUNC(COMMS_NUM_ARGS(__VA_ARGS__), __VA_ARGS__))
185
186
187
188
189
190
191
192
193
194
195
196