Database: sqlite default to appdata kworship.db
[kworship.git] / include / functionInfo.h
blobb3de2d6dcabc5cbc89079bc915397d00ecd13606
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #ifndef _functionInfo_h_
21 #define _functionInfo_h_
23 #include "repeatMacros.h"
25 namespace meta
27 // Pointer handling
28 template <typename T>
29 struct PointerInfo;
31 template <typename T>
32 struct PointerInfo<T*>
34 typedef T Type;
37 template <typename F>
38 struct Tuple;
39 #define META_TUPLE(ARGS) ::meta::Tuple<void (*)ARGS>
41 template <typename F, int P>
42 struct ParameterInfo;
44 // Number of arguments of a function
45 template <typename F>
46 struct FunctionInfo;
49 // Macros to make this easier
51 // Parameter list, e.g. "typename Param0; typename Param1;"
52 #define FUNCTION_INFO_PARAM_INSTANTIATION(A,B,N) Param##N param##N;
53 #define FUNCTION_INFO_PARAM_INSTANTIATIONS(N) REPEATB(FUNCTION_INFO_PARAM_INSTANTIATION,0,0,N)
54 // Parameter list, e.g. "typename Param0, typename Param1"
55 #define FUNCTION_INFO_TEMPLATE_PARAMETER(A,B,N) typename Param##N
56 #define FUNCTION_INFO_TEMPLATE_PARAMETERS(N) REPEATB_LIST(FUNCTION_INFO_TEMPLATE_PARAMETER,0,0,N)
57 // Parameter list, e.g. ", typename Param0, typename Param1"
58 #define FUNCTION_INFO_TEMPLATE_PARAMETER_TAIL(A,B,N) , typename Param##N
59 #define FUNCTION_INFO_TEMPLATE_PARAMETERS_TAIL(N) REPEATB(FUNCTION_INFO_TEMPLATE_PARAMETER_TAIL,0,0,N)
60 // Argument list, e.g. "(Param0 param0, Param1 param1)"
61 #define FUNCTION_INFO_PARAMETER(A,B,N) Param##N param##N
62 #define FUNCTION_INFO_PARAMETERS(N) \
63 ( REPEATB_LIST(FUNCTION_INFO_PARAMETER,0,0,N) )
64 // Tail argument list, e.g. ", Param0, Param1"
65 #define FUNCTION_INFO_PARAMETER_TAIL(A,B,N) , Param##N
66 #define FUNCTION_INFO_PARAMETERS_TAIL(N) \
67 REPEATB(FUNCTION_INFO_PARAMETER_TAIL,0,0,N)
68 // Argument list, e.g. "(param0, param1)"
69 #define FUNCTION_INFO_ARGUMENT(A,B,N) param##N
70 #define FUNCTION_INFO_ARGUMENTS(N) \
71 ( REPEATB_LIST(FUNCTION_INFO_ARGUMENT,0,0,N) )
72 // Unpack Argument list, e.g. "(P.param0, P.param1)"
73 #define FUNCTION_INFO_UNPACK_ARGUMENT(P,B,N) (P).param##N
74 #define FUNCTION_INFO_UNPACK_ARGUMENTS(P,N) \
75 ( REPEATB_LIST(FUNCTION_INFO_UNPACK_ARGUMENT,P,0,N) )
76 // Unpack Argument list with preceding comma, e.g. ", P.param0, P.param1"
77 #define FUNCTION_INFO_UNPACK_ARGUMENT_PRECOMMA(P,B,N) , (P).param##N
78 #define FUNCTION_INFO_UNPACK_ARGUMENTS_PRECOMMA(P,N) \
79 REPEATB(FUNCTION_INFO_UNPACK_ARGUMENT_PRECOMMA,P,0,N)
81 // Parameter template instantiation
82 #define PARAMETER_INFO(T,N) \
83 template <typename R, FUNCTION_INFO_TEMPLATE_PARAMETERS(T) > \
84 struct ParameterInfo<R (*)FUNCTION_INFO_PARAMETERS(T),N> \
85 { \
86 typedef Param##N Type; \
89 // Function length template instantiation
90 #define FUNCTION_INFO(N) \
91 template <FUNCTION_INFO_TEMPLATE_PARAMETERS(N) > \
92 struct Tuple<void (*)FUNCTION_INFO_PARAMETERS(N)> \
93 { \
94 enum \
95 { \
96 length = N \
97 }; \
98 struct Pack \
99 { \
100 /* A member for each item */ \
101 FUNCTION_INFO_PARAM_INSTANTIATIONS(N) \
102 /* Construct a new CTYPE using the pack as arguments */ \
103 template <typename CTYPE> \
104 CTYPE* construct() const \
106 return new CTYPE FUNCTION_INFO_ARGUMENTS(N); \
108 }; \
109 static struct Pack pack FUNCTION_INFO_PARAMETERS(N) \
111 Pack myPack = { REPEATB_LIST(FUNCTION_INFO_ARGUMENT,0,0,N) }; \
112 return myPack; \
114 template <int id> \
115 struct Param : public ParameterInfo<void (*)FUNCTION_INFO_PARAMETERS(N), id> \
117 }; \
118 template <typename Ret> \
119 struct Return \
121 typedef Ret (Signiture)FUNCTION_INFO_PARAMETERS(N); \
122 typedef Ret (*Pointer)FUNCTION_INFO_PARAMETERS(N); \
123 typedef FunctionInfo<Pointer> Info; \
124 }; \
125 template <typename NewArg> \
126 struct Unshift \
128 typedef void (*Pointer)(NewArg FUNCTION_INFO_PARAMETERS_TAIL(N)); \
129 typedef Tuple<Pointer> Info; \
130 }; \
131 }; \
132 template <typename Ret FUNCTION_INFO_TEMPLATE_PARAMETERS_TAIL(N) > \
133 struct FunctionInfo<Ret (*)FUNCTION_INFO_PARAMETERS(N)> \
135 typedef Ret Return; \
136 typedef Ret (*Pointer)FUNCTION_INFO_PARAMETERS(N); \
137 typedef Tuple<void (*)FUNCTION_INFO_PARAMETERS(N)> Params; \
138 template <typename NewArg> \
139 struct Unshift \
141 typedef Ret (*Pointer)(NewArg FUNCTION_INFO_PARAMETERS_TAIL(N)); \
142 typedef FunctionInfo<Pointer> Info; \
143 }; \
144 template <typename FUNCTOR> \
145 static Ret unpack(typename Params::Pack package) \
147 return FUNCTOR::call FUNCTION_INFO_UNPACK_ARGUMENTS(package,N); \
149 template <typename FUNCTOR, typename P0> \
150 static Ret unpack(P0 p0, typename Params::Pack package) \
152 return FUNCTOR::call ( p0 FUNCTION_INFO_UNPACK_ARGUMENTS_PRECOMMA(package,N) ); \
154 /* Call a pointer with a pack of arguments */ \
155 static Ret call(Pointer f, typename Params::Pack package) \
157 return f FUNCTION_INFO_UNPACK_ARGUMENTS(package,N); \
159 /* Call a pointer with one plus a pack of arguments */ \
160 template <typename P0> \
161 static Ret call(typename Unshift<P0>::Pointer f, P0 p0, typename Params::Pack package) \
163 return f( p0 FUNCTION_INFO_UNPACK_ARGUMENTS_PRECOMMA(package,N) ); \
165 }; \
166 REPEATA(PARAMETER_INFO, N, N)
169 REPEAT(FUNCTION_INFO, 8)
171 #undef FUNCTION_INFO_PARAMETER
172 #undef FUNCTION_INFO_PARAMETERS
173 #undef FUNCTION_INFO_ARGUMENT
174 #undef FUNCTION_INFO_POINTER_TYPE
175 #undef PARAMETER_INFO
176 #undef FUNCTION_INFO
179 #endif // _functionInfo_h_