I've written a library for Mongoose-OS C mgos_timers
. I wanted to write a wrapper to take advantage of the full potential of C++ lambdas. The original C timers allow to pass a callback function pointer.
/* Timer callback */
typedef void (*timer_callback)(void *param);
mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb,void *cb_arg);
The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type's function call operator.
Therefore I wanted to use an std::function
.
This is the C++ wrapper:
Header:
#pragma once
#include <mgos_timers.h>
#include <functional>
namespace mgos_utils {
class interval {
using interval_function_t = std::function<void(void)>;
public:
interval() = default;
interval& operator=(interval&& other);
interval(int millis, interval_function_t f);
void start();
void stop();
~interval();
private:
bool running = false;
mgos_timer_id id;
int repeat_millis;
interval_function_t function;
};
}
The implementation:
#include <mgos.h>
#include <mgos_utils_interval.h>
#include <functional>
#include <memory>
#include <mgos_timers.h>
#define MGOS_TIMER_DO_ONCE false
namespace mgos_utils {
using interval_function_t = std::function<void(void)>;
interval::interval(int millis, interval_function_t f) :
repeat_millis(millis), function(f)
{
start();
}
void interval::start() {
if (!running) {
running = true;
id = mgos_set_timer(repeat_millis, MGOS_TIMER_DO_ONCE, [](void* this_interval) {
auto interval = reinterpret_cast<mgos_utils::interval*>(this_interval);
if (interval->running) interval->function();
// Check again as the called function might stop the interval
if (interval->running) interval->start();
}, this);
} else {
stop();
start();
}
}
void interval::stop() {
if (running) {
running = false;
mgos_clear_timer(id);
}
}
interval& interval::operator=(interval&& other) {
other.stop();
function = other.function;
repeat_millis = other.repeat_millis;
start();
return *this;
}
interval::~interval() {
stop();
}
}
This is an example of its usage:
#include <mgos.h>
#include <mgos_utils_interval.h>
#include <memory>
class interval_test {
public:
interval_test() {
loop = mgos_utils::interval(500, [this](){
LOG(LL_INFO, ("Test interval v1 count %i", interval_count++));
if (interval_count > 5) {
loop = mgos_utils::interval(1000, [this]() {
LOG(LL_INFO, ("Test interval v2 count %i", interval_count++));
if (interval_count > 8) {
LOG(LL_INFO, ("Stop test interval"));
loop.stop();
}
});
}
});
}
private:
mgos_utils::interval loop;
int interval_count = 0;
};
std::unique_ptr<interval_test> test;
extern "C" enum mgos_app_init_result mgos_app_init(void) {
test = std::unique_ptr<interval_test>(new interval_test());
return MGOS_APP_INIT_SUCCESS;
}
I've implemented the move assignment operator to be able to initialize the timers at any point as in the example. Any ideas for a better and more efficient user interface?