操作队列

发布于 2024-10-08 05:02:18 字数 349 浏览 1 评论 0原文

我正在创建一个用于配置文件处理的类。我希望它的工作方式是:

有一个队列保留所有操作(插入数据、擦除数据...),然后,方法 Commit 将应用所有这些更改。

我的原型看起来像这样(文件上的所有设置都存储到 STL 映射中):

bool Insert(std::string const& key, std::string const& value);
bool Erase(std::string const& key);

因此,我尝试创建一个指向函数的指针的 STL 队列,但这些函数没有相同数量的参数,并且我不知道该怎么办...

I'm creating a class for configuration file handling. The way I want it to work is:

There's a queue that retains all the operations (insert data, erase data...), then, the method Commit will apply all those changes.

My prototypes looks like that (all the settings on the file are stored into a STL map):

bool Insert(std::string const& key, std::string const& value);
bool Erase(std::string const& key);

So, I've tried to create an STL queue of pointer to functions, but the functions don't have the same number of arguments, and I don't know what to do...

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

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

发布评论

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

评论(1

長街聽風 2024-10-15 05:02:18

您可以创建一个Operation 基类,然后派生两次以创建Insert 和Erase 类。然后您可以存储指向操作类的指针。

class Settings
{
public:
    bool Insert(std::string const& key, std::string const& value)
    {
        m_operations.push_back(new Insert(key, value));
        return true; //?      
    }

    bool Erase(std::string const& key)
    {
        m_operations.push_back(new Erase(key));
        return true; //?
    }

    bool Commit()
    {
        // You know what to do by now right ?
    }

private:
    class Operation
    {
    public:
        virtual void Execute() = 0
    }

    class Insert : public Operation
    {
    public:
        Insert(std::string const& key, std::string const& value) : 
            m_key(key), m_value(value) {}

        void Execute() {...}
    private:
        std::string m_key;
        std::string m_value;
    }

    class Erase : public Operation
    {
    public:
        Erase(std::string const& key) : m_key(key) {}

        void Execute() {...}
    private:
        std::string m_key;
    }

    std::queue<Operation*> m_operations;
    std::map<std::string, std::string> m_settings;
}

然后,您可以使操作类成为设置类的友元,或者将引用传递给应应用操作的地图。

You could create an Operation base class and then derive it twice to make Insert and Erase classes. You could then store pointers to the Operation class.

class Settings
{
public:
    bool Insert(std::string const& key, std::string const& value)
    {
        m_operations.push_back(new Insert(key, value));
        return true; //?      
    }

    bool Erase(std::string const& key)
    {
        m_operations.push_back(new Erase(key));
        return true; //?
    }

    bool Commit()
    {
        // You know what to do by now right ?
    }

private:
    class Operation
    {
    public:
        virtual void Execute() = 0
    }

    class Insert : public Operation
    {
    public:
        Insert(std::string const& key, std::string const& value) : 
            m_key(key), m_value(value) {}

        void Execute() {...}
    private:
        std::string m_key;
        std::string m_value;
    }

    class Erase : public Operation
    {
    public:
        Erase(std::string const& key) : m_key(key) {}

        void Execute() {...}
    private:
        std::string m_key;
    }

    std::queue<Operation*> m_operations;
    std::map<std::string, std::string> m_settings;
}

Then you either make your operation classes friend of the settings class or you pass a ref to the map the operation should by applied on.

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