summaryrefslogtreecommitdiffstats
path: root/installerbuilder/libinstaller/kdtools/KDToolsCore/kdmetamethoditerator.cpp
blob: 1a0b863c7a2ba161a2dc82bcc052ea2d51fb3669 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/****************************************************************************
** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
**
** This file is part of the KD Tools library.
**
** Licensees holding valid commercial KD Tools licenses may use this file in
** accordance with the KD Tools Commercial License Agreement provided with
** the Software.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU Lesser General Public License version 2 and version 3 as published by the
** Free Software Foundation and appearing in the file LICENSE.LGPL included.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Contact info@kdab.com if any conditions of this licensing are not
** clear to you.
**
**********************************************************************/

#include "kdmetamethoditerator.h"

/*!
 \class KDMetaMethodIterator
 \ingroup core
 \brief Iterator over methods of QObjects
 \since_c 2.3

 KDMetaMethodIterator provides a way over all methods of a QObject which can be accessed via Qt's meta
 object system. It is possible to filter on types of methods (Q_INVOKABLE methods, signals, slots, 
 constructors) and on access types (public, protected, private). Furthermore, KDMetaMethodIterator can
 be configured to filter methods of the given object's base class or all methods provided by QObject.

 \section general-use General Use

 The following example shows a general use case of KDMetaMethodIterator:

 \code

 class QIODeviceWrapper : public QIODevice
 {
     Q_OBJECT
 public:
     QIODeviceWrapper( QIODevice* nestedDevice )
        : nested( nestedDevice )
    {
        // make sure all signals of the nested QIODevice are forwarded,
        // but make sure not to connect QObject's own destroyed() signal,
        // therefore use KDMetaMethodIterator::IgnoreQObjectMethods
        KDMetaMethodIterator it( QIODevice::staticMetaObject, KDMetaMethodIterator::Signal, KDMetaMethodIterator::IgnoreQObjectMethods );
        while( it.hasNext() )
        {
            it.next();
            connect( nested, it.connectableSignature(), this, it.connectableSignature() );
        }
    }

 protected:
    // virtual methods needed to access the contents...

 private:
     QIODevice* const nested;
 };

 \endcode
*/

/*!
 \enum KDMetaMethodIterator::AccessType
 This enum describes the access type of methods to iterate:
*/

/*!
 \var KDMetaMethodIterator::Private
 Iterate over private methods.
*/

/*!
 \var KDMetaMethodIterator::Protected
 Iterate over protected methods.
*/

/*!
 \var KDMetaMethodIterator::Public
 Iterate over public methods.
*/

/*!
 \var KDMetaMethodIterator::AllAccessTypes
 Iterate over methods of all access types.
*/

/*!
 \enum KDMetaMethodIterator::MethodType
 This enum describes the type of methods to iterate:
*/

/*!
 \var KDMetaMethodIterator::Method
 Iterate over methods marked with Q_INVOKABLE
*/

/*!
 \var KDMetaMethodIterator::Signal
 Iterate over signals.
*/

/*!
 \var KDMetaMethodIterator::Slot
 Iterate over slots.
*/

/*!
 \var KDMetaMethodIterator::Constructor
 Iterate over constructors marked with Q_INVOKABLE.
*/

/*!
 \var KDMetaMethodIterator::AllMethodTypes
 Iterate over all supported method types.
*/

/*!
 \enum KDMetaMethodIterator::IteratorFlag
 This enum describes flags which can be passed to KDMetaMethodIterator:
*/

/*!
 \var KDMetaMethodIterator::NoFlags
 No flags set, only type filtering takes place.
*/

/*!
 \var KDMetaMethodIterator::IgnoreQObjectMethods
 KDMetaMethodIterator will not iterate over methods provided by QObject itself.
*/

/*!
 \var KDMetaMethodIterator::IgnoreSuperClassMethods
 KDMetaMethodIterator will only iterate over methods provided by the passed class instance itself, ignoring any base classes.
*/

/*!
 \internal
 */
