4 * The freeglut library callback macro file.
6 * Copyright (C) 2016 Vincent Simonetti
7 * Creation date: Sat Jan 16 2016
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #ifndef FREEGLUT_CALLBACK_MACROS_H
28 #define FREEGLUT_CALLBACK_MACROS_H
32 * FG_COMPILER_SUPPORTS_GCC_VA_ARGS_HACK: if the compiler supports GCC's variadic macro implementation (AKA, ##__VA_ARGS__)
33 * FG_COMPILER_SUPPORTS_VA_ARGS: if the compiler supports variadic macros
36 /* What supports variadic macros based off Wikipedia article on it (GCC-like must support C99 or higher to use variadic macros) */
37 #if (((defined(__GNUC__) && (__GNUC__ >= 3)) || \
38 (defined(__clang__))) && \
39 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))) || \
40 (defined(_MSC_VER) && (_MSC_VER >= 1400)) || \
41 (defined(__BORLANDC__) && (__BORLANDC__ >= 0x570)) || \
42 (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x530))
43 #define FG_COMPILER_SUPPORTS_VA_ARGS 1
45 #define FG_COMPILER_SUPPORTS_VA_ARGS 0
48 /* If __VA_ARGS__ is supported, it needs to be GCC(-like) or Clang (since Clang mimics GCC) */
49 #if FG_COMPILER_SUPPORTS_VA_ARGS && \
50 (defined(__GNUC__)) || \
52 #define FG_COMPILER_SUPPORTS_GCC_VA_ARGS_HACK 1
54 #define FG_COMPILER_SUPPORTS_GCC_VA_ARGS_HACK 0
60 * This took a while to figure out, so be sure try to understand what is happening so that you can ensure that whatever you
61 * change won't break other areas.
63 * If you are just adding a new callback/changing it's argument count, just go to the bottom of the file.
65 * This whole file exists purely for the sake of preventing the need to implement additional parsing logic for each callback
66 * to pass user arguments. Of course, the necessity to support older compilers means that, as seen in the line above, there
67 * is still a requirement to add/modify code to handle callbacks. If freeglut ever requires newer compilers (at minimum, ones
68 * that support C99 or higher), code can very slowly be removed from this file. Even better would be if the C standard eventually
69 * supports something similar to what GCC has implemented or offers an alternative. Another option is if C++ would be "allowed" by
70 * project maintaners, as then templates can be used and function overloading. Ironically, the template would probably look worse
71 * then the GCC macro, so maybe it's good to stay as is.
73 * Onto the different "versions" of macros:
75 * There is one for GCC/Clang(/and supposedly the Intel compiler) which supports the non-standard ##__VA_ARGS__ token. The code may
76 * look ugly, but the result is, if this was standard, no one would ever need to open this file unless they were curious (or needed
77 * more then 5 arguments for a callback, but that's trivial to add). It works by adding many fake macros to a "picker" macro
78 * (PP_HAS_ARGS_IMPL2) which then indictaes which macro counter to use. As we can already use variadic macros (the VA in __VA_ARGS__),
79 * this just becomes a "reuse the arguments*.
81 * The next is for any non-GCC/Clang/etc. compiler *cough* MSVC/compiler you probably shouldn't be using *cough* that supports C99
82 * by default. It requires each callback to have a specific argument count passthrough macro. The only reason there are specific
83 * count macros is so that (see paraghraph below) don't need have their own set of callback macros. Ideally, there would only be
84 * ZERO and ONE_OR_MORE. This works by having callback-specific macros call a specific handler macro to return user data (ZERO) or
85 * return one or more arguments along with userData (ONE_OR_MORE) where, with variadic macros, it just reuses the arguments.
87 * The last set is for the poor individual who has to use a compiler that doesn't support C99 by default, or may not support it at
88 * all. Stuff like MSVC6... It works by having a specific-count macro that "extracts" each argument to have them reused without the
91 * A note on parathesis, as earlier mentioned, if the GCC variadic macro element was standard, then instead of needing:
93 * func EXPAND_WCB(Mouse)(( (GLUT_LEFT_BUTTON, GLUT_DOWN, 10, 30), userData));
95 * ...you can do the following:
97 * func EXPAND_WCB (GLUT_LEFT_BUTTON, GLUT_DOWN, 10, 30);
99 * Wow... so much nice and easier to understand. Sub-note: I have not worked on a version that explicitly takes userData, so for now
100 * if you can get to that version, look in the version control change history for this file and you'll find that version which
101 * implicitly passes "userData" and only works on GCC vardiac macro supporting compilers.
104 #if FG_COMPILER_SUPPORTS_GCC_VA_ARGS_HACK
107 * EXPAND_WCB() is used as:
109 * EXPAND_WCB( cbname )(( arg_list, userData ))
111 * ... where {(arg_list)} is the parameter list and userData is user
114 * All additional macros are to get around trailing ',' for zero-arg
118 * http://stackoverflow.com/questions/5355241/generating-function-declaration-using-a-macro-iteration/5355946#5355946
122 * GCC-specific design that doesn't require per-callback defines
124 * The naming is terrible... and it's very convuluted and complex, but
125 * should not require any modification unless additional arguments are to
128 * Supports callbacks up to 5 args (follow the pattern of
129 * EXPAND_WCB_PP_HAS_ARGS_IMPL2 and EXPAND_WCB_PP_HAS_ARGS_SOURCE to add more)
134 #define EXPAND_WCB_UNPARAN(...) __VA_ARGS__
136 #define EXPAND_WCB_PP_HAS_ARGS_IMPL2(_0, _1, _2, _3, _4, _5, N, ...) N
137 #define EXPAND_WCB_PP_HAS_ARGS_SOURCE() ONE_OR_MORE, ONE_OR_MORE, ONE_OR_MORE, ONE_OR_MORE, ONE_OR_MORE, ZERO
139 #define EXPAND_WCB_PP_HAS_ARGS_IMPL(...) EXPAND_WCB_PP_HAS_ARGS_IMPL2( __VA_ARGS__ )
140 #define EXPAND_WCB_PP_HAS_ARGS(...) EXPAND_WCB_PP_HAS_ARGS_IMPL( NOT_EXIST, ##__VA_ARGS__, EXPAND_WCB_PP_HAS_ARGS_SOURCE() )
142 #define EXPAND_WCB_ZERO(args, userData) ( userData )
143 #define EXPAND_WCB_ONE_OR_MORE(args, userData) ( EXPAND_WCB_UNPARAN args, userData )
145 #define EXPAND_WCB_DISAMBIGUATE2(has_args, args, userData) EXPAND_WCB_ ## has_args ( args, userData )
146 #define EXPAND_WCB_DISAMBIGUATE(has_args, args, userData) EXPAND_WCB_DISAMBIGUATE2( has_args, args, userData )
148 #define EXPAND_WCB_UNWRAP_ARGS2(args, userData) EXPAND_WCB_DISAMBIGUATE( EXPAND_WCB_PP_HAS_ARGS args, args, userData )
149 #define EXPAND_WCB_UNWRAP_ARGS(args) EXPAND_WCB_UNWRAP_ARGS2 args
151 #define EXPAND_WCB(cbname) EXPAND_WCB_UNWRAP_ARGS
156 * EXPAND_WCB() is used as:
158 * EXPAND_WCB( cbname )(( arg_list, userData ))
160 * ... where {(arg_list)} is the parameter list and userData is user
163 * This will take the arg_list and extend it by one argument, adding
164 * the argument "userData" to the end of the list.
166 * In order for this to work, each callback must have a define that
167 * properly handles the arguments as needed by the callback.
168 * This callback is in the format of EXPAND_WCB_SUB_<cbname>.
169 * Helper functions exist for zero to five parameters: EXPAND_WCB_ZERO,
170 * EXPAND_WCB_ONE, EXPAND_WCB_TWO, EXPAND_WCB_THREE< EXPAND_WCB_FOUR,
171 * and EXPAND_WCB_FIVE. Each handle the callback argument counts.
173 * An example for the "Entry" callback, where "Entry" is the cbname:
174 * typedef void (* FGCBEntry )( int );
175 * typedef void (* FGCBEntryUC)( int, FGCBUserData );
176 * #define EXPAND_WCB_SUB_Entry(args) EXPAND_WCB_ONE args
179 #if FG_COMPILER_SUPPORTS_VA_ARGS
181 #define EXPAND_WCB_UNPARAN(...) __VA_ARGS__
182 #define EXPAND_WCB_ONE_OR_MORE(args, userData) ( EXPAND_WCB_UNPARAN args, userData )
184 #define EXPAND_WCB_ONE(args, userData) EXPAND_WCB_ONE_OR_MORE( args, userData )
185 #define EXPAND_WCB_TWO(args, userData) EXPAND_WCB_ONE_OR_MORE( args, userData )
186 #define EXPAND_WCB_THREE(args, userData) EXPAND_WCB_ONE_OR_MORE( args, userData )
187 #define EXPAND_WCB_FOUR(args, userData) EXPAND_WCB_ONE_OR_MORE( args, userData )
188 #define EXPAND_WCB_FIVE(args, userData) EXPAND_WCB_ONE_OR_MORE( args, userData )
192 #define EXPAND_WCB_EXTRACT_ONE_ARGS(arg1) arg1
193 #define EXPAND_WCB_EXTRACT_TWO_ARGS(arg1, arg2) arg1, arg2
194 #define EXPAND_WCB_EXTRACT_THREE_ARGS(arg1, arg2, arg3) arg1, arg2, arg3
195 #define EXPAND_WCB_EXTRACT_FOUR_ARGS(arg1, arg2, arg3, arg4) arg1, arg2, arg3, arg4
196 #define EXPAND_WCB_EXTRACT_FIVE_ARGS(arg1, arg2, arg3, arg4, arg5) arg1, arg2, arg3, arg4, arg5
198 #define EXPAND_WCB_ONE(args, userData) (EXPAND_WCB_EXTRACT_ONE_ARGS args, userData)
199 #define EXPAND_WCB_TWO(args, userData) (EXPAND_WCB_EXTRACT_TWO_ARGS args, userData)
200 #define EXPAND_WCB_THREE(args, userData) (EXPAND_WCB_EXTRACT_THREE_ARGS args, userData)
201 #define EXPAND_WCB_FOUR(args, userData) (EXPAND_WCB_EXTRACT_FOUR_ARGS args, userData)
202 #define EXPAND_WCB_FIVE(args, userData) (EXPAND_WCB_EXTRACT_FIVE_ARGS args, userData)
206 #define EXPAND_WCB_ZERO(args, userData) ( userData )
208 #define EXPAND_WCB(cbname) EXPAND_WCB_SUB_ ## cbname
211 * Freeglut callbacks type definitions macros
213 * Every time a callback is updated in fg_internal.h is updated, this needs updated
214 * if argument counts change, new callbacks are added, or callbacks are removed.
217 #define EXPAND_WCB_SUB_Display(args) EXPAND_WCB_ZERO args
218 #define EXPAND_WCB_SUB_Reshape(args) EXPAND_WCB_TWO args
219 #define EXPAND_WCB_SUB_Position(args) EXPAND_WCB_TWO args
220 #define EXPAND_WCB_SUB_Visibility(args) EXPAND_WCB_ONE args
221 #define EXPAND_WCB_SUB_Keyboard(args) EXPAND_WCB_THREE args
222 #define EXPAND_WCB_SUB_KeyboardUp(args) EXPAND_WCB_THREE args
223 #define EXPAND_WCB_SUB_Special(args) EXPAND_WCB_THREE args
224 #define EXPAND_WCB_SUB_SpecialUp(args) EXPAND_WCB_THREE args
225 #define EXPAND_WCB_SUB_Mouse(args) EXPAND_WCB_FOUR args
226 #define EXPAND_WCB_SUB_MouseWheel(args) EXPAND_WCB_FOUR args
227 #define EXPAND_WCB_SUB_Motion(args) EXPAND_WCB_TWO args
228 #define EXPAND_WCB_SUB_Passive(args) EXPAND_WCB_TWO args
229 #define EXPAND_WCB_SUB_Entry(args) EXPAND_WCB_ONE args
230 #define EXPAND_WCB_SUB_WindowStatus(args) EXPAND_WCB_ONE args
231 #define EXPAND_WCB_SUB_Joystick(args) EXPAND_WCB_FOUR args
232 #define EXPAND_WCB_SUB_OverlayDisplay(args) EXPAND_WCB_ZERO args
233 #define EXPAND_WCB_SUB_SpaceMotion(args) EXPAND_WCB_THREE args
234 #define EXPAND_WCB_SUB_SpaceRotation(args) EXPAND_WCB_THREE args
235 #define EXPAND_WCB_SUB_SpaceButton(args) EXPAND_WCB_TWO args
236 #define EXPAND_WCB_SUB_Dials(args) EXPAND_WCB_TWO args
237 #define EXPAND_WCB_SUB_ButtonBox(args) EXPAND_WCB_TWO args
238 #define EXPAND_WCB_SUB_TabletMotion(args) EXPAND_WCB_TWO args
239 #define EXPAND_WCB_SUB_TabletButton(args) EXPAND_WCB_FOUR args
240 #define EXPAND_WCB_SUB_Destroy(args) EXPAND_WCB_ZERO args
241 #define EXPAND_WCB_SUB_MultiEntry(args) EXPAND_WCB_TWO args
242 #define EXPAND_WCB_SUB_MultiButton(args) EXPAND_WCB_FIVE args
243 #define EXPAND_WCB_SUB_MultiMotion(args) EXPAND_WCB_THREE args
244 #define EXPAND_WCB_SUB_MultiPassive(args) EXPAND_WCB_THREE args
245 #define EXPAND_WCB_SUB_InitContext(args) EXPAND_WCB_ZERO args
246 #define EXPAND_WCB_SUB_AppStatus(args) EXPAND_WCB_ONE args
250 #endif /* FREEGLUT_CALLBACK_MACROS_H */
252 /*** END OF FILE ***/