我正在尝试实现一个向量,该向量表示从基类继承的 TimedCallback 对象列表。它们包含一些基本变量,以及一个作为主要功能的函数指针。该函数应该能够返回任何类型并具有任何参数。我将它们作为 lambdas 传递给函数,到目前为止还没有任何问题。
这是相关代码:
std::vector<std::unique_ptr<TimedCallbackBase>> m_TimerCallbackList;
struct TimedCallbackBase {
TimedCallbackBase() = default;
virtual ~TimedCallbackBase() = default;
template<typename T> T run()
{
return dynamic_cast< TimedCallback<T> & >(*this).Run();
}
template<typename T> std::string name()
{
return dynamic_cast< TimedCallback<T> & >(*this).Name;
}
template<typename T> TaskTimer time()
{
return dynamic_cast< TimedCallback<T> & >(*this).Time;
}
template<typename T> int repeatcount()
{
return dynamic_cast< TimedCallback<T> & >(*this).RepeatCount;
}
};
template <typename Fu>
struct TimedCallback : TimedCallbackBase {
TimedCallback(const std::string& name, const std::string& time, Fu f, int r) : Name(name), Run(f), Time(time), RepeatCount(r) {}
std::string Name;
Fu Run;
TaskTimer Time;
int RepeatCount;
};
template<typename Fu>
void Schedule(const std::string& name, const std::string& time, Fu f, int repeatCount = 0) {
TimedCallback cb(name, time, f, repeatCount);
if (!vec_contains(m_TimerCallbackList, cb)) {
m_TimerCallbackList.push_back(cb);
}
else { Global->Log->Warning(title(), "Callback '"+name+"' already exists."); }
}
我的问题是这种方法。我无法正确运行 func 指针。
void _RunTimers() {
if (m_TimerCallbackList.size() > 0) {
for (auto &t : m_TimerCallbackList) {
if (t != nullptr) {
std::string _name = t.get()->name(); // wrong syntax, or signature?
TaskTimer _time = t.get()->time();
int _repeatcount = t.get()->repeatcount();
//auto _run = t.get()->run(); ??
Global->t("Callback name: " + _name);
Global->t("Callback time: " + _time.GetRealTimeAsString());
Global->t("Callback count: " + its(_repeatcount));
}
}
}
else { Global->Log->Warning(title(), "No timed tasks to run at this time."); }
}
我打算使用这样的代码:
Task->Schedule("testTimer", "10sec", [&]{ return Task->Test("I'm a 10sec timer."); });
_RunTimers();
我觉得我离正确地做到这一点还很远。我不想为 _RunTimers(); 指定任何模板;方法。请帮助我了解这怎么可能。
编辑:
我的意思是,我想完全可以按照以下方式定义一堆 typedef
using int_func = std::function<int()>;
对于每种可能的情况,然后重载我的包装器对象,但我一直在寻找更动态和防更改的东西。
编辑 2:实施建议的更改后
注意:为了模棱两可,我重命名了这些方法。(此处不包括方法 Test(),而只是对字符串参数执行 std::cout )
主要的:
Callback myCallback = make_callback(&TaskAssigner::Test, "I'm a 5sec timer.");
Task->ScheduleJob("timer1", "5sec", myCallback, -1);
Task->RunScheduledJobs();
实用程序:
typedef double RetVal;
typedef std::function<RetVal()> Callback;
template <class F, class... Args>
Callback make_callback(F&& f, Args&&... args)
{
auto callable = std::bind(f, args...); // Here we handle the parameters
return [callable]() -> RetVal{ return callable(); }; // Here we handle the return type
}
struct TimedCallback {
TimedCallback(cstR name, cstR time, Callback f, int r)
: Name(name), Run(f), Time(time), RepeatCount(r) {}
RetVal operator()() const { return Run(); }
bool operator==(const TimedCallback& other) {
if (Name == other.Name && RepeatCount == other.RepeatCount) { return true; }
return false;
}
const bool operator==(const TimedCallback& other) const {
if (Name == other.Name && RepeatCount == other.RepeatCount) { return true; }
return false;
}
std::string Name;
Callback Run;
TaskTimer Time;
int RepeatCount;
};
任务分配器.h:
void ScheduleJob(const std::string& name, const std::string& time, const Callback& func, int repeatCount = 0);
void RunScheduledJobs();
std::vector<TimedCallback> m_TimerCallbackList;
任务分配器.cpp:
void TaskAssigner::ScheduleJob(const std::string& name, const std::string& time, const Callback& func, int repeatCount) {
TimedCallback cb(name, time, func, repeatCount);
if (!vec_contains(m_TimerCallbackList, cb)) {
m_TimerCallbackList.emplace_back(cb);
}
else { Global->Log->Warning(title(), "Callback '" + name + "' already added."); }
}
void TaskAssigner::RunScheduledJobs() {
if (m_TimerCallbackList.size() > 0) {
for (auto &t : m_TimerCallbackList)
{
RetVal value = t();
//Global->t("Callback result: " + std::to_string(value));
Global->t("Callback name: " + t.Name);
Global->t("Callback time: " + t.Time.GetRealTimeAsString());
Global->t("Callback count: " + its(t.RepeatCount));
}
}
else { Global->Log->Warning(title(), "No timed tasks to run at this time."); }
}
当前问题:
编译器说: C3848: 类型为 'const std::_Bind , const char(&)[18]>' 的表达式会丢失一些 const-volatile 限定符以便调用.....
我尝试进行研究,有些人提到了 VS 2013 关于绑定和自动的错误。解决方案包括键入签名而不是自动签名,或者删除/添加适当的 const(?)。不确定这是否是同一个错误,或者我的实现是否仍然不正确。(错误:https ://stackoverflow.com/a/30344737/8263197 )
我不确定如何使 RetVal 支持此 typedef 的任何值。我试过了
template<typename T>
struct CallbackReturnValue {
CallbackReturnValue(T v) : value(v) {}
T value;
};
但是我仍然需要模板化其他支持方法。我在这里误会了什么?