LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
throttle.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <coroutine>
12 #include <chrono>
13 #include <QElapsedTimer>
14 #include <QTimer>
15 #include <QVector>
16 #include "../threadsconfig.h"
17 
18 namespace LC::Util
19 {
21  {
22  QTimer Timer_;
23  QElapsedTimer LastInvocation_;
24  std::chrono::milliseconds Interval_;
25 
26  QVector<std::coroutine_handle<>> Queue_;
27 
28  int BackoffFactor_ = 0;
29 
30  struct Awaiter
31  {
32  Throttle& Throttle_;
33  std::coroutine_handle<> Handle_ {};
34 
35  explicit Awaiter (Throttle& throttle);
36  Awaiter (Awaiter&& other) noexcept;
37 
38  Awaiter (const Awaiter&) = delete;
39  Awaiter& operator= (const Awaiter&) = delete;
40  Awaiter& operator= (Awaiter&&) = delete;
41 
42  ~Awaiter ();
43 
44  bool await_ready () const;
45  void await_suspend (std::coroutine_handle<>);
46  void await_resume ();
47  };
48  public:
49  explicit Throttle (std::chrono::milliseconds, Qt::TimerType = Qt::TimerType::CoarseTimer);
50 
51  std::chrono::milliseconds GetInterval () const;
52 
53  void Backoff ();
54 
55  Awaiter operator co_await ();
56  private:
57  void StartTimer (std::chrono::milliseconds);
58  };
59 
60  using Throttle_ptr = std::shared_ptr<Throttle>;
61 }
decltype(auto) Awaiter(T &&obj)
Definition: context.h:38
#define UTIL_THREADS_API
Definition: threadsconfig.h:16
std::shared_ptr< Throttle > Throttle_ptr
Definition: throttle.h:60
auto Tup2 &&tup2 noexcept
Definition: ctstringutils.h:68