1/* Just some code meant to be snipets on
2 * http://woboq.com/blog/reflection-in-cpp-and-qt-moc.html */
3
4#include <QtCore/QObject>
5
6#if 0
7class MyObject : public QObject {
8 Q_OBJECT
9public [[qt::slot]]:
10 void fooBar();
11 void otherSlot(int);
12public [[qt::signal]]:
13 void mySignal(int param);
14public:
15 enum [[qt::enum]] Foobar { Value1, Value2 };
16};
17#endif
18
19/*************************************************************************************************/
20
21#undef Q_PROPERTY
22
23template<typename... Fs> struct QPropertyHolder { template<Fs... Types> struct Property {}; };
24template<typename... Fs> QPropertyHolder<Fs...> qPropertyGenerator(Fs...);
25
26#define WRITE , &ThisType::
27#define READ , &ThisType::
28#define NOTIFY , &ThisType::
29#define MEMBER , &ThisType::
30
31#define Q_PROPERTY(A) Q_PROPERTY_IMPL(A) /* expands the WRITE and READ macro */
32
33#define Q_PROPERTY_IMPL(Prop, ...) static void qt_property_ ## __COUNTER__(\
34 Prop, decltype(qPropertyGenerator(__VA_ARGS__))::Property<__VA_ARGS__>) = delete;
35
36class MyPropObject : public QObject {
37 Q_OBJECT
38 typedef MyPropObject ThisType; // FIXME: how do do that automatically
39 // from within the Q_OBJECT macro?
40
41signals: // would expand to public [[qt::signal]]:
42 void fooChanged();
43public:
44 QString foo() const;
45 void setFoo(const QString&);
46
47 Q_PROPERTY(QString foo READ foo WRITE setFoo NOTIFY fooChanged)
48
49};
50
51/*************************************************************************************************/
52
53struct Obj { void func(); };
54template<void (Obj::*)()> struct Trait {};
55int main() {
56 Trait<&Obj::func> t1; //Ok. The function is directly written
57
58 constexpr auto var = &Obj::func;
59 Trait<var> t2; //Error: var is not a function directly written.
60}
61
62/*************************************************************************************************/
63
64using namespace std;
65
66/* Given a simple class */
67class SomeClass {
68public:
69 int foo();
70 void bar(int x);
71};
72
73#if 0
74/* The new typename<>... and typedef<>... 'operators' : */
75 vector<string> names = { typename<SomeClass>... } ;
76 auto members = std::make_tuple(typedef<SomeClass>...) ;
77#else
78/* Would be expanded to something equivalent to: */
79 vector<string> names = { "SomeClass", "foo", "bar" };
80 auto members = std::make_tuple(static_cast<SomeClass*>(nullptr),
81 &SomeClass::foo, &SomeClass::bar);
82#endif
83
84/*************************************************************************************************/
85
86#if 0
87int signalId=0;
88/* Somehow loop over all the signals to implement them (made up syntax) */
89for(auto signal : {typedef<MyObject requires has_attribute("qt::signal")>... }) {
90 signalId++;
91 signal(auto... arguments) = {
92 SignalImplementation<decltype(signal), signalId>::impl(this, arguments...);
93 }
94}
95#endif
96