class KDMetaMethodIterator::Priv
{
public:
    Priv( const QMetaObject* object, MethodTypes types, AccessTypes access, IteratorFlags flags )
        : index( -1 ),
          m( types ),
          a( access ),
          f( flags ),
          metaObject( object )
    {
    }

    Priv( const QMetaObject* object, MethodTypes types, IteratorFlags flags )
        : index( -1 ),
          m( types ),
          a( ( types & Signal ) ? ( Protected | Public ) : Public ),
          f( flags ),
          metaObject( object )
    {
    }

    bool filterMatches( const QMetaMethod& method, int index ) const;

    int index;
    const MethodTypes m;
    const AccessTypes a;
    const IteratorFlags f;
    const QMetaObject* const metaObject;

    mutable QByteArray connectableSignature;
};

/*!
 Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types.
 By default, only public methods will be returned. If \a types contains Signal, the access type filter will automatically set 
 to return protected and public. 
 
 By default, \a flags is NoFlags, which lists all methods.
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject& metaObject, MethodTypes types, IteratorFlags flags )
    : d( new Priv( &metaObject, types, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types,
 matching the \a access mode.
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject& metaObject, MethodTypes types, AccessType access, IteratorFlags flags )
    : d( new Priv( &metaObject, types, access, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods matching the \a access mode.
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject& metaObject, AccessType access, IteratorFlags flags )
    : d( new Priv( &metaObject, AllMethodTypes, access, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types.
 By default, only public methods will be returned. If \a types contains Signal, the access type filter will automatically set 
 to return protected and public. 
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject* metaObject, MethodTypes types, IteratorFlags flags )
    : d( new Priv( metaObject, types, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods of the given \a types,
 matching the \a access mode.
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject* metaObject, MethodTypes types, AccessType access, IteratorFlags flags )
    : d( new Priv( metaObject, types, access, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a metaObject. The iterator will only return methods matching the \a access mode.
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QMetaObject* metaObject, AccessType access, IteratorFlags flags )
    : d( new Priv( metaObject, AllMethodTypes, access, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a object. The iterator will only return methods of the given \a types.
 By default, only public methods will be returned. If \a types contains Signal, the access type filter will automatically set 
 to return protected and public. 
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QObject* object, MethodTypes types, IteratorFlags flags )
    : d( new Priv( object->metaObject(), types, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a object. The iterator will only return methods of the given \a types,
 matching the \a access mode.
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QObject* object, MethodTypes types, AccessType access, IteratorFlags flags )
    : d( new Priv( object->metaObject(), types, access, flags ) )
{
}

/*!
 Creates a new KDMetaMethodIterator iterating over \a object. The iterator will only return methods matching the \a access mode.
 
 By default, \a flags is NoFlags, which lists all methods.

 \overload
 */
KDMetaMethodIterator::KDMetaMethodIterator( const QObject* object, AccessType access, IteratorFlags flags )
    : d( new Priv( object->metaObject(), AllMethodTypes, access, flags ) )
{
}

/*!
 Destroys the KDMetaMethodIterator.
 */
KDMetaMethodIterator::~KDMetaMethodIterator()
{
}

/*!
 Returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the method list; otherwise returns false.
 */
bool KDMetaMethodIterator::hasNext() const
{
    for( int i = d->index + 1; i < d->metaObject->methodCount(); ++i )
    {
        const QMetaMethod method = d->metaObject->method( i );
        if( d->filterMatches( method, i ) )
            return true;
    }
    return false;
}

/*!
 Returns the next method and advances the iterator by one position.
 */
QMetaMethod KDMetaMethodIterator::next()
{
    Q_ASSERT( hasNext() );
    d->connectableSignature.clear();
    for( ++d->index; d->index < d->metaObject->methodCount(); ++d->index )
    {
        const QMetaMethod method = d->metaObject->method( d->index );
        if( d->filterMatches( method, d->index ) )
            return method;
    }
    return QMetaMethod();
}

/*!
 Returns the current method's signature ready to be passed to QObject::connect, i.e. already passed through the SIGNAL or SLOT macro.
 The return value stays valid up to the following next() call. This is only valid for signals and slots.
 */
