2012-04-19 16:42:35 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-04-19 16:42:35 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-04-19 16:42:35 +10:00
|
|
|
*
|
2016-03-11 19:15:20 +11:00
|
|
|
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
|
2012-04-19 16:42:35 +10:00
|
|
|
*/
|
|
|
|
|
2012-05-03 18:09:06 +10:00
|
|
|
#ifndef __UTIL_PREPROCESSOR_HPP
|
|
|
|
#define __UTIL_PREPROCESSOR_HPP
|
2012-04-19 16:42:35 +10:00
|
|
|
|
2016-03-11 19:15:20 +11:00
|
|
|
|
2012-05-11 12:19:36 +10:00
|
|
|
#define PASTE_DETAIL(x, y) x##y
|
|
|
|
#define PASTE(x, y) PASTE_DETAIL(x, y)
|
|
|
|
|
2016-03-14 22:32:25 +11:00
|
|
|
#define PASTE_LIST(x,y) PASTE(x,y),
|
|
|
|
|
|
|
|
#define NAMESPACE_LIST(x,y) x::y,
|
|
|
|
|
2016-03-11 19:15:20 +11:00
|
|
|
#define STRINGIZE_DETAIL(x) #x
|
|
|
|
#define STRINGIZE(x) STRINGIZE_DETAIL(x)
|
|
|
|
|
|
|
|
#define STRINGIZE_LIST(x) STRINGIZE(x),
|
|
|
|
|
2016-03-14 22:32:25 +11:00
|
|
|
#define STATIC_ASSERT(MSG) static_assert(false, MSG);
|
2016-03-11 19:15:20 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Varags functional style map
|
|
|
|
//
|
|
|
|
// Based off a technique seen on StackOverflow:
|
|
|
|
// http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments
|
2016-03-14 22:32:25 +11:00
|
|
|
#define MAP_0_01(F, X) F(X)
|
|
|
|
#define MAP_0_02(F, X, ...) F(X)MAP_0_01(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_03(F, X, ...) F(X)MAP_0_02(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_04(F, X, ...) F(X)MAP_0_03(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_05(F, X, ...) F(X)MAP_0_04(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_06(F, X, ...) F(X)MAP_0_05(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_07(F, X, ...) F(X)MAP_0_06(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_08(F, X, ...) F(X)MAP_0_07(F, __VA_ARGS__)
|
2016-06-24 13:15:01 +10:00
|
|
|
|
2016-03-14 22:32:25 +11:00
|
|
|
#define MAP_0_09(F, X, ...) F(X)MAP_0_08(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_10(F, X, ...) F(X)MAP_0_09(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_11(F, X, ...) F(X)MAP_0_10(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_12(F, X, ...) F(X)MAP_0_11(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_13(F, X, ...) F(X)MAP_0_12(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_14(F, X, ...) F(X)MAP_0_13(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_15(F, X, ...) F(X)MAP_0_14(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_16(F, X, ...) F(X)MAP_0_15(F, __VA_ARGS__)
|
2016-03-11 19:15:20 +11:00
|
|
|
|
2016-06-24 13:15:01 +10:00
|
|
|
#define MAP_0_17(F, X, ...) F(X)MAP_0_16(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_18(F, X, ...) F(X)MAP_0_17(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_19(F, X, ...) F(X)MAP_0_18(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_20(F, X, ...) F(X)MAP_0_19(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_21(F, X, ...) F(X)MAP_0_20(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_22(F, X, ...) F(X)MAP_0_21(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_23(F, X, ...) F(X)MAP_0_22(F, __VA_ARGS__)
|
|
|
|
#define MAP_0_24(F, X, ...) F(X)MAP_0_23(F, __VA_ARGS__)
|
|
|
|
|
|
|
|
|
2016-03-14 22:32:25 +11:00
|
|
|
#define MAP_1_01(F, A, X) F(A, X)
|
|
|
|
#define MAP_1_02(F, A, X, ...) F(A, X)MAP_1_01(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_03(F, A, X, ...) F(A, X)MAP_1_02(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_04(F, A, X, ...) F(A, X)MAP_1_03(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_05(F, A, X, ...) F(A, X)MAP_1_04(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_06(F, A, X, ...) F(A, X)MAP_1_05(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_07(F, A, X, ...) F(A, X)MAP_1_06(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_08(F, A, X, ...) F(A, X)MAP_1_07(F, A, __VA_ARGS__)
|
2016-06-24 13:15:01 +10:00
|
|
|
|
2016-03-14 22:32:25 +11:00
|
|
|
#define MAP_1_09(F, A, X, ...) F(A, X)MAP_1_08(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_10(F, A, X, ...) F(A, X)MAP_1_09(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_11(F, A, X, ...) F(A, X)MAP_1_10(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_12(F, A, X, ...) F(A, X)MAP_1_11(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_13(F, A, X, ...) F(A, X)MAP_1_12(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_14(F, A, X, ...) F(A, X)MAP_1_13(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_15(F, A, X, ...) F(A, X)MAP_1_14(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_16(F, A, X, ...) F(A, X)MAP_1_15(F, A, __VA_ARGS__)
|
2016-03-11 19:15:20 +11:00
|
|
|
|
2016-06-24 13:15:01 +10:00
|
|
|
#define MAP_1_17(F, A, X, ...) F(A, X)MAP_1_16(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_18(F, A, X, ...) F(A, X)MAP_1_17(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_19(F, A, X, ...) F(A, X)MAP_1_18(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_20(F, A, X, ...) F(A, X)MAP_1_19(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_21(F, A, X, ...) F(A, X)MAP_1_20(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_22(F, A, X, ...) F(A, X)MAP_1_21(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_23(F, A, X, ...) F(A, X)MAP_1_22(F, A, __VA_ARGS__)
|
|
|
|
#define MAP_1_24(F, A, X, ...) F(A, X)MAP_1_23(F, A, __VA_ARGS__)
|
|
|
|
|
2016-03-11 19:15:20 +11:00
|
|
|
// Uses the sliding pairs dispatch technique: by passing __VA_ARGS__ as the
|
|
|
|
// first set of variables and appending (reverse orderred) names of the
|
|
|
|
// corresponding macros, we get the name of the correct arity macro.
|
2016-03-14 22:32:25 +11:00
|
|
|
//
|
|
|
|
// Ensure this is called with at least N+2 parameters otherwise GCC in
|
|
|
|
// particular will complain about requiring at least one value for varargs
|
2016-03-11 19:15:20 +11:00
|
|
|
#define ARITY_DISPATCH( \
|
|
|
|
_01,_02,_03,_04,_05,_06,_07,_08, \
|
|
|
|
_09,_10,_11,_12,_13,_14,_15,_16, \
|
2016-06-24 13:15:01 +10:00
|
|
|
_17,_18,_19,_20,_21,_22,_23,_24, \
|
2016-03-11 19:15:20 +11:00
|
|
|
NAME,... \
|
|
|
|
) NAME
|
|
|
|
|
|
|
|
// Map a macro across __VA_ARGS__. Can be easily expanded, but has non-obvious
|
2016-03-14 22:32:25 +11:00
|
|
|
// failure mode when supported arity is exceeded.
|
2016-03-11 19:15:20 +11:00
|
|
|
//
|
2016-03-14 22:32:25 +11:00
|
|
|
// Don't use this, unless you really need the stringize operator, or other
|
|
|
|
// macro magic.
|
|
|
|
//
|
|
|
|
// Just use templates. Really...
|
|
|
|
#define MAP(FUNC,...) \
|
|
|
|
ARITY_DISPATCH(__VA_ARGS__, \
|
2016-06-24 13:15:01 +10:00
|
|
|
MAP_0_24, MAP_0_23, MAP_0_22, MAP_0_21, MAP_0_20, MAP_0_19, MAP_0_18, MAP_0_17, \
|
2016-03-14 22:32:25 +11:00
|
|
|
MAP_0_16, MAP_0_15, MAP_0_14, MAP_0_13, MAP_0_12, MAP_0_11, MAP_0_10, MAP_0_09, \
|
|
|
|
MAP_0_08, MAP_0_07, MAP_0_06, MAP_0_05, MAP_0_04, MAP_0_03, MAP_0_02, MAP_0_01, \
|
|
|
|
STATIC_ASSERT("invalid arity for MAP") \
|
|
|
|
)(FUNC, __VA_ARGS__)
|
|
|
|
|
|
|
|
#define MAP1(FUNC,...) \
|
|
|
|
ARITY_DISPATCH(__VA_ARGS__, \
|
2016-06-24 13:15:01 +10:00
|
|
|
MAP_1_23, MAP_1_22, MAP_1_21, MAP_1_20, MAP_1_19, MAP_1_18, MAP_1_17, \
|
|
|
|
MAP_1_16, MAP_1_15, MAP_1_14, MAP_1_13, MAP_1_12, MAP_1_11, MAP_1_10, MAP_1_09, \
|
2016-03-14 22:32:25 +11:00
|
|
|
MAP_1_08, MAP_1_07, MAP_1_06, MAP_1_05, MAP_1_04, MAP_1_03, MAP_1_02, MAP_1_01, \
|
|
|
|
STATIC_ASSERT("invalid arity for MAP1") \
|
2016-03-11 19:15:20 +11:00
|
|
|
)(FUNC, __VA_ARGS__)
|
2012-04-19 16:42:35 +10:00
|
|
|
|
2016-03-14 22:32:25 +11:00
|
|
|
|
|
|
|
|
|
|
|
#define LITERAL_00 0
|
|
|
|
#define LITERAL_01 1
|
|
|
|
#define LITERAL_02 2
|
|
|
|
#define LITERAL_03 3
|
|
|
|
#define LITERAL_04 4
|
|
|
|
#define LITERAL_05 5
|
|
|
|
#define LITERAL_06 6
|
|
|
|
#define LITERAL_07 7
|
|
|
|
#define LITERAL_08 8
|
|
|
|
#define LITERAL_09 9
|
|
|
|
#define LITERAL_10 10
|
|
|
|
#define LITERAL_11 11
|
|
|
|
#define LITERAL_12 12
|
|
|
|
#define LITERAL_13 13
|
|
|
|
#define LITERAL_14 14
|
|
|
|
#define LITERAL_15 15
|
|
|
|
#define LITERAL_16 16
|
2016-06-24 13:15:01 +10:00
|
|
|
#define LITERAL_17 17
|
|
|
|
#define LITERAL_18 18
|
|
|
|
#define LITERAL_19 19
|
|
|
|
#define LITERAL_20 20
|
|
|
|
#define LITERAL_21 21
|
|
|
|
#define LITERAL_22 22
|
|
|
|
#define LITERAL_23 23
|
|
|
|
#define LITERAL_24 24
|
2016-03-14 22:32:25 +11:00
|
|
|
|
|
|
|
|
|
|
|
#define VA_ARGS_COUNT(...) \
|
|
|
|
ARITY_DISPATCH(__VA_ARGS__, \
|
2016-06-24 13:15:01 +10:00
|
|
|
LITERAL_24, LITERAL_23, LITERAL_22, LITERAL_21, \
|
|
|
|
LITERAL_20, LITERAL_19, LITERAL_18, LITERAL_17, \
|
2016-03-14 22:32:25 +11:00
|
|
|
LITERAL_16, LITERAL_15, LITERAL_14, LITERAL_13, \
|
|
|
|
LITERAL_12, LITERAL_11, LITERAL_10, LITERAL_09, \
|
|
|
|
LITERAL_08, LITERAL_07, LITERAL_06, LITERAL_05, \
|
|
|
|
LITERAL_04, LITERAL_03, LITERAL_02, LITERAL_01, \
|
|
|
|
STATIC_ASSERT("invalid value for VA_ARGS_COUNT")\
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-04-19 16:42:35 +10:00
|
|
|
#endif
|