In the 1.6 cycle I'd like to get rid of the 'Dimension' data member in
Inset. As a lot of insets need that member and derivation from a
'common' 'DimInset' base was impractical I am thinking about using the
old ATL trick:


        #include <iostream>

        using namespace std;

        // Base of the trick: Data _and_ implementation of the feature.
        template <class Base>
        struct DimStuff : Base
        {
                DimStuff() : w(30) {}
                int width() const { return w; }
                void setWidth(int ww) { w = ww; }
                int w;
        };


        // Business as usual
        struct Inset
        {
                void metrics() { cout << width() << ' '; }
                // Note: no need to invent dummy implementions here.
                virtual int width() const = 0;
                virtual void setWidth(int) = 0;
                virtual ~Inset() {}
        };


        // Business as usual
        struct SlimInset : Inset
        {
                int width() const { return 42; }
                void setWidth(int) { cout << "cannot do that"; }
        };


        struct FatBase : Inset
        {
                // Note that we do not 'reimplement' widht/setWidth
                // at all. Nevertheless, we can 'use' it everywhere
                // (outside of constructors and destructor)
                void foo() { setWidth(20); cout << "foo: " << width() << " "; }
        };


        // We 'add' data _and_ implementation here. And we can do that
        // at any place in the normal hierarchy of insets.
        typedef  DimStuff<FatBase>  FatInset;


        // Test case:

        int main()
        {
                FatInset * d = new FatInset;
                d->foo();
                d->metrics();
                d->setWidth(50);
                d->metrics();

                Inset * s = new SlimInset;
                s->metrics();
                s->setWidth(50);
                s->metrics();
        }


It is pretty compact and even several 'features' can be added like that
without influencing the inset hierarchy. 

Just want you to get used to the thought ;-)

Andre'

Reply via email to