Roy Want / Mbed OS beaconCompileReadyFork
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EventQueue.h Source File

EventQueue.h

00001 /*
00002  * Copyright (c) 2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef EVENTQUEUE_EVENTQUEUE_H_
00018 #define EVENTQUEUE_EVENTQUEUE_H_
00019 
00020 #include <stdio.h>
00021 #include "Thunk.h"
00022 #include "MakeThunk.h"
00023 
00024 namespace eq {
00025 
00026 class EventQueue {
00027 
00028 public:
00029     /// typedef for callable type.
00030     /// the callable type used should support the same operations
00031     /// supported by a void(*)() function pointer.
00032     typedef Thunk function_t;
00033 
00034     /// handle to a posted event which will be executed later.
00035     /// model after a void* pointer.
00036     typedef void* event_handle_t;
00037 
00038     /// type used for time
00039     typedef std::size_t ms_time_t;
00040 
00041     /// Construct an empty event queue
00042     EventQueue() { }
00043 
00044     virtual ~EventQueue() { }
00045 
00046     /**
00047      * Post a callable to the event queue.
00048      * It will be executed during the next dispatch cycle.
00049      * @param f The callbable to be executed by the event queue.
00050      * @return the handle to the event.
00051      */
00052     template<typename F>
00053     event_handle_t post(const F& fn) {
00054         return do_post(fn);
00055     }
00056 
00057     /**
00058      * Bind a callable and an argument then post a callable to the event queue.
00059      * It will be executed during the next dispatch cycle.
00060      * @param f The callbable to be bound with arg0.
00061      * @param arg0 The first argument to bind to f.
00062      * @return the handle to the event.
00063      */
00064     template<typename F, typename Arg0>
00065     event_handle_t post(const F& fn, const Arg0& arg0) {
00066         return do_post(make_thunk(fn, arg0));
00067     }
00068 
00069     template<typename F, typename Arg0, typename Arg1>
00070     event_handle_t post(const F& fn, const Arg0& arg0, const Arg1& arg1) {
00071         return do_post(make_thunk(fn, arg0, arg1));
00072     }
00073 
00074     template<typename F, typename Arg0, typename Arg1, typename Arg2>
00075     event_handle_t post(const F& fn, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2) {
00076         return do_post(make_thunk(fn, arg0, arg1, arg2));
00077     }
00078 
00079     template<typename F>
00080     event_handle_t post_in(const F& fn, ms_time_t ms_delay) {
00081         return do_post(fn, ms_delay);
00082     }
00083 
00084     template<typename F, typename Arg0>
00085     event_handle_t post_in(const F& fn, const Arg0& arg0, ms_time_t ms_delay) {
00086         return do_post(make_thunk(fn, arg0), ms_delay);
00087     }
00088 
00089     template<typename F, typename Arg0, typename Arg1>
00090     event_handle_t post_in(const F& fn, const Arg0& arg0, const Arg1& arg1, ms_time_t ms_delay) {
00091         return do_post(make_thunk(fn, arg0, arg1), ms_delay);
00092     }
00093 
00094     template<typename F, typename Arg0, typename Arg1, typename Arg2>
00095     event_handle_t post_in(const F& fn, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2, ms_time_t ms_delay) {
00096         return do_post(make_thunk(fn, arg0, arg1, arg2), ms_delay);
00097     }
00098 
00099     template<typename F>
00100     event_handle_t post_every(const F& fn, ms_time_t ms_delay) {
00101         return do_post(fn, ms_delay, true);
00102     }
00103 
00104     template<typename F, typename Arg0>
00105     event_handle_t post_every(const F& fn, const Arg0& arg0, ms_time_t ms_delay) {
00106         return do_post(make_thunk(fn, arg0), ms_delay, true);
00107     }
00108 
00109     template<typename F, typename Arg0, typename Arg1>
00110     event_handle_t post_every(const F& fn, const Arg0& arg0, const Arg1& arg1, ms_time_t ms_delay) {
00111         return do_post(make_thunk(fn, arg0, arg1), ms_delay, true);
00112     }
00113 
00114     template<typename F, typename Arg0, typename Arg1, typename Arg2>
00115     event_handle_t post_every(const F& fn, const Arg0& arg0, const Arg1& arg1, const Arg2& arg2, ms_time_t ms_delay) {
00116         return do_post(make_thunk(fn, arg0, arg1, arg2), ms_delay, true);
00117     }
00118 
00119     virtual bool cancel(event_handle_t event_handle) = 0;
00120 
00121 private:
00122     virtual event_handle_t do_post(const function_t& fn, ms_time_t ms_delay = 0, bool repeat = false) = 0;
00123 };
00124 
00125 } // namespace eq
00126 
00127 #endif /* EVENTQUEUE_EVENTQUEUE_H_ */