Date: Fri, 10 Jan 2020 23:28:04 +0000 (UTC)
Hello,
I hope you're well and you enjoy 2020. I'm writing you a short message about a C++ proposal. Of course, the C++ is my favorite programming language and I have an idea about a new std class or struct that doesn't yet exist. I think it could be cool to have a std::timer similar to the win32 timer (https://docs.microsoft.com/en-us/windows/win32/winmsg/using-timers) or the qt timer (https://doc.qt.io/qt-5/qtimer.html) or the GLUT timer (https://linux.die.net/man/3/gluttimerfunc) or ...
The implementation can be discuted of course. It can use std::chrono, std::thread, std::function and more.
This is a draft.
namespace std
{
class timer
{
public:
timer(); // constructor
// ANOTHER VERSION OF THE CONSTRUCTORS : THE ADVANCED VERSION with which thread to use for the call of the callback
timer(); // will call the callback on the main thread (or the thread where the timer has been constructed)
timer(thread_id); // will use this thread_id for the call of the callback
virtual ~timer(); // stop and destroy
void start_once(function, when = 0); // will call the callback named function after a fixed duration
void start_interval(function, interval_duration, nbTimes, when = 0); // will call the callback nb times every 'interval_duration' after a fixed duration
void restart(); // stop the timer and call start_xxx with the last used params
void stop(); // stop the timer but the restart can do something
void cancel(); // stop the timer but the restart does nothing
bool is_active() const; // start_xxx has_been used and the timer is still active
bool can_restart() const; // restart can be used
private:
// ...
};
}
I added 1 example :
Example #1
Sometime in UI developement (or in network), you need a timer. At the end of the timer (like a timeout), a callback will be called. This version helps to write less code and with an easy manner.
I put a small example : You want to know when the user stopped to move the mouse.
The pseudo c++ code is here :
class MyMainWindow : public Framework::Window
{
public:
MyMainWindow()
{
this->SetMouseMove(MyMainWindow::OnMouseMove); // set the event handler
// set the timer to 3 seconds
timer.start_once(MyMainWindow::DoTheAction, 3s);
timer.stop(); // stop it now
}
static void OnMouseMove(...)
{
// reset the timer
timer.restart(); // will call timer.stop() and timer.start_once() with the last used params
}
static void DoTheAction()
{
std::cout << "the user stopped to move the mouse since 3 secondes" << std::endl;
// ... do the other stuffs ...
}
private:
std::timer timer;
};
Remark 1 : There are also another "Timer" that gives the elapsed time between a start/stop (used for benchmark) like the Boost Timer (https://www.boost.org/doc/libs/1_53_0/libs/timer/doc/index.html). Herb sent me this link. Thanks. We need to take in consideration the 'good naming' of course.
Remark 2 : I don't yet talk about the implementation and the accuracy. I'm waiting that this proposal is accepted.
What do you think ?
BR,
Mustapha
I hope you're well and you enjoy 2020. I'm writing you a short message about a C++ proposal. Of course, the C++ is my favorite programming language and I have an idea about a new std class or struct that doesn't yet exist. I think it could be cool to have a std::timer similar to the win32 timer (https://docs.microsoft.com/en-us/windows/win32/winmsg/using-timers) or the qt timer (https://doc.qt.io/qt-5/qtimer.html) or the GLUT timer (https://linux.die.net/man/3/gluttimerfunc) or ...
The implementation can be discuted of course. It can use std::chrono, std::thread, std::function and more.
This is a draft.
namespace std
{
class timer
{
public:
timer(); // constructor
// ANOTHER VERSION OF THE CONSTRUCTORS : THE ADVANCED VERSION with which thread to use for the call of the callback
timer(); // will call the callback on the main thread (or the thread where the timer has been constructed)
timer(thread_id); // will use this thread_id for the call of the callback
virtual ~timer(); // stop and destroy
void start_once(function, when = 0); // will call the callback named function after a fixed duration
void start_interval(function, interval_duration, nbTimes, when = 0); // will call the callback nb times every 'interval_duration' after a fixed duration
void restart(); // stop the timer and call start_xxx with the last used params
void stop(); // stop the timer but the restart can do something
void cancel(); // stop the timer but the restart does nothing
bool is_active() const; // start_xxx has_been used and the timer is still active
bool can_restart() const; // restart can be used
private:
// ...
};
}
I added 1 example :
Example #1
Sometime in UI developement (or in network), you need a timer. At the end of the timer (like a timeout), a callback will be called. This version helps to write less code and with an easy manner.
I put a small example : You want to know when the user stopped to move the mouse.
The pseudo c++ code is here :
class MyMainWindow : public Framework::Window
{
public:
MyMainWindow()
{
this->SetMouseMove(MyMainWindow::OnMouseMove); // set the event handler
// set the timer to 3 seconds
timer.start_once(MyMainWindow::DoTheAction, 3s);
timer.stop(); // stop it now
}
static void OnMouseMove(...)
{
// reset the timer
timer.restart(); // will call timer.stop() and timer.start_once() with the last used params
}
static void DoTheAction()
{
std::cout << "the user stopped to move the mouse since 3 secondes" << std::endl;
// ... do the other stuffs ...
}
private:
std::timer timer;
};
Remark 1 : There are also another "Timer" that gives the elapsed time between a start/stop (used for benchmark) like the Boost Timer (https://www.boost.org/doc/libs/1_53_0/libs/timer/doc/index.html). Herb sent me this link. Thanks. We need to take in consideration the 'good naming' of course.
Remark 2 : I don't yet talk about the implementation and the accuracy. I'm waiting that this proposal is accepted.
What do you think ?
BR,
Mustapha
Received on 2020-01-10 17:30:40