summaryrefslogtreecommitdiff
path: root/src/QGlib/emitimpl.h
blob: d47c2021531160612373f4b8dd180aaf7e793601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
    Copyright (C) 2010  George Kiagiadakis <kiagiadakis.george@gmail.com>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(BOOST_PP_IS_ITERATING) || !BOOST_PP_IS_ITERATING

# ifndef IN_QGLIB_SIGNAL_H
#  error "This file must not be included directly"
# endif

# include "value.h"
# include <QtCore/QList>
# include <QtCore/QDebug>
# include <stdexcept>
# include <boost/type_traits.hpp>


namespace QGlib {
namespace Private {

/*! This method is used internally from the templated emit() method. */
Value emit(void *instance, const char *detailedSignal, const QList<Value> & args);

template <typename Signature>
struct EmitImpl {};

} //namespace Private
} //namespace QGlib


# if QGLIB_HAVE_CXX0X

namespace QGlib {
namespace Private {

//BEGIN ******** packArguments ********

inline QList<Value> packArguments()
{
    return QList<Value>();
}

template <typename Arg1, typename... Args>
QList<Value> packArguments(const Arg1 & a1, const Args & ... args)
{
    QList<Value> && result = packArguments(args...);
    Value v;
    v.init<Arg1>();
    ValueImpl<Arg1>::set(v, a1);
    //prepend, since we are packing from the last to the first argument
    result.prepend(v);
    return result;
}

//END ******** packArguments ********
//BEGIN ******** EmitImpl ********

template <typename R, typename... Args>
struct EmitImpl<R (Args...)>
{
    static inline R emit(void *instance, const char *detailedSignal, const Args & ... args)
    {
        try {
            Value && returnValue = Private::emit(instance, detailedSignal, packArguments(args...));
            return ValueImpl<R>::get(returnValue);
        } catch(const std::exception & e) {
            qCritical() << "Error during emission of signal" << detailedSignal << ":" << e.what();
            return R();
        }
    }
};

template <typename... Args>
struct EmitImpl<void (Args...)>
{
    static inline void emit(void *instance, const char *detailedSignal, const Args & ... args)
    {
        try {
            Value && returnValue = Private::emit(instance, detailedSignal, packArguments(args...));

            if (returnValue.isValid()) {
                qWarning() << "Ignoring return value from emission of signal" << detailedSignal;
            }
        } catch(const std::exception & e) {
            qCritical() << "Error during emission of signal" << detailedSignal << ":" << e.what();
        }
    }
};

//END ******** EmitImpl ********

} //namespace Private

//BEGIN ******** QGlib::emit ********

template <typename R, typename... Args>
R emit(void *instance, const char *detailedSignal, const Args & ... args)
{
    return QGlib::Private::EmitImpl<R (Args...)>::emit(instance, detailedSignal, args...);
}

//END ******** QGlib::emit ********

} //namespace QGlib

# else //QGLIB_HAVE_CXX0X

// include the second part of this file as many times as QGLIB_SIGNAL_MAX_ARGS specifies
#  define BOOST_PP_ITERATION_PARAMS_1 (3,(0, QGLIB_SIGNAL_MAX_ARGS, "QGlib/emitimpl.h"))
#  include BOOST_PP_ITERATE()
#  undef BOOST_PP_ITERATION_PARAMS_1

# endif //QGLIB_HAVE_CXX0X


#else // !defined(BOOST_PP_IS_ITERATING) || !BOOST_PP_IS_ITERATING

/*
    This part is included from BOOST_PP_ITERATE(). It defines specializations of struct EmitImpl
    with different number of arguments as well as the multiple implementations of the non-variadic
    QGlib::emit. This part is included multiple times (QGLIB_SIGNAL_MAX_ARGS defines how many),
    and each time it defines those classes and functions with different number of arguments.
    The concept is based on the implementation of boost::function.
*/

# define QGLIB_SIGNAL_IMPL_NUM_ARGS  \
    BOOST_PP_ITERATION()

# define QGLIB_SIGNAL_IMPL_TRAILING_TEMPLATE_PARAMS \
    BOOST_PP_ENUM_TRAILING_PARAMS(QGLIB_SIGNAL_IMPL_NUM_ARGS, typename A)

# define QGLIB_SIGNAL_IMPL_TEMPLATE_PARAMS \
    BOOST_PP_ENUM_PARAMS(QGLIB_SIGNAL_IMPL_NUM_ARGS, typename A)

