Skip to content

Commit 7df6160

Browse files
committed
Use Perfect forwading to pass arguments from attach() to attach_us()
This prevents a possible heavy callback copy.
1 parent a6cf302 commit 7df6160

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

drivers/Ticker.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
7474
Ticker(const ticker_data_t *data);
7575

7676
/** Attach a function to be called by the Ticker, specifying the interval in seconds
77-
* The method must be inlined to convert to not use floating-point operations
78-
* given attach_us() expects an integer value for the callback interval.
77+
*
78+
* The method forwards its arguments to attach_us() rather than copying them which
79+
* may not be trivial depending on the callback copied.
80+
* The function is forcibly inlined to not use floating-point operations. This is
81+
* possible given attach_us() expects an integer value for the callback interval.
7982
* @param func pointer to the function to be called
8083
* @param t the time between calls in seconds
8184
*/
82-
MBED_FORCEINLINE void attach(Callback<void()> func, float t)
85+
template <typename F, typename T>
86+
MBED_FORCEINLINE void attach(F&& func, T&& t)
8387
{
8488
attach_us(func, t * 1000000.0f);
8589
}

0 commit comments

Comments
 (0)