我想学习如何对 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/