Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
source/EventQueue/EventQueue.h@0:ed0152b5c495, 2016-09-19 (annotated)
- Committer:
- roywant
- Date:
- Mon Sep 19 00:59:11 2016 +0000
- Revision:
- 0:ed0152b5c495
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| roywant | 0:ed0152b5c495 | 1 | /* |
| roywant | 0:ed0152b5c495 | 2 | * Copyright (c) 2016, ARM Limited, All Rights Reserved |
| roywant | 0:ed0152b5c495 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| roywant | 0:ed0152b5c495 | 4 | * |
| roywant | 0:ed0152b5c495 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| roywant | 0:ed0152b5c495 | 6 | * not use this file except in compliance with the License. |
| roywant | 0:ed0152b5c495 | 7 | * You may obtain a copy of the License at |
| roywant | 0:ed0152b5c495 | 8 | * |
| roywant | 0:ed0152b5c495 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| roywant | 0:ed0152b5c495 | 10 | * |
| roywant | 0:ed0152b5c495 | 11 | * Unless required by applicable law or agreed to in writing, software |
| roywant | 0:ed0152b5c495 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| roywant | 0:ed0152b5c495 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| roywant | 0:ed0152b5c495 | 14 | * See the License for the specific language governing permissions and |
| roywant | 0:ed0152b5c495 | 15 | * limitations under the License. |
| roywant | 0:ed0152b5c495 | 16 | */ |
| roywant | 0:ed0152b5c495 | 17 | #ifndef EVENTQUEUE_EVENTQUEUE_H_ |
| roywant | 0:ed0152b5c495 | 18 | #define EVENTQUEUE_EVENTQUEUE_H_ |
| roywant | 0:ed0152b5c495 | 19 | |
| roywant | 0:ed0152b5c495 | 20 | #include <stdio.h> |
| roywant | 0:ed0152b5c495 | 21 | #include "Thunk.h" |
| roywant | 0:ed0152b5c495 | 22 | #include "MakeThunk.h" |
| roywant | 0:ed0152b5c495 | 23 | |
| roywant | 0:ed0152b5c495 | 24 | namespace eq { |
| roywant | 0:ed0152b5c495 | 25 | |
| roywant | 0:ed0152b5c495 | 26 | class EventQueue { |
| roywant | 0:ed0152b5c495 | 27 | |
| roywant | 0:ed0152b5c495 | 28 | public: |
| roywant | 0:ed0152b5c495 | 29 | /// typedef for callable type. |
| roywant | 0:ed0152b5c495 | 30 | /// the callable type used should support the same operations |
| roywant | 0:ed0152b5c495 | 31 | /// supported by a void(*)() function pointer. |
| roywant | 0:ed0152b5c495 | 32 | typedef Thunk function_t; |
| roywant | 0:ed0152b5c495 | 33 | |
| roywant | 0:ed0152b5c495 | 34 | /// handle to a posted event which will be executed later. |
| roywant | 0:ed0152b5c495 | 35 | /// model after a void* pointer. |
| roywant | 0:ed0152b5c495 | 36 | typedef void* event_handle_t; |
| roywant | 0:ed0152b5c495 | 37 | |
| roywant | 0:ed0152b5c495 | 38 | /// type used for time |
| roywant | 0:ed0152b5c495 | 39 | typedef std::size_t ms_time_t; |
| roywant | 0:ed0152b5c495 | 40 | |
| roywant | 0:ed0152b5c495 | 41 | /// Construct an empty event queue |
| roywant | 0:ed0152b5c495 | 42 | EventQueue() { } |
| roywant | 0:ed0152b5c495 | 43 | |
| roywant | 0:ed0152b5c495 | 44 | virtual ~EventQueue() { } |
| roywant | 0:ed0152b5c495 | 45 | |
| roywant | 0:ed0152b5c495 | 46 | /** |
| roywant | 0:ed0152b5c495 | 47 | * Post a callable to the event queue. |
| roywant | 0:ed0152b5c495 | 48 | * It will be executed during the next dispatch cycle. |
| roywant | 0:ed0152b5c495 | 49 | * @param f The callbable to be executed by the event queue. |
| roywant | 0:ed0152b5c495 | 50 | * @return the handle to the event. |
| roywant | 0:ed0152b5c495 | 51 | */ |
| roywant | 0:ed0152b5c495 | 52 | template<typename F> |
| roywant | 0:ed0152b5c495 | 53 | event_handle_t post(const F& fn) { |
| roywant | 0:ed0152b5c495 | 54 | return do_post(fn); |
| roywant | 0:ed0152b5c495 | 55 | } |
| roywant | 0:ed0152b5c495 | 56 | |
| roywant | 0:ed0152b5c495 | 57 | /** |
| roywant | 0:ed0152b5c495 | 58 | * Bind a callable and an argument then post a callable to the event queue. |
| roywant | 0:ed0152b5c495 | 59 | * It will be executed during the next dispatch cycle. |
| roywant | 0:ed0152b5c495 | 60 | * @param f The callbable to be bound with arg0. |
| roywant | 0:ed0152b5c495 | 61 | * @param arg0 The first argument to bind to f. |
| roywant | 0:ed0152b5c495 | 62 | * @return the handle to the event. |
| roywant | 0:ed0152b5c495 | 63 | */ |
| roywant | 0:ed0152b5c495 | 64 | template<typename F, typename Arg0> |
| roywant | 0:ed0152b5c495 | 65 | event_handle_t post(const F& fn, const Arg0& arg0) { |
| roywant | 0:ed0152b5c495 | 66 | return do_post(make_thunk(fn, arg0)); |
| roywant | 0:ed0152b5c495 | 67 | } |
| roywant | 0:ed0152b5c495 | 68 | |
| roywant | 0:ed0152b5c495 | 69 | template<typename F, typename Arg0, typename Arg1> |
| roywant | 0:ed0152b5c495 | 70 | event_handle_t post(const F& fn, const Arg0& arg0, const Arg1& arg1) { |
| roywant | 0:ed0152b5c495 | 71 | return do_post(make_thunk(fn, arg0, arg1)); |
| roywant | 0:ed0152b5c495 | 72 | } |
| roywant | 0:ed0152b5c495 | 73 | |
| roywant | 0:ed0152b5c495 | 74 | template<typename F, typename Arg0, typename Arg1, typename Arg2> |
| roywant | 0:ed0152b5c495 | 75 | event_handle_t post(const F& fn, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2) { |
| roywant | 0:ed0152b5c495 | 76 | return do_post(make_thunk(fn, arg0, arg1, arg2)); |
| roywant | 0:ed0152b5c495 | 77 | } |
| roywant | 0:ed0152b5c495 | 78 | |
| roywant | 0:ed0152b5c495 | 79 | template<typename F> |
| roywant | 0:ed0152b5c495 | 80 | event_handle_t post_in(const F& fn, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 81 | return do_post(fn, ms_delay); |
| roywant | 0:ed0152b5c495 | 82 | } |
| roywant | 0:ed0152b5c495 | 83 | |
| roywant | 0:ed0152b5c495 | 84 | template<typename F, typename Arg0> |
| roywant | 0:ed0152b5c495 | 85 | event_handle_t post_in(const F& fn, const Arg0& arg0, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 86 | return do_post(make_thunk(fn, arg0), ms_delay); |
| roywant | 0:ed0152b5c495 | 87 | } |
| roywant | 0:ed0152b5c495 | 88 | |
| roywant | 0:ed0152b5c495 | 89 | template<typename F, typename Arg0, typename Arg1> |
| roywant | 0:ed0152b5c495 | 90 | event_handle_t post_in(const F& fn, const Arg0& arg0, const Arg1& arg1, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 91 | return do_post(make_thunk(fn, arg0, arg1), ms_delay); |
| roywant | 0:ed0152b5c495 | 92 | } |
| roywant | 0:ed0152b5c495 | 93 | |
| roywant | 0:ed0152b5c495 | 94 | template<typename F, typename Arg0, typename Arg1, typename Arg2> |
| roywant | 0:ed0152b5c495 | 95 | event_handle_t post_in(const F& fn, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 96 | return do_post(make_thunk(fn, arg0, arg1, arg2), ms_delay); |
| roywant | 0:ed0152b5c495 | 97 | } |
| roywant | 0:ed0152b5c495 | 98 | |
| roywant | 0:ed0152b5c495 | 99 | template<typename F> |
| roywant | 0:ed0152b5c495 | 100 | event_handle_t post_every(const F& fn, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 101 | return do_post(fn, ms_delay, true); |
| roywant | 0:ed0152b5c495 | 102 | } |
| roywant | 0:ed0152b5c495 | 103 | |
| roywant | 0:ed0152b5c495 | 104 | template<typename F, typename Arg0> |
| roywant | 0:ed0152b5c495 | 105 | event_handle_t post_every(const F& fn, const Arg0& arg0, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 106 | return do_post(make_thunk(fn, arg0), ms_delay, true); |
| roywant | 0:ed0152b5c495 | 107 | } |
| roywant | 0:ed0152b5c495 | 108 | |
| roywant | 0:ed0152b5c495 | 109 | template<typename F, typename Arg0, typename Arg1> |
| roywant | 0:ed0152b5c495 | 110 | event_handle_t post_every(const F& fn, const Arg0& arg0, const Arg1& arg1, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 111 | return do_post(make_thunk(fn, arg0, arg1), ms_delay, true); |
| roywant | 0:ed0152b5c495 | 112 | } |
| roywant | 0:ed0152b5c495 | 113 | |
| roywant | 0:ed0152b5c495 | 114 | template<typename F, typename Arg0, typename Arg1, typename Arg2> |
| roywant | 0:ed0152b5c495 | 115 | event_handle_t post_every(const F& fn, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2, ms_time_t ms_delay) { |
| roywant | 0:ed0152b5c495 | 116 | return do_post(make_thunk(fn, arg0, arg1, arg2), ms_delay, true); |
| roywant | 0:ed0152b5c495 | 117 | } |
| roywant | 0:ed0152b5c495 | 118 | |
| roywant | 0:ed0152b5c495 | 119 | virtual bool cancel(event_handle_t event_handle) = 0; |
| roywant | 0:ed0152b5c495 | 120 | |
| roywant | 0:ed0152b5c495 | 121 | private: |
| roywant | 0:ed0152b5c495 | 122 | virtual event_handle_t do_post(const function_t& fn, ms_time_t ms_delay = 0, bool repeat = false) = 0; |
| roywant | 0:ed0152b5c495 | 123 | }; |
| roywant | 0:ed0152b5c495 | 124 | |
| roywant | 0:ed0152b5c495 | 125 | } // namespace eq |
| roywant | 0:ed0152b5c495 | 126 | |
| roywant | 0:ed0152b5c495 | 127 | #endif /* EVENTQUEUE_EVENTQUEUE_H_ */ |