通过基类自动化窗口编程

问题描述 投票:0回答:1

我为一个应用程序编写了几个窗口,它们都继承了Gtkmm::Window。在这一点上,我想自动化这个过程。现在,以下结构突出:

class MyWindow : public Gtk::Window
{
public:
    MyWindow();
    virtual ~MyWindow();

    //...

private:
    void registerLayouts(); // Adds layouts to the window.
    void registerWidgets(); // Adds widgets to the layouts.

    //...
};

和构造函数:

MyWindow::MyWindow()
{
    registerLayouts(); // Cannot be virtual: in constructor.
    registerWidgets(); // Cannot be virtual: in constructor.

    //...
}

所以问题是所有这一切必须在每次必须编程新窗口时手动完成(即复制/粘贴),因为registerLayouts()registerWidgets()在构造时被调用,因此不能是virtual

理想情况下,我会有一个我可以继承的基类,它可以让我选择覆盖这两个方法,并会处理其余的方法:它会在适当的位置调用这两个方法。

问题是,我还没有找到合适的位置。我看过不同的信号处理程序,但似乎没有。

你知道我怎么做吗?

MFC有CDialog::OnInitDialog()执行类似于我需要的东西。

c++ constructor widget gtkmm gtkmm3
1个回答
1
投票

您可以将工作委托给单独的类:

class MyWindow : public Gtk::Window
{
//public:  *** EDIT ***
protected:
    template <typename LayoutManager>
    MyWindow(LayoutManager const& lm)
    {
        lm.registerLayouts(this);
        lm.registerWidgets(this);
    }
};

class SubWindow : public MyWindow
{
    class LM { /* ... */ };
public:
     SubWindow() : MyWindow(LM()) { }
};

(编辑:改进的模式隐藏了公共子类的布局管理器...)

或者,整个类作为模板 (可能优于上述) :

template <typename LayoutManager>
class MyWindow : public Gtk::Window
{
public:
    MyWindow()
    {
        LayoutManager lm(*this);
        lm.registerLayouts();
        lm.registerWidgets();
    }
};

class SpecificLayoutManager { /* ... */ };
using SpecificWindow = MyWindow<SpecificLayoutManager>;

如果您还需要布局管理器进行清理(我自己不熟悉GTK):

template <typename LayoutManager>
class MyWindow : public Gtk::Window
{
    LayoutManager lm;
public:
    MyWindow() : lm(*this)
    {
        lm.registerLayouts();
        lm.registerWidgets();
    }
    virtual ~MyWindow()
    {
        // still access to lm...
    }
};

重要的注意事项:在所有变体中,我们还没有完全构造的派生类 - 在布局管理器中向后者投射因此是不合法的(用curiously recurring template pattern进行实验,但出于完全相同的原因而抛弃了这个想法:需要转换为派生类在基地的构造者中)。

编辑以回应评论:关于如何管理子类的其他成员的示例(使用上面的第三个变体,使用布局管理器成员的模板类; lm成员现在需要是protected):

class SubWindowLayoutManager
{
    template <typename>
    friend class MyWindow;
    friend class SubWindow;

    int someMember;

    void registerLayouts() { }
    void registerWidgets() { }

};
class SubWindow : public MyWindow<SubWindowLayoutManager>
{
    void doSomething()
    {
        lm.someMember = 77;
    }
};

另外一个完全没有模板的新变种:

class MyWindow : public Gtk::Window
{
protected:
    class LayoutManager
    {
    public:
         virtual void registerLayouts(MyWindow* parent) = 0;
         virtual void registerWidgets(MyWindow* parent) = 0;
    };

    std::unique_ptr<LayoutManager> lm;

    MyWindow(std::unique_ptr<LayoutManager> lm)
        : lm(std::move(lm))
    {
        this->lm->registerLayouts(this);
        this->lm->registerWidgets(this);
    }
};

class SubWindow : public MyWindow
{
    class LM : public LayoutManager
    {
    public:
        void registerLayouts(MyWindow* parent) override { }
        void registerWidgets(MyWindow* parent) override { }

        int someMember;
    };

    // convenience access function:
    inline LM& lm()
    {
        return *static_cast<LM*>(MyWindow::lm.get());
    }

public:
    SubWindow() : MyWindow(std::make_unique<LM>()) { }

    void doSomething()
    {
        //static_cast<LM*>(lm.get())->someMember = 77;
        lm().someMember = 77;
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.