Pimpl copying

André Wöbbeking Woebbeking at onlinehome.de
Fri Jul 14 16:35:05 BST 2006


On Friday 14 July 2006 16:42, Peter Kümmel wrote:
> Peter Kümmel wrote:
> > The problem is that KTempDirTest uses the Pimpl idiom, but does
> > not define a copy ctor/operator, so all private data gets lost
> > on copying, nor does it forbid copying.
> >
> > I fear kdelibs is full of this bug and think we should catch
> > such constructs at compile time.
> >
> > Shouldn't we introduce a coding policy something like this:
>
> I've solved the issue by this patch:
>
> SVN commit 562284 by kuemmel:
>
> macro for pimpl handling:
>  * consistent naming of the private implementation
>  * explicit copying, checked by the linker
>  * disable copying, checked at compile time
>  * const pimpl
>
>
>  M  +1 -0      TODO
>  M  +2 -2      kdecore/ktempdir.cpp
>  M  +1 -2      kdecore/ktempdir.h
>  M  +2 -2      kdecore/tests/ktempdirtest.cpp
>  M  +62 -0     kdemacros.h.cmake

> --- trunk/KDE/kdelibs/TODO #562283:562284
> @@ -10,6 +10,7 @@
>    help prevent mistakes where developers forget to delete the
> pointer. Maybe make use of
>    Qt4 helper macros?
>    (Frerich)
> +  Use KDE_PIMPL_EN(DIS)ABLE_COPY to avoid compiler generated copy
> functions (Kümmel)
>
>  - Move all utility functions away from KApplication. TBD: Make
> KApplication a very thin wrapper around QApplication. Ideally,
> KApplication should go --- trunk/KDE/kdelibs/kdecore/ktempdir.cpp
> #562283:562284
> @@ -53,7 +53,7 @@
>  #include <kdebug.h>
>  #include "kde_file.h"
>
> -class KTempDir::Private
> +class KTempDir::KTempDirPrivate

Many pimpls were just changed to (nested) Private.

> +
> +#define KDE_PIMPL_BASE_MACRO(COPY_ACCESS,X) \
> +COPY_ACCESS: \
> +    X(const X##&); \
> +    X##& operator=(const X##&); \
> +private: \
> +    class X##Private; \
> +    X##Private * const d;
> +
> +#define KDE_PIMPL_ENABLE_COPY(X)  KDE_PIMPL_BASE_MACRO(public, X)
> +#define KDE_PIMPL_DISABLE_COPY(X) KDE_PIMPL_BASE_MACRO(private,X)

Does't KDE_PIMPL_DISABLE_COPY lead to unresolved smybols linking error 
with MSVC (see "unresolved symbols from value.cpp/.h" on k-c-d from 
20.01.2006)?




More information about the kde-core-devel mailing list