c++ - 在 C++ 中静默地将一个变量实例绑定(bind)到一个类?

标签 c++

所以我正在编写一个基于插件的系统。用户可以创建插件类的子类,然后它将在运行时加载并与系统的其余部分集成。当一个插件从系统运行时,它在一组插件的上下文中运行,我称之为 session 。

我的问题是,在用户插件中,可以使用两个名为 pf_ostream 和 pf_istream 的流类来向系统读取/写入数据。我想以某种方式将插件实例的 session 变量绑定(bind)到 pf_ostream 和 pf_istream 以便当用户实例化这些类时,它已经为他们绑定(bind)到 session (基本上我不希望他们看到 session 内部)

我可以只用一个宏来做到这一点,包装对构造函数的调用,例如:

#define MAKE_OSTREAM = pf_ostream_int(this->session)

但我认为可能有更好的方法。我查看了在包装 pf_ostream 的插件中使用嵌套类,但嵌套类似乎无法以某种闭包方式访问封闭类变量。

有谁知道一个巧妙的方法来做到这一点?

最佳答案

这是另一个基于您在其中一条评论中提到的工厂想法的答案。它使用 Facade 模式在绑定(bind)到 SessionSystem 类中集中创建系统设施:

#include <boost/shared_ptr.hpp>

class System
{
public:
    typedef boost::shared_ptr<pf_ostream> pf_ostream_ptr;
    typedef boost::shared_ptr<pf_istream> pf_istream_ptr;
    // Other system facilities...

    System(Session* session) : session_(session) {}

    pf_ostream_ptr makeOstream()
        {return pf_ostream_ptr(new pf_ostream(session_));}

    pf_istream_ptr makeIstream()
        {return pf_istream_ptr(new pf_istream(session_));}

private:
    Session* session_;
};


class Plugin
{
public:
    Plugin(System* system) : system_(system) {}

    System& system() {return *system_;}

private:
    System* system_;
};


class MyPlugin : public Plugin
{
public:
    MyPlugin(System* system) : Plugin(system) {}

    void print()
    {
        pf_ostream_ptr pfos( system().makeOstream() );
        *pfos << "Hello World!\n";
        // pfos will be deleted automatically at the end of this scope
    }
};

如果您有很多系统设施,您应该明智地使用前向声明以避免编译时间过长。该解决方案的缺点是将您的系统设施集中在一个依赖性“焦点”(System 类)中。如果 System 类在使用或多或少系统设施的另一个应用程序中重新使用,则可能需要更改它。

编辑:

以下是如何应用代理模式(以及 Pimpl 习惯用法)来获得具有值语义的引用计数流类:

#include <boost/shared_ptr.hpp>

class pf_ostream
{
public:
    pf_ostream(Session* session);
    pf_ostream& operator<<(int rhs);
    // Use of default copy-constuctor and assignment operator
    //    will work fine because of shared_ptr.

private:
    struct Impl;
    boost::shared_ptr<Impl> pimpl_;
};

// In cpp file

struct pf_ostream::Impl
{
    Impl(Session* session) : session(session) {}
    void insert(int rhs) {/*...*/}
    Session* session;
};

pf_ostream::pf_ostream(Session* session) : pimpl_(new Impl(session)) {}

pf_ostream& pf_ostream::operator<<(int rhs) {pimpl_.insert(rhs); return *this;}

用户必须知道代理对象的拷贝将引用相同的真实流。希望这会有所帮助。

关于c++ - 在 C++ 中静默地将一个变量实例绑定(bind)到一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2676658/

相关文章:

c++ - 没有可行的重载+ =,为什么?

c++ - 为什么-1 > sizeof(int)?

c++ - 操作系统 : How to statically link a library and dynamically link the standard library?

c++ - 通过查找位返回一个数组?

c++ - 在安全关键系统中测试 C++17

c++ - 如何在我的 C 代码中访问我的 C++ 函数,反之亦然?

c++ - 如何在 Armadillo 中创建一个nxn Unity矩阵?

c++ - 我是否在 WinApi 中正确执行动画?

c++ - 用于 MSVC 的多线程 malloc

c++ - 构造函数和析构函数对 vTable 的 undefined reference