|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2020 ARM Limited |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifndef EVENTQUEUE_FAKE_H |
| 19 | +#define EVENTQUEUE_FAKE_H |
| 20 | + |
| 21 | +#include <map> |
| 22 | +#include <memory> |
| 23 | +#include <functional> |
| 24 | +#include <algorithm> |
| 25 | +#include <vector> |
| 26 | +#include "events/EventQueue.h" |
| 27 | +#include <chrono> |
| 28 | +#include <mstd_tuple> |
| 29 | + |
| 30 | +namespace events { |
| 31 | + |
| 32 | +typedef int handle_t; |
| 33 | +typedef std::function<void()> function_t; |
| 34 | +typedef unsigned tick_t; |
| 35 | + |
| 36 | +class EventQueue { |
| 37 | + using duration = std::chrono::duration<int, std::milli>; |
| 38 | + |
| 39 | +public: |
| 40 | + EventQueue(unsigned size = 0, unsigned char *buffer = NULL) { delete buffer; }; |
| 41 | + |
| 42 | + ~EventQueue() { }; |
| 43 | + |
| 44 | + /** This will advence time by given amount of milliseonds and then dispatch all events that were set to happen in that time. |
| 45 | + * |
| 46 | + * @param ms number of miliseconds to advance time |
| 47 | + */ |
| 48 | + void dispatch(int milliseconds = -1) { |
| 49 | + if (milliseconds > 0) { |
| 50 | + process_events(milliseconds); |
| 51 | + } else { |
| 52 | + _now = (tick_t)-1; |
| 53 | + process_events(); |
| 54 | + _now = 0; |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + tick_t tick() { |
| 59 | + return _now; |
| 60 | + }; |
| 61 | + |
| 62 | + bool cancel(handle_t id) { |
| 63 | + return cancel_handler(id); |
| 64 | + }; |
| 65 | + |
| 66 | + /** Get number of events in queue. |
| 67 | + * |
| 68 | + * @return Number of events waiting in the queue. |
| 69 | + */ |
| 70 | + size_t size() const { |
| 71 | + return _handlers.size(); |
| 72 | + } |
| 73 | + |
| 74 | + template<typename F, typename ... ArgTs> |
| 75 | + handle_t call(F f, ArgTs... args) { |
| 76 | + return call_handler( |
| 77 | + [f, args = mstd::make_tuple(args...)]() { |
| 78 | + mstd::apply(f, args); |
| 79 | + } |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + template<typename F, typename ... ArgTs> |
| 84 | + handle_t call_in(duration ms, F f, ArgTs... args) { |
| 85 | + return call_handler_in( |
| 86 | + ms.count(), |
| 87 | + [f, args = mstd::make_tuple(args...)]() { |
| 88 | + mstd::apply(f, args); |
| 89 | + } |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + template <typename T, typename R, typename... ArgTs> |
| 94 | + int call(T *obj, R(T::*method)(ArgTs...), ArgTs... args) { |
| 95 | + return call_handler( |
| 96 | + [obj, method, args = mstd::make_tuple(args...)]() { |
| 97 | + mstd::apply(method, obj, args); |
| 98 | + } |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + template <typename T, typename R, typename... ArgTs> |
| 103 | + int call_in(duration ms, T *obj, R(T::*method)(ArgTs...), ArgTs... args) { |
| 104 | + return call_handler_in( |
| 105 | + ms.count(), |
| 106 | + [obj, method, args = mstd::make_tuple(args...)]() { |
| 107 | + mstd::apply(method, obj, args); |
| 108 | + } |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | +private: |
| 113 | + handle_t call_handler(function_t handler); |
| 114 | + |
| 115 | + handle_t call_handler_in(tick_t ms, function_t handler); |
| 116 | + |
| 117 | + bool cancel_handler(handle_t handle); |
| 118 | + |
| 119 | + void process_events(tick_t duration_ms); |
| 120 | + |
| 121 | + void process_events(); |
| 122 | + |
| 123 | +private: |
| 124 | + struct internal_event { |
| 125 | + std::unique_ptr<function_t> handler; |
| 126 | + tick_t tick; |
| 127 | + handle_t handle; |
| 128 | + }; |
| 129 | + |
| 130 | + std::vector<internal_event> _handlers; |
| 131 | + tick_t _now = 0; |
| 132 | + handle_t _handler_id = 0; |
| 133 | +}; |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +#endif //EVENTQUEUE_FAKE_H |
0 commit comments