# define QGLIB_SIGNAL_IMPL_TRAILING_TEMPLATE_ARGS \
    BOOST_PP_ENUM_TRAILING_PARAMS(QGLIB_SIGNAL_IMPL_NUM_ARGS, A)

# define QGLIB_SIGNAL_IMPL_TEMPLATE_ARGS \
    BOOST_PP_ENUM_PARAMS(QGLIB_SIGNAL_IMPL_NUM_ARGS, A)

# define QGLIB_SIGNAL_IMPL_FUNCTION_PARAMS \
    BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(QGLIB_SIGNAL_IMPL_NUM_ARGS, const A, & a)

# define QGLIB_SIGNAL_IMPL_FUNCTION_ARGS \
    BOOST_PP_ENUM_TRAILING_PARAMS(QGLIB_SIGNAL_IMPL_NUM_ARGS, a)


namespace QGlib {
namespace Private {

//BEGIN ******** boostpp EmitImpl ********

# define QGLIB_SIGNAL_IMPL_PACK_ARGS_STEP(z, n, list) \
    { \
        Value v; \
        v.init<A##n>(); \
        ValueImpl<A##n>::set(v, a##n); \
        list.append(v); \
    }

# define QGLIB_SIGNAL_IMPL_PACK_ARGS(list) \
    BOOST_PP_REPEAT(QGLIB_SIGNAL_IMPL_NUM_ARGS, QGLIB_SIGNAL_IMPL_PACK_ARGS_STEP, list)

template <typename R QGLIB_SIGNAL_IMPL_TRAILING_TEMPLATE_PARAMS>
struct EmitImpl<R (QGLIB_SIGNAL_IMPL_TEMPLATE_ARGS)>
{
    static inline R emit(void *instance, const char *detailedSignal
                         QGLIB_SIGNAL_IMPL_FUNCTION_PARAMS)
    {
        try {
            QList<Value> values;
            QGLIB_SIGNAL_IMPL_PACK_ARGS(values)
            Value returnValue = Private::emit(instance, detailedSignal, values);
            return ValueImpl<R>::get(returnValue);
        } catch(const std::exception & e) {
            qCritical() << "Error during emission of signal" << detailedSignal << ":" << e.what();
            return R();
        }
    }
};

template <QGLIB_SIGNAL_IMPL_TEMPLATE_PARAMS>
struct EmitImpl<void (QGLIB_SIGNAL_IMPL_TEMPLATE_ARGS)>
{
    static inline void emit(void *instance, const char *detailedSignal
                            QGLIB_SIGNAL_IMPL_FUNCTION_PARAMS)
    {
        try {
            QList<Value> values;
            QGLIB_SIGNAL_IMPL_PACK_ARGS(values)
            Value returnValue = Private::emit(instance, detailedSignal, values);
            if (returnValue.isValid()) {
                qWarning() << "Ignoring return value from emission of signal" << detailedSignal;
            }
        } catch(const std::exception & e) {
            qCritical() << "Error during emission of signal" << detailedSignal << ":" << e.what();
        }
    }
};

# undef QGLIB_SIGNAL_IMPL_PACK_ARGS
# undef QGLIB_SIGNAL_IMPL_PACK_ARGS_STEP

//END ******** boostpp EmitImpl ********

} //namespace Private

//BEGIN ******** boostpp QGlib::emit ********

template <typename R QGLIB_SIGNAL_IMPL_TRAILING_TEMPLATE_PARAMS>
R emit(void *instance, const char *detailedSignal QGLIB_SIGNAL_IMPL_FUNCTION_PARAMS)
{
    return QGlib::Private::EmitImpl<R (QGLIB_SIGNAL_IMPL_TEMPLATE_ARGS)>
                             ::emit(instance, detailedSignal QGLIB_SIGNAL_IMPL_FUNCTION_ARGS);
}

//END ******** boostpp QGlib::emit ********

} //namespace QGlib

# undef QGLIB_SIGNAL_IMPL_FUNCTION_ARGS
# undef QGLIB_SIGNAL_IMPL_FUNCTION_PARAMS
# undef QGLIB_SIGNAL_IMPL_TEMPLATE_ARGS
# undef QGLIB_SIGNAL_IMPL_TRAILING_TEMPLATE_ARGS
# undef QGLIB_SIGNAL_IMPL_TEMPLATE_PARAMS
# undef QGLIB_SIGNAL_IMPL_TRAILING_TEMPLATE_PARAMS
# undef QGLIB_SIGNAL_IMPL_NUM_ARGS

#endif // !defined(BOOST_PP_IS_ITERATING) || !BOOST_PP_IS_ITERATING