const char* KDMetaMethodIterator::connectableSignature() const
{
    if( d->connectableSignature.isEmpty() )
    {
        const QMetaMethod method = d->metaObject->method( d->index );
        d->connectableSignature = method.signature();
        d->connectableSignature.push_front( QString::number( ( static_cast< int >( method.methodType() ) - 3 ) * -1 ).toLocal8Bit() );
    }
    return d->connectableSignature.constData();
}

/*!
 Returns true if \a method matches the filters set on the iterator, currently pointing to \a index.
 \internal
 */
bool KDMetaMethodIterator::Priv::filterMatches( const QMetaMethod& method, int index ) const
{
    // filter QObject's own methods
    if( ( f & IgnoreQObjectMethods ) && index < QObject::staticMetaObject.methodCount() )
        return false;
    
    // filter any super classes methods
    if( ( f & IgnoreSuperClassMethods ) && index < metaObject->methodOffset() )
        return false;

    // filter any methods with unwanted access types
    if( ( ( 1 << method.access() ) & a ) == 0 )
        return false;

    // filter any methods with unwanted method types
    return ( ( 1 << method.methodType() ) & m ) != 0;
}

#ifdef KDTOOLSCORE_UNITTESTS

#include <KDUnitTest/Test>

class TestClass : public QObject
{
    Q_OBJECT
public:
    TestClass(){}

public Q_SLOTS:
    void publicSlot( int ) {}

protected Q_SLOTS:
    void protectedSlot( int ) {}

private Q_SLOTS:
    void privateSlot( int ) {}

private:
    Q_PRIVATE_SLOT( d, void veryPrivateSlot() )
    
    class Private;
    kdtools::pimpl_ptr< Private > d;
};

class TestClassDerived : public TestClass
{
    Q_OBJECT
public:
    TestClassDerived(){}
};

class TestClass::Private
{
public:
    void veryPrivateSlot() {}
};

KDAB_UNITTEST_SIMPLE( KDMetaMethodIterator, "kdcoretools" ) {

    QObject o;
    {
        KDMetaMethodIterator it( &o, KDMetaMethodIterator::Signal );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "destroyed()" );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( QObject::staticMetaObject, KDMetaMethodIterator::Signal );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" );
    }
    {
        KDMetaMethodIterator it( o.metaObject(), KDMetaMethodIterator::Signal );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" );
    }

    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Signal );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "destroyed(QObject*)" );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "destroyed()" );
        assertEqual( std::string( it.connectableSignature() ), "2destroyed()" );
        assertFalse( it.hasNext() );
    }

    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Signal, KDMetaMethodIterator::IgnoreQObjectMethods );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Signal, KDMetaMethodIterator::IgnoreSuperClassMethods );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "deleteLater()" );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "publicSlot(int)" );
        assertEqual( std::string( it.connectableSignature() ), "1publicSlot(int)" );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::IgnoreQObjectMethods );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "publicSlot(int)" );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Protected, KDMetaMethodIterator::IgnoreQObjectMethods );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "protectedSlot(int)" );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( TestClass::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Private, KDMetaMethodIterator::IgnoreQObjectMethods );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "privateSlot(int)" );
        assertEqual( std::string( it.next().signature() ), "veryPrivateSlot()" );
        assertFalse( it.hasNext() );
    }

    {
        KDMetaMethodIterator it( TestClassDerived::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Private, KDMetaMethodIterator::IgnoreQObjectMethods );
        assertTrue( it.hasNext() );
        assertEqual( std::string( it.next().signature() ), "privateSlot(int)" );
        assertEqual( std::string( it.next().signature() ), "veryPrivateSlot()" );
        assertFalse( it.hasNext() );
    }
    {
        KDMetaMethodIterator it( TestClassDerived::staticMetaObject, KDMetaMethodIterator::Slot, KDMetaMethodIterator::Private, KDMetaMethodIterator::IgnoreSuperClassMethods );
        assertFalse( it.hasNext() );
    }

}

#include "kdmetamethoditerator.moc"

#endif // KDTOOLSCORE_UNITTESTS