1 /*
2     SuperCollider Language
3     Copyright (c) 2018 SuperCollider Team
4     https://supercollider.github.io/
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 
21 #pragma once
22 
23 #include <QPointer>
24 
25 namespace QtCollider {
26 
27 class QcCallback;
28 
29 class QcCallbackWeakFunctor {
30 public:
31     QcCallbackWeakFunctor(QPointer<QcCallback> cb): _cb(cb) {}
32 
33     template <typename RESULT> void operator()(RESULT r) const;
34 
35 private:
36     QPointer<QcCallback> _cb;
37 };
38 
39 class QcCallback : public QObject {
40     Q_OBJECT
41 
42 public:
43     QcCallback() {}
44 
45     template <typename CallbackT> void call(const CallbackT& result) { Q_EMIT(onCalled(result)); }
46 
47     QcCallbackWeakFunctor asFunctor() { return QcCallbackWeakFunctor(QPointer<QcCallback>(this)); }
48 
49 Q_SIGNALS:
50     void onCalled(bool);
51     void onCalled(const QString&);
52     void onCalled(const QVariant&);
53     void onCalled(const QUrl&);
54 };
55 
56 template <typename RESULT> void QcCallbackWeakFunctor::operator()(RESULT r) const {
57     if (_cb) {
58         _cb->call(r);
59     }
60 }
61 
62 } // namespace QtCollider
63 
64 Q_DECLARE_METATYPE(QtCollider::QcCallback*);
65