template based d pointer class
Ravikiran Rajagopal
ravi at ee.eng.ohio-state.edu
Sat May 17 20:35:27 BST 2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,
This seems like a fairly complicated way to store extra data members. As far
as I can see, what you have is a Singleton to hold instances of the private
data for the class with no d-pointer.
One simpler(?) solution would be to define a generic singleton pattern
template class [1] that would be usable much more widely in KDE. Then, derive
another template class from the singleton which acts as a registrar (sorry, I
forgot the name for this kind of pattern) which is similar to your proposed
template here but would be safer as it cannot be instantiated multiple times.
If you don't like singletons, you can add a static member (to the class
lacking the d-pointer). Either way, you would have to remember to register
your instantiations in each constructor, though.
A nitpick: instead of the #define, it would be better to add a private const
member function. See http://www.gotw.ca/gotw/012.htm for a similarly flavored
example.
Anyway, are there any classes with publicly available API without
d-pointers?
[1] Is Simon Hausmann the person who typically ports Loki code to kdelibs? I
have been wanting to add a couple of Alexei Andrescu's pattern
implementations as well as a little more typelist functionality to kdelibs.
It would not increase binary code size (unless you instantiate the templates
elsewhere).
Regards,
Ravi
> 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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE+xo8IbI8Y8y0oVXcRAorrAKCOMnu/t9sdFyAly8X3nieAyplPFACfQSNB
RgF3kAPdRbHT9ybye9ZxGzk=
=mHco
-----END PGP SIGNATURE-----
More information about the kde-core-devel
mailing list