I'm trying to implement a vector that represents a list of TimedCallback objects who inherits from a base class. They hold some basic variables, plus a function pointer which is the main feature. The function should be able to return any type and have any params. I pass them as lambdas into the functions, not really any problems so far.
This is the relevant code:
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."); }
}
My problem is this method. I am unable to properly run the func pointers.
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."); }
}
I intend to use the code like this:
Task->Schedule("testTimer", "10sec", [&]{ return Task->Test("I'm a 10sec timer."); });
_RunTimers();
I feel like I am pretty far from doing this correctly. I don't want to specify any templates for the _RunTimers(); method. Please help me understand how this can possible.
Edit:
I mean, I guess it is totally possible to just define a bunch of typedefs along the lines of
using int_func = std::function<int()>;
for every possible case and then overload my wrapper object, but I was looking for something more dynamic and change-proof.
Edit 2: After implementing the suggested changes
Note: I've renamed the methods for ambiguity's sake. (The method Test() is not included here, but simply does a std::cout of a string param)
main:
Callback myCallback = make_callback(&TaskAssigner::Test, "I'm a 5sec timer.");
Task->ScheduleJob("timer1", "5sec", myCallback, -1);
Task->RunScheduledJobs();
Utilities:
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;
};
TaskAssigner .h:
void ScheduleJob(const std::string& name, const std::string& time, const Callback& func, int repeatCount = 0);
void RunScheduledJobs();
std::vector<TimedCallback> m_TimerCallbackList;
TaskAssigner.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."); }
}
Current problem:
Compiler says: C3848: expression having type 'const std::_Bind, const char(&)[18]>' would lose some const-volatile qualifiers in order to call .....
I tried researching and some have mentioned a bug with VS 2013 regarding bind and auto. Solutions include typing the signature rather than auto-ing it, or remove/add proper const(?). Not sure if this is the same bug or if my implementation is still incorrect. (Bug: https://stackoverflow.com/a/30344737/8263197)
I am unsure how exactly I can make RetVal support any value with this typedef. I tried
template<typename T>
struct CallbackReturnValue {
CallbackReturnValue(T v) : value(v) {}
T value;
};
but then I will still need to template the other supporting methods. What am I mistaking here?