Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Timer.hpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/test/serial_io/SchedulerTree.hpp>
  21. namespace ableton
  22. {
  23. namespace test
  24. {
  25. namespace serial_io
  26. {
  27. struct Timer
  28. {
  29. using ErrorCode = SchedulerTree::TimerErrorCode;
  30. using TimePoint = SchedulerTree::TimePoint;
  31. Timer(const SchedulerTree::TimerId timerId,
  32. const TimePoint& now,
  33. std::shared_ptr<SchedulerTree> pScheduler)
  34. : mId(timerId)
  35. , mNow(now)
  36. , mpScheduler(std::move(pScheduler))
  37. {
  38. }
  39. ~Timer()
  40. {
  41. if (!mbMovedFrom)
  42. {
  43. cancel();
  44. }
  45. }
  46. Timer(const Timer&) = delete;
  47. Timer(Timer&& rhs)
  48. : mId(rhs.mId)
  49. , mNow(rhs.mNow)
  50. , mExpiration(std::move(rhs.mExpiration))
  51. , mpScheduler(std::move(rhs.mpScheduler))
  52. {
  53. rhs.mbMovedFrom = true;
  54. }
  55. void expires_at(const TimePoint t)
  56. {
  57. if (t < mNow)
  58. {
  59. throw std::runtime_error("Setting timer in the past");
  60. }
  61. else
  62. {
  63. cancel();
  64. mExpiration = t;
  65. }
  66. }
  67. template <typename T, typename Rep>
  68. void expires_from_now(std::chrono::duration<T, Rep> duration)
  69. {
  70. expires_at(mNow + duration);
  71. }
  72. void cancel()
  73. {
  74. auto pScheduler = mpScheduler.lock();
  75. pScheduler->cancelTimer(mId);
  76. }
  77. template <typename Handler>
  78. void async_wait(Handler handler)
  79. {
  80. auto pScheduler = mpScheduler.lock();
  81. pScheduler->setTimer(mId, mExpiration, std::move(handler));
  82. }
  83. TimePoint now() const
  84. {
  85. return mNow;
  86. }
  87. const SchedulerTree::TimerId mId;
  88. const TimePoint& mNow;
  89. TimePoint mExpiration;
  90. std::weak_ptr<SchedulerTree> mpScheduler;
  91. bool mbMovedFrom = false;
  92. };
  93. } // namespace serial_io
  94. } // namespace test
  95. } // namespace ableton