P99
p99_defarg.h
Go to the documentation of this file.
1 /* This may look like nonsense, but it really is -*- mode: C; coding: utf-8 -*- */
2 /* */
3 /* Except for parts copied from previous work and as explicitly stated below, */
4 /* the authors and copyright holders for this work are as follows: */
5 /* (C) copyright 2010-2012 Jens Gustedt, INRIA, France */
6 /* (C) copyright 2012 William Morris */
7 /* */
8 /* This file is free software; it is part of the P99 project. */
9 /* */
10 /* Licensed under the Apache License, Version 2.0 (the "License"); */
11 /* you may not use this file except in compliance with the License. */
12 /* You may obtain a copy of the License at */
13 /* */
14 /* http://www.apache.org/licenses/LICENSE-2.0 */
15 /* */
16 /* Unless required by applicable law or agreed to in writing, software */
17 /* distributed under the License is distributed on an "AS IS" BASIS, */
18 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
19 /* See the License for the specific language governing permissions and */
20 /* limitations under the License. */
21 /* */
22 #ifndef P99_DEFARG_H_
23 # define P99_DEFARG_H_
24 
31 #include "p99_map.h"
32 #include P99_ADVANCE_ID
33 
49 #define P00_code_augmenting
50 
51 
77 #ifdef P00_DOXYGEN
78 # define P99_CALL_DEFARG(NAME, M, ...) NAME(__VA_ARGS__)
79 # define P99_CALL_DEFARG_LIST(NAME, M, ...) __VA_ARGS__
80 #else
81 P00_DOCUMENT_NUMBER_ARGUMENT(P99_CALL_DEFARG, 1)
82 # define P99_CALL_DEFARG(NAME, M, ...) NAME(P99_CALL_DEFARG_LIST(NAME, M, __VA_ARGS__))
83 
84 P00_DOCUMENT_NUMBER_ARGUMENT(P99_CALL_DEFARG_LIST, 1)
85 # define P99_CALL_DEFARG_LIST(NAME, M, ...) \
86 P99_IF_EQ(0,M) \
87 (__VA_ARGS__) \
88 (P99_IF_EMPTY(__VA_ARGS__) \
89  (P00_DEFARGS(NAME, M, P99_PASTE2(NAME,_defarg_0)())) \
90  (P00_DEFARGS(NAME, M, __VA_ARGS__)) \
91  )
92 #endif
93 
94 
193 P00_DOCUMENT_TYPE_ARGUMENT(P99_PROTOTYPE, 0)
194 P00_DOCUMENT_TYPE_ARGUMENT(P99_PROTOTYPE, 2)
195 
196 #ifdef P00_DOXYGEN
197 #define P99_PROTOTYPE(...)
198 #else
199 #define P00_PROTOTYPE(RT, NAME, ...) \
200  RT (NAME)(P99_IF_EMPTY(__VA_ARGS__)(void)(__VA_ARGS__)); \
201  typedef RT P99_CAT2(NAME, _prototype_ret); \
202  P99_TYPEDEFS(P99_CAT2(NAME, _prototype_), __VA_ARGS__)
203 
204 #define P99_PROTOTYPE(...) \
205 P99_IF_EQ_2(P99_NARG(__VA_ARGS__)) \
206 (P00_PROTOTYPE(__VA_ARGS__, void)) \
207 (P00_PROTOTYPE(__VA_ARGS__))
208 #endif
209 
210 #if P99_COMPILER & P99_COMPILER_CLANG
211 #define P00_INSTANTIATE(RT, NAME, ...) \
212 RT (*const P99_PASTE3(p00_, NAME, _pointer)[])(P99_IF_EMPTY(__VA_ARGS__)(void)(__VA_ARGS__)) = { NAME }; \
213 p00_instantiate RT (NAME)(P99_IF_EMPTY(__VA_ARGS__)(void)(__VA_ARGS__))
214 #else
215 #define P00_INSTANTIATE(RT, NAME, ...) \
216 p00_instantiate RT (NAME)(P99_IF_EMPTY(__VA_ARGS__)(void)(__VA_ARGS__))
217 #endif
218 
219 #ifdef P00_DOXYGEN
220 
241 #define P99_INSTANTIATE(RT, NAME, ...) RT NAME(__VA_ARGS__)
242 #else
243 #define P99_INSTANTIATE(...) \
244 P99_IF_EQ_2(P99_NARG(__VA_ARGS__)) \
245 (P00_INSTANTIATE(__VA_ARGS__, void)) \
246 (P00_INSTANTIATE(__VA_ARGS__))
247 #endif
248 
249 #define P00_EXPR_FUNCTION(NAME, X, N) \
250 P99_IF_EMPTY(X) \
251 () \
252 ( \
253  inline \
254  P99_PASTE3(NAME, _prototype_, N) P99_PASTE3(NAME, _defarg_, N)(void) { \
255  P99_PASTE3(NAME, _prototype_, N) p00_ret = (X); \
256  return p00_ret; \
257  } \
258 )
259 
260 #define P00_DAFE(NAME, X, N) \
261 P99_IF_EMPTY(X) \
262 (P99_MACRO_END(NAME, _boring_, N)) \
263 (P99_INSTANTIATE(P99_PASTE3(NAME, _prototype_, N), P99_PASTE3(NAME, _defarg_, N)))
264 
265 #define P00_DECLARE_DEFARG(NAME, N, ...) \
266 P99_FOR(NAME, N, P00_SER, P00_EXPR_FUNCTION, __VA_ARGS__) \
267 P99_MACRO_END(NAME, _declare_defarg)
268 
269 #ifdef P00_DOXYGEN
270 
289 #define P99_DECLARE_DEFARG(NAME, ...)
290 #else
291 #define P99_DECLARE_DEFARG(NAME, ...) P00_DECLARE_DEFARG(NAME, P99_NARG(__VA_ARGS__), __VA_ARGS__)
292 #endif
293 
294 #define P00_DEFINE_DEFARG(NAME, N, ...) \
295  P99_FOR(NAME, N, P00_SEP, P00_DAFE, __VA_ARGS__)
296 
297 #ifdef P00_DOXYGEN
298 
304 #define P99_DEFINE_DEFARG(NAME, ...)
305 #else
306 #define P99_DEFINE_DEFARG(NAME, ...) P00_DEFINE_DEFARG(NAME, P99_NARG(__VA_ARGS__), __VA_ARGS__)
307 #endif
308 
309 
310 #define P00_DARG(NAME, X, N) P99_IF_EMPTY(X)(P99_PASTE3(NAME, _defarg_, N)())(X)
311 #define P00__DEFARGS(NAME, N, ...) P99_FOR(NAME, N, P00_SEQ, P00_DARG, __VA_ARGS__)
312 #define P00_DEFARGS(NAME, N, ...) P00__DEFARGS(NAME, N, P99_IF_LT(P99_NARG(__VA_ARGS__),N) (__VA_ARGS__, P99_DUPL(P99_MINUS(N,P99_NARG(__VA_ARGS__)),)) (__VA_ARGS__))
313 
314 
318 #define P99_DEFARG_DOCU(NAME) \
319  \
320  \
321 
324 #define P00_VAARG_0(NAME, T) NAME
325 #define P00_VAARG_1(NAME, T) T
326 
327 #define P00_VAARG(NAMET, X, I) P99_RVAL(P00_VAARG_1 NAMET, P99_IF_EMPTY(X)(P99_PASTE2(P00_VAARG_0 NAMET, _defarg)())(X))
328 
329 #define P00_CALL_VA_ARG(NAME, T, ...) P99_FOR((NAME, T), P99_NARG(__VA_ARGS__), P00_SEQ, P00_VAARG, __VA_ARGS__)
330 
331 #ifdef DOXYGEN
332 
424 #define P99_CALL_VA_ARG(NAME, M, T, ...) NAME(__VA_ARGS__)
425 #else
426 P00_DOCUMENT_NUMBER_ARGUMENT(P99_CALL_VA_ARG, 1)
427 #define P99_CALL_VA_ARG(NAME, M, T, ...) \
428 P99_IF_GT(P99_NARG(__VA_ARGS__), M) \
429 (NAME(P00__DEFARGS(NAME, M, P99_SELS(M, __VA_ARGS__)), P00_CALL_VA_ARG(NAME, T, P99_SKP(M, __VA_ARGS__)))) \
430 (P99_CALL_DEFARG(NAME, M, __VA_ARGS__))
431 #endif
432 
439 #endif /* !P99_DEFARG_H_ */
p99_map.h
macros to produce lists of statements or declarations.
P99_PROTOTYPE
#define P99_PROTOTYPE(...)
Define the prototype of function NAME.
Definition: p99_defarg.h:197
P99_CALL_VA_ARG
#define P99_CALL_VA_ARG(NAME, M, T,...)
Assure type safety for variadic functions.
Definition: p99_defarg.h:424
P99_CALL_DEFARG_LIST
#define P99_CALL_DEFARG_LIST(NAME, M,...)
Expand an argument list with default arguments.
P99_CALL_DEFARG
#define P99_CALL_DEFARG(NAME, M,...)
Define a replacement macro for functions that can provide default arguments to the underlying real fu...