blob: 74345f14376b35d2ab863975734ca86296c01dab [file] [log] [blame]
Wyatt Hepleree3e02f2019-12-05 10:52:31 -08001.. _chapter-pw-preprocessor:
Alexei Frolovc10c8122019-11-01 16:31:19 -07002
3.. default-domain:: cpp
4
5.. highlight:: sh
6
Wyatt Heplerb82f9952019-11-25 13:56:31 -08007---------------
8pw_preprocessor
9---------------
Alexei Frolovc10c8122019-11-01 16:31:19 -070010The preprocessor module provides various helpful preprocessor macros.
11
12Compatibility
13=============
14C and C++
15
Alexei Frolovc10c8122019-11-01 16:31:19 -070016Headers
17=======
18The preprocessor module provides several headers.
19
Wyatt Hepler5191f582020-08-24 09:26:23 -070020pw_preprocessor/arguments.h
21---------------------------------
22Defines macros for handling variadic arguments to function-like macros. Macros
23include the following:
24
25.. c:function:: PW_DELEGATE_BY_ARG_COUNT(name, ...)
26
27 Selects and invokes a macro based on the number of arguments provided. Expands
28 to ``<name><arg_count>(...)``. For example,
29 ``PW_DELEGATE_BY_ARG_COUNT(foo_, 1, 2, 3)`` expands to ``foo_3(1, 2, 3)``.
30
31 This example shows how ``PW_DELEGATE_BY_ARG_COUNT`` could be used to log a
32 customized message based on the number of arguments provided.
33
34 .. code-block:: cpp
35
36 #define ARG_PRINT(...) PW_DELEGATE_BY_ARG_COUNT(_ARG_PRINT, __VA_ARGS__)
37 #define _ARG_PRINT_0(a) LOG_INFO("nothing!")
38 #define _ARG_PRINT_1(a) LOG_INFO("1 arg: %s", a)
39 #define _ARG_PRINT_2(a, b) LOG_INFO("2 args: %s, %s", a, b)
40 #define _ARG_PRINT_3(a, b, c) LOG_INFO("3 args: %s, %s, %s", a, b, c)
41
42 When used, ``ARG_PRINT`` expands to the ``_ARG_PRINT_#`` macro corresponding
43 to the number of arguments.
44
45 .. code-block:: cpp
46
47 ARG_PRINT(); // Outputs: nothing!
48 ARG_PRINT("a"); // Outputs: 1 arg: a
49 ARG_PRINT("a", "b"); // Outputs: 2 args: a, b
50 ARG_PRINT("a", "b", "c"); // Outputs: 3 args: a, b, c
51
52.. c:function:: PW_COMMA_ARGS(...)
53
54 Expands to a comma followed by the arguments if any arguments are provided.
55 Otherwise, expands to nothing. If the final argument is empty, it is omitted.
56 This is useful when passing ``__VA_ARGS__`` to a variadic function or template
57 parameter list, since it removes the extra comma when no arguments are
58 provided. ``PW_COMMA_ARGS`` must NOT be used when invoking a macro from
59 another macro.
60
61 For example. ``PW_COMMA_ARGS(1, 2, 3)``, expands to ``, 1, 2, 3``, while
62 ``PW_COMMA_ARGS()`` expands to nothing. ``PW_COMMA_ARGS(1, 2, )`` expands to
63 ``, 1, 2``.
64
Alexei Frolovc10c8122019-11-01 16:31:19 -070065pw_preprocessor/boolean.h
66-------------------------
67Defines macros for boolean logic on literal 1s and 0s. This is useful for
68situations when a literal is needed to build the name of a function or macro.
69
70pw_preprocessor/compiler.h
71--------------------------
72Macros for compiler-specific features, such as attributes or builtins.
73
74pw_preprocessor/concat.h
75------------------------
76Defines the ``PW_CONCAT(...)`` macro, which expands its arguments if they are
77macros and token pastes the results. This can be used for building names of
78classes, variables, macros, etc.
79
Alexei Frolovc10c8122019-11-01 16:31:19 -070080pw_preprocessor/util.h
81----------------------
82General purpose, useful macros.
83
84* ``PW_ARRAY_SIZE(array)`` -- calculates the size of a C array
85* ``PW_UNUSED(value)`` -- silences "unused variable" compiler warnings
86* ``PW_STRINGIFY(...)`` -- expands its arguments as macros and converts them to
87 a string literal
88* ``PW_EXTERN_C`` -- declares a name to be ``extern "C"`` in C++; expands to
89 nothing in C
90* ``PW_EXTERN_C_START`` / ``PW_EXTERN_C_END`` -- declares an ``extern "C" { }``
91 block in C++; expands to nothing in C