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.

111 lines
2.5KB

  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/platforms/asio/AsioWrapper.hpp>
  21. #include <ableton/test/serial_io/SchedulerTree.hpp>
  22. #include <ableton/test/serial_io/Timer.hpp>
  23. #include <ableton/util/Log.hpp>
  24. #include <chrono>
  25. #include <memory>
  26. namespace ableton
  27. {
  28. namespace test
  29. {
  30. namespace serial_io
  31. {
  32. class Context
  33. {
  34. public:
  35. Context(const SchedulerTree::TimePoint& now,
  36. const std::vector<::asio::ip::address>& ifAddrs,
  37. std::shared_ptr<SchedulerTree> pScheduler)
  38. : mNow(now)
  39. , mIfAddrs(ifAddrs)
  40. , mpScheduler(std::move(pScheduler))
  41. , mNextTimerId(0)
  42. {
  43. }
  44. ~Context()
  45. {
  46. if (mpScheduler != nullptr)
  47. {
  48. // Finish any pending tasks before shutting down
  49. mpScheduler->run();
  50. }
  51. }
  52. Context(const Context&) = delete;
  53. Context& operator=(const Context&) = delete;
  54. Context(Context&& rhs)
  55. : mNow(rhs.mNow)
  56. , mIfAddrs(rhs.mIfAddrs)
  57. , mpScheduler(std::move(rhs.mpScheduler))
  58. , mLog(std::move(rhs.mLog))
  59. , mNextTimerId(rhs.mNextTimerId)
  60. {
  61. }
  62. template <typename Handler>
  63. void async(Handler handler)
  64. {
  65. mpScheduler->async(std::move(handler));
  66. }
  67. Context clone()
  68. {
  69. return {mNow, mIfAddrs, mpScheduler->makeChild()};
  70. }
  71. using Timer = serial_io::Timer;
  72. Timer makeTimer()
  73. {
  74. return {mNextTimerId++, mNow, mpScheduler};
  75. }
  76. using Log = util::NullLog;
  77. Log& log()
  78. {
  79. return mLog;
  80. }
  81. std::vector<::asio::ip::address> scanNetworkInterfaces()
  82. {
  83. return mIfAddrs;
  84. }
  85. private:
  86. const SchedulerTree::TimePoint& mNow;
  87. const std::vector<::asio::ip::address>& mIfAddrs;
  88. std::shared_ptr<SchedulerTree> mpScheduler;
  89. Log mLog;
  90. SchedulerTree::TimerId mNextTimerId;
  91. };
  92. } // namespace serial_io
  93. } // namespace test
  94. } // namespace ableton