Use Perfect forwading to pass arguments from `attach()` to `attach_us()`

This prevents a possible heavy callback copy.
pull/11236/head
Hugues Kamba 2019-08-16 12:28:47 +01:00
parent 5635e94af5
commit e06c91c7ae
1 changed files with 7 additions and 3 deletions

View File

@ -74,12 +74,16 @@ public:
Ticker(const ticker_data_t *data); Ticker(const ticker_data_t *data);
/** Attach a function to be called by the Ticker, specifying the interval in seconds /** Attach a function to be called by the Ticker, specifying the interval in seconds
* The method must be inlined to convert to not use floating-point operations *
* given attach_us() expects an integer value for the callback interval. * The method forwards its arguments to attach_us() rather than copying them which
* may not be trivial depending on the callback copied.
* The function is forcibly inlined to not use floating-point operations. This is
* possible given attach_us() expects an integer value for the callback interval.
* @param func pointer to the function to be called * @param func pointer to the function to be called
* @param t the time between calls in seconds * @param t the time between calls in seconds
*/ */
MBED_FORCEINLINE void attach(Callback<void()> func, float t) template <typename F, typename T>
MBED_FORCEINLINE void attach(F&& func, T&& t)
{ {
attach_us(func, t * 1000000.0f); attach_us(func, t * 1000000.0f);
} }