@chehrlic said in Write Scheduler to run different pointer to member:
I still don't see why a QTimer isn't enough - it can be started, stopped, can be run once or more, can execute any function given with any kind of parameters, even a lambda, ... there is nothing your description requires you to store the PMF by your own. But maybe QMetaMethod is something you can take a look on
Well, like you said, QTimer alone could be enough at first sight, but if I want to "manage" them, or even organize them, I need a wrapper with some human-readable data. That's when the Task class comes handy.
This is my current Task class, as of today:
class Task : public QObject
{
Q_OBJECT
public:
typedef std::function<bool(QString&)> Function;
private:
QString m_name;
Function m_function;
QDateTime m_firstExec;
int m_frequency;
const Task *m_depends;
QDateTime m_lastExec;
bool m_lastStatus;
QString m_failedReason;
QMetaObject::Connection m_dependCnt;
public:
// Constructors/Getters/Setters
signals:
void starting();
void finished(bool status);
public slots:
void schedule();
void exec();
};
I'm stuck with a single type of Function stored (bool (QString&)), with the m_depends property I can wait for another Task to emit finished to effectively call schedule() (when I call schedule(), if there is a dependency, the task waits for the dependency's finished() to be emitted, then it calls schedule() on itself. When schedule() doesn't have to wait for anything, it calls QTimer::singleShot() with exec() as the slot, which itself calls the internal m_function stored.
So I'm using a QTimer in the end, but with some extra properties. Except being stuck with a single type of function (I'm storing lambdas for now), I'm pretty happy with that, moreover if I need to build a widget showing the status of each of them.
I will look at QMetaMethod also, thanks for your suggestions