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