Skip to content

Commit a7a3abb

Browse files
committed
📝 Macro comments
1 parent 6f68da5 commit a7a3abb

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Marlin/src/core/macros.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,18 @@
178178
#define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
179179
#define DO(W,C,V...) (_DO_N(W,C,NUM_ARGS(V),V))
180180

181-
// Macros to support option testing
181+
// Concatenate symbol names, without or with pre-expansion
182182
#define _CAT(a,V...) a##V
183183
#define CAT(a,V...) _CAT(a,V)
184184

185+
// Recognize "true" values: blank, 1, 0x1, true
185186
#define _ISENA_ ~,1
186187
#define _ISENA_1 ~,1
187188
#define _ISENA_0x1 ~,1
188189
#define _ISENA_true ~,1
189190
#define _ISENA(V...) IS_PROBE(V)
190191

192+
// Macros to evaluate simple option switches
191193
#define _ENA_1(O) _ISENA(CAT(_IS,CAT(ENA_, O)))
192194
#define _DIS_1(O) NOT(_ENA_1(O))
193195
#define ENABLED(V...) DO(ENA,&&,V)
@@ -198,14 +200,17 @@
198200
#define COUNT_ENABLED(V...) DO(ENA,+,V)
199201
#define MANY(V...) (COUNT_ENABLED(V) > 1)
200202

203+
// Ternary pre-compiler macros conceal non-emitted content from the compiler
201204
#define TERN(O,A,B) _TERN(_ENA_1(O),B,A) // OPTION ? 'A' : 'B'
202205
#define TERN0(O,A) _TERN(_ENA_1(O),0,A) // OPTION ? 'A' : '0'
203206
#define TERN1(O,A) _TERN(_ENA_1(O),1,A) // OPTION ? 'A' : '1'
204207
#define TERN_(O,A) _TERN(_ENA_1(O),,A) // OPTION ? 'A' : '<nul>'
205208
#define _TERN(E,V...) __TERN(_CAT(T_,E),V) // Prepend 'T_' to get 'T_0' or 'T_1'
206209
#define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
207210
#define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.
211+
#define IF_DISABLED(O,A) TERN(O,,A)
208212

213+
// Macros to conditionally emit array items and function arguments
209214
#define _OPTITEM(A...) A,
210215
#define OPTITEM(O,A...) TERN_(O,DEFER4(_OPTITEM)(A))
211216
#define _OPTARG(A...) , A
@@ -220,8 +225,6 @@
220225
#define SUM_TERN(O,B,A) ((B) PLUS_TERN0(O,A)) // ((B) (OPTION ? '+ (A)' : '<nul>'))
221226
#define DIFF_TERN(O,B,A) ((B) MINUS_TERN0(O,A)) // ((B) (OPTION ? '- (A)' : '<nul>'))
222227

223-
#define IF_DISABLED(O,A) TERN(O,,A)
224-
225228
// Macros to support pins/buttons exist testing
226229
#define PIN_EXISTS(PN) (defined(PN##_PIN) && PN##_PIN >= 0)
227230
#define _PINEX_1 PIN_EXISTS
@@ -233,20 +236,24 @@
233236
#define BUTTONS_EXIST(V...) DO(BTNEX,&&,V)
234237
#define ANY_BUTTON(V...) DO(BTNEX,||,V)
235238

239+
// Value helper macros
236240
#define WITHIN(N,L,H) ((N) >= (L) && (N) <= (H))
237241
#define ISEOL(C) ((C) == '\n' || (C) == '\r')
238242
#define NUMERIC(a) WITHIN(a, '0', '9')
239243
#define DECIMAL(a) (NUMERIC(a) || a == '.')
240244
#define HEXCHR(a) (NUMERIC(a) ? (a) - '0' : WITHIN(a, 'a', 'f') ? ((a) - 'a' + 10) : WITHIN(a, 'A', 'F') ? ((a) - 'A' + 10) : -1)
241245
#define NUMERIC_SIGNED(a) (NUMERIC(a) || (a) == '-' || (a) == '+')
242246
#define DECIMAL_SIGNED(a) (DECIMAL(a) || (a) == '-' || (a) == '+')
247+
248+
// Array shorthand
243249
#define COUNT(a) (sizeof(a)/sizeof(*a))
244250
#define ZERO(a) memset((void*)a,0,sizeof(a))
245251
#define COPY(a,b) do{ \
246252
static_assert(sizeof(a[0]) == sizeof(b[0]), "COPY: '" STRINGIFY(a) "' and '" STRINGIFY(b) "' types (sizes) don't match!"); \
247253
memcpy(&a[0],&b[0],_MIN(sizeof(a),sizeof(b))); \
248254
}while(0)
249255

256+
// Expansion of some code
250257
#define CODE_16( A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,...) A; B; C; D; E; F; G; H; I; J; K; L; M; N; O; P
251258
#define CODE_15( A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,...) A; B; C; D; E; F; G; H; I; J; K; L; M; N; O
252259
#define CODE_14( A,B,C,D,E,F,G,H,I,J,K,L,M,N,...) A; B; C; D; E; F; G; H; I; J; K; L; M; N
@@ -267,6 +274,7 @@
267274
#define _CODE_N(N,V...) CODE_##N(V)
268275
#define CODE_N(N,V...) _CODE_N(N,V)
269276

277+
// Expansion of some non-delimited content
270278
#define GANG_16(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,...) A B C D E F G H I J K L M N O P
271279
#define GANG_15(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,...) A B C D E F G H I J K L M N O
272280
#define GANG_14(A,B,C,D,E,F,G,H,I,J,K,L,M,N,...) A B C D E F G H I J K L M N
@@ -288,7 +296,7 @@
288296
#define GANG_N(N,V...) _GANG_N(N,V)
289297
#define GANG_N_1(N,K) _GANG_N(N,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K,K)
290298

291-
// Macros for initializing arrays
299+
// Expansion of some list items
292300
#define LIST_26(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
293301
#define LIST_25(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y
294302
#define LIST_24(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,...) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X

0 commit comments

Comments
 (0)