将回调传递给组件列表 c++ 的包装器

发布于 2025-01-06 16:34:52 字数 923 浏览 2 评论 0原文

我有一个遗留软件,它有一个管理器来控制程序的运行。它在程序执行的整个过程中调用各种回调类的方法。这些是用户挂钩。问题是这 1000 个不同的 hook 中 1)它们显然都有不同的界面 2) 运行管理器只有一个插槽用于其中一个。

我注意到,只允许用户向运行管理器注册每个用户挂钩类的一个实例,会导致我的团队生成大量意大利面条式代码。我想编写包含钩子列表的简单包装器,然后循环该列表并调用每个实例的回调。示例:

class SomeLegacyUserActionClass
{
public:
    virtual void A();
    virtual void B();
};

class MyWrapper : public SomeLegacyUserActionClass
{
    std::vector< SomeLegacyUserActionClass* > actionList;
public:
    void A()
    {
        // loop over each action in actionList
        {
            action->A();
        }
    }

    void B()
    {
        // loop over each action in actionList
        {
           action->B();
        }
    }

    void addAction( SomeLegacyUserActionClass* action ) { ... }
};

对于如此多的类来说,这变得非常乏味和丑陋。有什么方法可以让我制作一个模板类或其他东西来一击完成此任务吗?这里显然有一个模式,我只是不知道我是否可以在 C++ 中以某种方式利用它。

我想我可以让我的团队为他们的所有操作实现某种装饰器模式,并消除向量和循环。

谢谢

I have a legacy piece of software that has a single manager which controls a run of the program. It calls methods of various callback classes throughout the execution of the program. These are the user hooks. The problem is that of these 1000 different hooks is that
1) they each obviously have a different interface
2) the run manager only has a slot for one of each.

I have noticed that only allowing for the user to register just one instance of each user hook-in class with the run manager results in a lot of spaghetti code from my group. I would like to write simple wrappers that contain a list of hookins, then loop over the list and call the callback of each instance. Example:

class SomeLegacyUserActionClass
{
public:
    virtual void A();
    virtual void B();
};

class MyWrapper : public SomeLegacyUserActionClass
{
    std::vector< SomeLegacyUserActionClass* > actionList;
public:
    void A()
    {
        // loop over each action in actionList
        {
            action->A();
        }
    }

    void B()
    {
        // loop over each action in actionList
        {
           action->B();
        }
    }

    void addAction( SomeLegacyUserActionClass* action ) { ... }
};

This is becoming very tedious and ugly to do with so many classes. Is there some way I can make a template class or something to do this in one fell blow? There is obviously a pattern here, I just don't know if I can capitalize on it somehow in c++.

I guess I could get my group to implement some kind of decorator pattern for all their actions and do away with the vectors and loops.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2025-01-13 16:34:52

这对于模板来说是不可能的,因为无法检索在模板中使用的类型的成员函数列表。如果您确实有很多类,那么使用经典代码生成可能是明智的。

This is not possible with templates, because there is no way to retrieve the list of member-functions of a type for use in a template. If you really have a lot of classes, it might be sensible to use classic code-generation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文