Replacing RESTORE() macro with template functions.
Marc Mutz
mutz at kde.org
Mon Dec 2 12:36:54 GMT 2002
Hi!
RESTORE() has the drawback that it can only be used with a single type
of mainwindow. ATM, I'm not sure if you can overload cpp macros w.r.t.
the number of arguments, but even if you could, they'd still be
_macros_ (bah! :-).
It also has the drawback that it fails for namespaced mainwindows:
#include "mainwindow.h"
using myApp::MainWindow;
int main( int, char** ) {
//...
if ( kapp->isRestored() )
RESTORE(MainWindow); // fails
else
// ...
// ...
}
Here's an alternative that should be BC and SC and with which the above
should work:
#define RESTORE(type) kRestoreMainWindows<type>();
template <typename T1>
static inline kRestoreMainWindows() {
for ( int n = 1 ; KMainWindow::canBeRestored( n ) ; ++n )
if ( T1::staticMetaObject()->className()
== KMainWindow::classNameOfTopLevel( n ) )
(new T1)->restore( n );
}
template <typename T1, typename T2>
static inline kRestoreMainWindows() {
for ( int n = 1 ; KMainWindow::canBeRestored( n ) ; ++n )
if ( T1::staticMetaObject()->className()
== KMainWindow::classNameOfTopLevel( n ) )
(new T1)->restore( n );
else if ( T2::staticMetaObject()->className()
== KMainWindow::classNameOfTopLevel( n ) )
(new T2)->restore( n );
}
template <typename T1, typename T2, typename T3>
static inline kRestoreMainWindows() {
for ( int n = 1 ; KMainWindow::canBeRestored( n ) ; ++n )
if ( T1::staticMetaObject()->className()
== KMainWindow::classNameOfTopLevel( n ) )
(new T1)->restore( n );
else if ( T2::staticMetaObject()->className()
== KMainWindow::classNameOfTopLevel( n ) )
(new T2)->restore( n );
else if ( T3::staticMetaObject()->className()
== KMainWindow::classNameOfTopLevel( n ) )
(new T3)->restore( n );
}
#define KDE_MAX_KRESTORE_MAINWINDOWS_TEMPLATE_ARGS 3
// etc.. If an app author needs more, he can say so and we'd add it for
the next release while she does:
if ( kapp->isRestored() ) {
#if defined(KDE_MAX_KRESTORE_MAINWINDOWS_TEMPLATE_ARGS) && \
KDE_MAX_KRESTORE_MAINWINDOWS_TEMPLATE_ARGS >= 5
kRestoreMainWindows<myWin1,myWin2,myWin3,myWin4,myWin5>();
#else
//.. do it by hand
#endif
} else {
//...
}
Marc
--
memAlloc() Amnesia Error: Out of Memory
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: signature
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20021202/a86b3660/attachment.sig>
More information about the kde-core-devel
mailing list