| 1 |
/* |
| 2 |
* Copyright (c) 2018~now Palo Kisa <[email protected]> |
| 3 |
* |
| 4 |
* This Program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2, or (at your option) |
| 7 |
* any later version. |
| 8 |
* |
| 9 |
* This Program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this addon; see the file COPYING. If not, write to |
| 16 |
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 |
* http://www.gnu.org/copyleft/gpl.html |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#if !defined(CallLimiter_hh) |
| 22 |
#define CallLimiter_hh |
| 23 |
|
| 24 |
#include <chrono> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
/*! |
| 28 |
* \brief Limit calls for the specific Callable object to once |
| 29 |
* per interval. User of this object can call \sa Call() any time, |
| 30 |
* but the underneath next call is excecuted only if the configured |
| 31 |
* inteval is passed. |
| 32 |
*/ |
| 33 |
template <class Callable> |
| 34 |
class CallLimiter |
| 35 |
{ |
| 36 |
public: |
| 37 |
/*! |
| 38 |
* \brief Create the limiter object for \param callable with call |
| 39 |
* interval \param interval. \param delayFirstCall is flag if the |
| 40 |
* the first real call should be postponed. |
| 41 |
*/ |
| 42 |
CallLimiter(Callable callable |
| 43 |
, std::chrono::milliseconds interval |
| 44 |
, bool delayFirstCall) |
| 45 |
: mCallable{std::move(callable)} |
| 46 |
, mInterval{std::move(interval)} |
| 47 |
, mNextCall{std::chrono::high_resolution_clock::now()} |
| 48 |
{ |
| 49 |
if (delayFirstCall) |
| 50 |
mNextCall += interval; |
| 51 |
} |
| 52 |
|
| 53 |
/*! |
| 54 |
* \brief Execute the operator() on our callable object if the period |
| 55 |
* after previous passed. |
| 56 |
* \return flag if underneath call was performed |
| 57 |
* |
| 58 |
* \note User of this object should call this method more often than |
| 59 |
* the period to achieve the desired behavior. |
| 60 |
*/ |
| 61 |
bool Call() |
| 62 |
{ |
| 63 |
auto now = std::chrono::high_resolution_clock::now(); |
| 64 |
if (mNextCall <= now) |
| 65 |
{ |
| 66 |
while (mNextCall < now) |
| 67 |
mNextCall += mInterval; |
| 68 |
static_cast<void>(mCallable()); |
| 69 |
return true; |
| 70 |
} |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
private: |
| 75 |
Callable mCallable; |
| 76 |
const std::chrono::milliseconds mInterval; |
| 77 |
|
| 78 |
std::chrono::time_point<std::chrono::high_resolution_clock> mNextCall; |
| 79 |
}; |
| 80 |
|
| 81 |
/*! |
| 82 |
* \brief Helper to get CallLimiter<T> object. |
| 83 |
*/ |
| 84 |
template <class Callable, class Rep, class Period> |
| 85 |
CallLimiter<Callable> getCallLimiter(Callable callable, std::chrono::duration<Rep, Period> interval, bool delayFirstCall) |
| 86 |
{ |
| 87 |
return CallLimiter<Callable>(std::move(callable), std::move(interval), delayFirstCall); |
| 88 |
} |
| 89 |
|
| 90 |
#endif |