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.

115 lines
2.4KB

  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/util/test/Timer.hpp>
  21. namespace ableton
  22. {
  23. namespace util
  24. {
  25. namespace test
  26. {
  27. struct IoService
  28. {
  29. // Wrapper around the internal util::test::Timer in the list
  30. struct Timer
  31. {
  32. using ErrorCode = test::Timer::ErrorCode;
  33. using TimePoint = test::Timer::TimePoint;
  34. Timer(util::test::Timer* pTimer)
  35. : mpTimer(pTimer)
  36. {
  37. }
  38. void expires_at(std::chrono::system_clock::time_point t)
  39. {
  40. mpTimer->expires_at(t);
  41. }
  42. template <typename T, typename Rep>
  43. void expires_from_now(std::chrono::duration<T, Rep> duration)
  44. {
  45. mpTimer->expires_from_now(duration);
  46. }
  47. ErrorCode cancel()
  48. {
  49. return mpTimer->cancel();
  50. }
  51. template <typename Handler>
  52. void async_wait(Handler handler)
  53. {
  54. mpTimer->async_wait(std::move(handler));
  55. }
  56. TimePoint now() const
  57. {
  58. return mpTimer->now();
  59. }
  60. util::test::Timer* mpTimer;
  61. };
  62. IoService() = default;
  63. Timer makeTimer()
  64. {
  65. mTimers.emplace_back();
  66. return Timer{&mTimers.back()};
  67. }
  68. template <typename Handler>
  69. void post(Handler handler)
  70. {
  71. mHandlers.emplace_back(std::move(handler));
  72. }
  73. template <typename T, typename Rep>
  74. void advance(std::chrono::duration<T, Rep> duration)
  75. {
  76. runHandlers();
  77. for (auto& timer : mTimers)
  78. {
  79. timer.advance(duration);
  80. }
  81. }
  82. void runHandlers()
  83. {
  84. for (auto& handler : mHandlers)
  85. {
  86. handler();
  87. }
  88. mHandlers.clear();
  89. }
  90. std::vector<std::function<void()>> mHandlers;
  91. std::vector<util::test::Timer> mTimers;
  92. };
  93. } // namespace test
  94. } // namespace util
  95. } // namespace ableton