c++ - 将自定义函数传递到基本抽象类以延迟执行

标签 c++ design-patterns c++17 std std-function

我想学习如何对 Task 类进行抽象,该类可以采用任何函数或仿函数对象(及其参数等)并将其存储以供以后执行或将其分布在某些线程上,等等。

我用 std::function 做了一些实验。和模板类,但没有成功。所以我想先编译它然后运行以熟悉这些概念,然后我会寻找一些更有效的模式来满足我的需要。所以,问题是我如何才能在第一步首先编译我的代码?代码如下所示。

#include <iostream>
#include <string>
#include <queue>
#include <memory>
#include <functional>

class ITask
{
    public:
    virtual ~ITask() = default;
    virtual void execute() = 0;
};

template<typename ReturnType, typename... Args>
class GameTask : ITask
{
    explicit GameTask(std::function<ReturnType(Args...)>& func) :
        func_(func)
    {}

    void execute()
    {
        // func(Args...); ??
    }

private:
    std::function<ReturnType(Args...)> func_;
};


// lets imitate some bigger classes with various methods
class BigClassA
{
public:
    void func1(int a) { std::cout << ++a; }
    int func2(const std::string& s) { std::cout << s; return b; }

    int b = 4;
};

class BigClassB
{
public:
    double func1(BigClassA& bca, int i) { bca.b += i; return 0.1; }
};

int main()
{
    BigClassA a;
    BigClassB b;

    // perform immidiately by current main thread:
    a.func1(2);
    b.func1(a, 3);
    a.func2("Hello");


    //store under queue for later execution
    std::queue<std::unique_ptr<ITask>> queue;

    /*  a.func1(2);  */
    // queue.push(std::make_unique<GameTask>( [&a](){ a.func1(2); } ));

    /*  b.func1(a, 3);  */
    //  queue.push(std::make_unique<GameTask>(  ));

    /*  a.func2("Hello");  */
    // queue.push(std::make_unique<GameTask>(  ));


    while (queue.size())
    {
        queue.front()->execute();
        queue.pop();
    }

}

编辑:

瓦拉迪奇在这里确实是针。这就是我目前最终得到的代码:
#include <iostream>
#include <string>
#include <queue>
#include <memory>
#include <functional>

class ITask
{
public:
    virtual ~ITask() = default;
    virtual void execute() = 0;
};

class GameTask : public ITask
{
public:
    GameTask(std::function<void()> func) : func_(func) {}

    void execute() final
    {
        func_();
    }

private:
    std::function<void()> func_;
};

// lets imitate some bigger classes with various methods
class BigClassA
{
public:
    void func1(int a) const { std::cout << ++a; }
    int func2(const std::string& s) { std::cout << s; return b; }

    int b = 4;
};

class BigClassB
{
public:
    double func1(BigClassA& bca, int i) { bca.b += i; return 0.1; }
};

int main()
{
    BigClassA a;
    BigClassB b;

    // perform immidiately by current main thread:
    a.func1(2);
    b.func1(a, 3);
    a.func2("Hello");

    //store under queue for later execution
    std::queue<std::unique_ptr<ITask>> queue;

    queue.push(std::make_unique<GameTask>( [&a]() { a.func1(2); } ));

    queue.push(std::make_unique<GameTask>( [&a, &b]() {b.func1(a, 3); } ));

    queue.push(std::make_unique<GameTask>( [&a]() { a.func2("Hello"); } ));


    // delayed execution
    while (queue.size())
    {
        queue.front()->execute();
        queue.pop();
    }

}

我想听听我可以添加的每一项改进。

最佳答案

这是代码的工作版本,有一些更改。虽然设计可以改进很多,但是,我只是改变了你的代码来编译和工作。

#include <iostream>
#include <string>
#include <queue>
#include <memory>
#include <functional>
#include <queue>

template<typename ReturnType, typename... Args>
class ITask
{
public:
   virtual ~ITask() = default;
   virtual void execute(Args ...) = 0;
};

template<typename ReturnType, typename... Args>
class GameTask : public ITask<ReturnType, Args...>
{
public:
   GameTask(std::function<ReturnType(Args...)>& func) :
      func_(func)
   {
   }

   void execute(Args ... args) override
   {
      func_(args...); 
   }

private:
   std::function<ReturnType(Args...)> func_;
};


// lets imitate some bigger classes with various methods
class BigClassA
{
public:
   void func1(int a) { std::cout << ++a; }
   int func2(const std::string& s) { std::cout << s; return b; }

   int b = 4;
};

class BigClassB
{
public:
   double func1(BigClassA& bca, int i) { bca.b += i; return 0.1; }
};

int main()
{
   BigClassA a;
   BigClassB b;

   // perform immediately by current main thread:
   a.func1(2);
   b.func1(a, 3);
   a.func2("Hello");


   //store under queue for later execution
   std::queue<std::unique_ptr<ITask<void , int>>> queue;

   a.func1(2);
    queue.push(std::make_unique<GameTask<void, int>>(std::function<void(int)>([&a](int x) { a.func1(2); })));

   b.func1(a, 3);
   queue.push(std::make_unique<GameTask<void, int>>(std::function<void(int)>([&b , &a](int x) { b.func1(a , 122); })));

   a.func2("Hello");  
   queue.push(std::make_unique<GameTask<void, int>>(std::function<void(int)>([&a](int x) { a.func2("Hi"); })));


   while (queue.size())
   {
      queue.front()->execute(3);
      queue.pop();
   }
}

编辑 1:更新了std::queue使用多态类型的成员。

关于c++ - 将自定义函数传递到基本抽象类以延迟执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60169505/

相关文章:

c++ - 如何通过可变参数模板将多个构造函数参数转发到数组初始值设定项列表?

c++ - 如何创建一个类似于 `range` 的可迭代浮点对象?

c++ - 在 OpenGL 中绑定(bind)超过 MAX_TEXTURE_UNITS 个纹理

c++ - 为什么这段代码会泄漏内存? (输出参数和指针)

c++ - 将 SharedPtr 与 std::list 一起使用时出现内存泄漏!漏洞?

java - 根据对象的属性将对象归为特定类型

c++ - vector 与字符串

java - 设计模式来验证请求?

php - 在某些 Controller 上运行某些代码的最佳方式 - Laravel 5

c++ - 超出命名空间函数定义的参数类型查找