jeudi 24 août 2017

Creating subclass using templates

I have a design problem I am trying to overcome. Lets assume that the class structure and hierarchy is correct, and I can not change that.

I have a class called BaseWidget. It has internal nested classes Grid, and Square. Grid maintains a vector of squares. They are there to better organise drawing behaviour. BaseWidget constructs the vector of Grid and Squares in its Layout() method.

Now I have Sub-classed BaseWidget with class DerivedWidget. Derived widget has additional drawing requirements for the class BaseWidget::Square. So it would be nice and easy to sub-class BaseWidget::Square with Derived::WidgetSquare.

The problem comes from the fact that the Grids and Squares are constructed in BaseWidget::OnLayout. So when the Grid vector of squares is created I would like to say "use DerivedSquare instead of Square in the vector you are populating".

I believe my solution is to use templates but I am not sure in what capacity.

class BaseWidget : public Widget
{
    protected:
        void Layout(void)
        void Draw(canvas c);

        class Square {
            Square(Rect r) : region(r) {}
            Rect region;
            virtual void Draw(canvas c);
        };

        class Grid {
            std::std::vector<shared_ptr<<Square>> squares;
            Rect region;
            void Draw(canvas c);
        };

        std::vector<Grid> m_grids;
};

void Parent::Layout {
    m_grids.resize(num_grids);

    for (int i=0; i<num_grids; ++i) {

        m_grids[i].region = some_rect;

        for (int j=0; j<num_squares; ++j) {
            m_grids[i].m_squares.push_back(std::make_shared<Square>(some_other_rect));
        }
    }
}

void BaseWidget::Draw(Canvas c) 
{
    for (int i = 0; i < m_grids.size(); i++) {
        m_grids.Draw(c);
    }
}

void Grid::Draw(Canvas c)
{
    // draw some stuff here
    for (int i = 0; i < m_squares.size(); i++) {
        m_squares[i].Draw(c);
    }
}

void Square::Draw(Canvas c)
{
    // draw some stuff here
}



// new class that extends parent
class DerivedWidget : public BaseWidget 
{
    protected:

        /*
         * This class wants to do some special drawing in addition to its parents drawing
         */
        class DerivedSquare : public Square {
            virtual void Draw(canvas c);
        };
}

Aucun commentaire:

Enregistrer un commentaire