Skip to content

Commit 1778bb2

Browse files
committed
Use rvalue references and std::move to avoid copying Handler objects
1 parent a68b04d commit 1778bb2

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/arduino-timer-cpp17.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ class TimerSet
178178
}
179179

180180
TimerHandle
181-
add_timer(Timepoint start, Timepoint expires, Handler h, Timepoint repeat = 0) noexcept
181+
add_timer(Timepoint start, Timepoint expires, Handler&& h, Timepoint repeat = 0) noexcept
182182
{
183183
if (auto it = next_timer_slot(); it != timers.end()) {
184-
it->handler = h;
184+
it->handler = std::move(h);
185185
it->start = start;
186186
it->expires = expires;
187187
it->repeat = repeat;
@@ -216,32 +216,32 @@ class TimerSet
216216
public:
217217
// Calls handler in delay units of time
218218
TimerHandle
219-
in(Timepoint delay, Handler h) noexcept
219+
in(Timepoint delay, Handler&& h) noexcept
220220
{
221-
return add_timer(clock::now(), delay, h);
221+
return add_timer(clock::now(), delay, std::move(h));
222222
}
223223

224224
// Calls handler at time
225225
TimerHandle
226-
at(Timepoint when, Handler h) noexcept
226+
at(Timepoint when, Handler&& h) noexcept
227227
{
228228
Timepoint now = clock::now();
229-
return add_timer(now, when - now, h);
229+
return add_timer(now, when - now, std::move(h));
230230
}
231231

232232
// Calls handler every interval units of time
233233
TimerHandle
234-
every(Timepoint interval, Handler h) noexcept
234+
every(Timepoint interval, Handler&& h) noexcept
235235
{
236-
return add_timer(clock::now(), interval, h, interval);
236+
return add_timer(clock::now(), interval, std::move(h), interval);
237237
}
238238

239239
// Calls handler immediately and every interval units of time
240240
TimerHandle
241-
now_and_every(Timepoint interval, Handler h) noexcept
241+
now_and_every(Timepoint interval, Handler&& h) noexcept
242242
{
243243
Timepoint now = clock::now();
244-
return add_timer(now, now, h, interval);
244+
return add_timer(now, now, std::move(h), interval);
245245
}
246246

247247
// Cancels timer

0 commit comments

Comments
 (0)