1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes macros for parsing variadic arguments.
32  */
33 
34 #ifndef OT_ARG_MACROS_HPP_
35 #define OT_ARG_MACROS_HPP_
36 
37 /**
38  * This macro returns the first argument in a list of input arguments.
39  *
40  * @param[in] ...   A list of arguments (MUST contain at least one).
41  *
42  * @returns The first argument in the given list of input arguments.
43  *
44  */
45 #define OT_FIRST_ARG(...) _OT_GET_FIRST_ARG(__VA_ARGS__, JUNK)
46 
47 /**
48  * This macro returns the second argument in a list of input arguments if any.
49  *
50  * @note: This macro works when the list contains either one or two arguments.
51  *
52  * @param[in] ...   A list of arguments (MUST contain either one or two arguments).
53  *
54  * @returns The second argument if any.
55  *
56  */
57 #define OT_SECOND_ARG(...) _OT_GET_SECOND_ARG(_OT_HAS_ONE_ARG_OR_TWO_ARGS(__VA_ARGS__), __VA_ARGS__)
58 
59 /**
60  * This macro expands to comma separated list of arguments excluding the first one.
61  *
62  * If there is only one argument, it expands to empty. If there is more than one argument, it expands to an initial
63  * comma followed by all the rest of arguments excluding the first one.
64  *
65  * @note: This macro supports up to 20 arguments.
66  *
67  * @param[in] ...  A list of arguments (MUST contain at least one).
68  *
69  * @returns A comma separated list of arguments excluding the first one.
70  *
71  */
72 #define OT_REST_ARGS(...) _OT_GET_REST_ARGS(_OT_HAS_ONE_ARG_VS_TWO_OR_MORE(__VA_ARGS__), __VA_ARGS__)
73 
74 //---------------------------------------------------------------------------------------------------------------------
75 // Private/local macros - for use in this header only.
76 
77 #define _OT_GET_FIRST_ARG(aFirst, ...) aFirst
78 
79 #define _OT_HAS_ONE_ARG_OR_TWO_ARGS(...) _OT_SELECT_3(__VA_ARGS__, TWO_ARGS, ONE_ARG, JUNK)
80 #define _OT_SELECT_3(a1, a2, a3, ...) a3
81 
82 #define _OT_GET_SECOND_ARG(aNum, ...) _OT_GET_SECOND_APPEND_NUM_ARGS(aNum, __VA_ARGS__)
83 #define _OT_GET_SECOND_APPEND_NUM_ARGS(aNum, ...) _OT_GET_SECOND_WITH_##aNum(__VA_ARGS__)
84 
85 #define _OT_GET_SECOND_WITH_ONE_ARG(aFirst)
86 #define _OT_GET_SECOND_WITH_TWO_ARGS(aFirst, aSecond) aSecond
87 
88 #define _OT_GET_REST_ARGS(aNum, ...) _OT_GET_REST_APPEND_NUM_ARGS(aNum, __VA_ARGS__)
89 #define _OT_GET_REST_APPEND_NUM_ARGS(aNum, ...) _OT_GET_REST_WITH_##aNum(__VA_ARGS__)
90 
91 #define _OT_GET_REST_WITH_ONE_ARG(aFirst)
92 #define _OT_GET_REST_WITH_TWO_OR_MORE_ARGS(aFirst, ...) , __VA_ARGS__
93 
94 #define _OT_HAS_ONE_ARG_VS_TWO_OR_MORE(...)                                                                 \
95     _OT_SELECT_20(__VA_ARGS__, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS,      \
96                   TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, \
97                   TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, \
98                   TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, TWO_OR_MORE_ARGS, ONE_ARG, JUNK)
99 
100 #define _OT_SELECT_20(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, ...) \
101     a20
102 
103 #endif // OT_ARG_MACROS_HPP_
104