template based d pointer class

Alexander Kellett lypanov at kde.org
Sat May 17 13:53:46 BST 2003


any thoughts on the following. would make it
much easier to define private data members when
no d pointer was added at class invention.

USAGE:
------

class KBookmarkBarPrivate : public Private<KBookmarkBar, KBookmarkBarPrivate> {
public:
    QPtrList<KAction> m_actions;
};
QPtrDict<KBookmarkBarPrivate>* Private<KBookmarkBar, KBookmarkBarPrivate>::d_ptr = 0;

#define d() KBookmarkBarPrivate::d(this)

IMPLEMENTATION: (most code from seli's developer.kde.org page on bic)
---------------

template<class KBookmarkBar, class KBookmarkBarPrivate>
class Private {
public:
    static KBookmarkBarPrivate* d( const KBookmarkBar* instance ) 
    {
        if ( !d_ptr ) {
            d_ptr = new QPtrDict<KBookmarkBarPrivate>;
            qAddPostRoutine( cleanup_d_ptr );
        }
        KBookmarkBarPrivate* ret = d_ptr->find( (void*) instance );
        if ( ! ret ) {
            ret = new KBookmarkBarPrivate;
            d_ptr->replace( (void*) instance, ret );
        }
        return ret;
    }
    static void delete_d( const KBookmarkBar* instance ) 
    {
        if ( d_ptr )
            d_ptr->remove( (void*) instance );
    }
private:
    static void cleanup_d_ptr() 
    {
        delete d_ptr;
    }
    static QPtrDict<KBookmarkBarPrivate>* d_ptr;
};

(completely untested and the above won't 
 say KBookmarkBar everywhere of course)

Alex




More information about the kde-core-devel mailing list