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 20 #pragma once 21 22 #include <ableton/util/test/Timer.hpp> 23 24 namespace ableton 25 { 26 namespace util 27 { 28 namespace test 29 { 30 31 struct IoService 32 { 33 // Wrapper around the internal util::test::Timer in the list 34 struct Timer 35 { 36 using ErrorCode = test::Timer::ErrorCode; 37 using TimePoint = test::Timer::TimePoint; 38 Timerableton::util::test::IoService::Timer39 Timer(util::test::Timer* pTimer) 40 : mpTimer(pTimer) 41 { 42 } 43 expires_atableton::util::test::IoService::Timer44 void expires_at(std::chrono::system_clock::time_point t) 45 { 46 mpTimer->expires_at(t); 47 } 48 49 template <typename T, typename Rep> expires_from_nowableton::util::test::IoService::Timer50 void expires_from_now(std::chrono::duration<T, Rep> duration) 51 { 52 mpTimer->expires_from_now(duration); 53 } 54 cancelableton::util::test::IoService::Timer55 ErrorCode cancel() 56 { 57 return mpTimer->cancel(); 58 } 59 60 template <typename Handler> async_waitableton::util::test::IoService::Timer61 void async_wait(Handler handler) 62 { 63 mpTimer->async_wait(std::move(handler)); 64 } 65 nowableton::util::test::IoService::Timer66 TimePoint now() const 67 { 68 return mpTimer->now(); 69 } 70 71 util::test::Timer* mpTimer; 72 }; 73 74 IoService() = default; 75 makeTimerableton::util::test::IoService76 Timer makeTimer() 77 { 78 mTimers.emplace_back(); 79 return Timer{&mTimers.back()}; 80 } 81 82 template <typename Handler> postableton::util::test::IoService83 void post(Handler handler) 84 { 85 mHandlers.emplace_back(std::move(handler)); 86 } 87 88 template <typename T, typename Rep> advanceableton::util::test::IoService89 void advance(std::chrono::duration<T, Rep> duration) 90 { 91 runHandlers(); 92 93 for (auto& timer : mTimers) 94 { 95 timer.advance(duration); 96 } 97 } 98 runHandlersableton::util::test::IoService99 void runHandlers() 100 { 101 for (auto& handler : mHandlers) 102 { 103 handler(); 104 } 105 mHandlers.clear(); 106 } 107 108 std::vector<std::function<void()>> mHandlers; 109 std::vector<util::test::Timer> mTimers; 110 }; 111 112 } // namespace test 113 } // namespace util 114 } // namespace ableton 115