/* * Copyright (c) 2018-2019, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MBED_TASK_H #define MBED_TASK_H #include "events/EventQueue.h" #include "drivers/internal/TaskBase.h" #include "platform/mbed_assert.h" #include "platform/Callback.h" namespace events { /** \addtogroup drivers-internal-api-usb * @{ */ template struct AllArgs; template struct AllArgs { typedef AllArgs Self; B0 b0; AllArgs(B0 b0 = B0()): b0(b0) {} template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); s->b0(); s->~Self(); } }; typedef Operations ops; }; template struct AllArgs { typedef AllArgs Self; B0 b0; B1 b1; AllArgs(B0 b0 = B0(), B1 b1 = B1()): b0(b0), b1(b1) {} template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); s->b0(s->b1); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(); s->~Self(); } }; typedef Operations ops; }; template struct AllArgs { typedef AllArgs Self; B0 b0; B1 b1; B2 b2; AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2()): b0(b0), b1(b1), b2(b2) {} template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); s->b0(s->b1, s->b2); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2); s->~Self(); } }; typedef Operations ops; }; template struct AllArgs { typedef AllArgs Self; B0 b0; B1 b1; B2 b2; B3 b3; AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3()): b0(b0), b1(b1), b2(b2), b3(b3) {} template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); s->b0(s->b1, s->b2, s->b3); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3); s->~Self(); } }; typedef Operations ops; }; template struct AllArgs { typedef AllArgs Self; B0 b0; B1 b1; B2 b2; B3 b3; B4 b4; AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3(), B4 b4 = B4()): b0(b0), b1(b1), b2(b2), b3(b3), b4(b4) {} template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); s->b0(s->b1, s->b2, s->b3, s->b4); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4); s->~Self(); } }; typedef Operations ops; }; template struct AllArgs { typedef AllArgs Self; B0 b0; B1 b1; B2 b2; B3 b3; B4 b4; B5 b5; AllArgs(B0 b0 = B0(), B1 b1 = B1(), B2 b2 = B2(), B3 b3 = B3(), B4 b4 = B4(), B5 b5 = B5()): b0(b0), b1(b1), b2(b2), b3(b3), b4(b4), b5(b5) {} template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); s->b0(s->b1, s->b2, s->b3, s->b4, s->b5); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5); s->~Self(); } }; template struct Operations { static void copy(void *_dest, void *_src) { new (_dest) Self(*(Self *)_src); } static void call(void *data) { Self *s = static_cast(data); ((s->b0)->*(s->b1))(s->b2, s->b3, s->b4, s->b5); s->~Self(); } }; typedef Operations ops; }; template class Task; template class Task: public TaskBase { public: Task(TaskQueue *q = NULL, mbed::Callback cb = mbed::Callback()) : TaskBase(q), _args(cb) { } Task &operator=(mbed::Callback cb) { _args.b0 = cb; return *this; } void call() { post(); } protected: virtual uint32_t size() { return sizeof(_args); } virtual run_callback_t start(void *data, uint32_t max_size) { All::ops::copy(data, (void *)&_args); return &All::ops::call; } private: typedef AllArgs > All; All _args; }; template class Task: public TaskBase { public: Task(TaskQueue *q = NULL, mbed::Callback cb = mbed::Callback()) : TaskBase(q), _args(cb) { } Task &operator=(mbed::Callback cb) { _args.b0 = cb; return *this; } void call(A0 a0) { _args.b1 = a0; post(); } protected: virtual uint32_t size() { return sizeof(_args); } virtual run_callback_t start(void *data, uint32_t max_size) { All::ops::copy(data, (void *)&_args); return &All::ops::call; } private: typedef AllArgs, A0> All; All _args; }; /** Task * * Representation of a postable task */ template class Task: public TaskBase { public: /** * Construct a new task * * @param q TaskQueue to post to * @param cb Callback to run */ Task(TaskQueue *q = NULL, mbed::Callback cb = mbed::Callback()) : TaskBase(q), _args(cb) { } /** * Set the callback of this task * * @param cb Callback to run */ Task &operator=(mbed::Callback cb) { _args.b0 = cb; return *this; } /** * Post this task for execution * * The number of arguments to call should match * the type of the callback. For example Task * expects two integers as arguments to call, while Task * expects no arguments. * * @param a0 First callback parameter * @param a1 Second callback parameter */ void call(A0 a0, A1 a1) { _args.b1 = a0; _args.b2 = a1; post(); } protected: virtual uint32_t size() { return sizeof(_args); } virtual run_callback_t start(void *data, uint32_t max_size) { All::ops::copy(data, (void *)&_args); return &All::ops::call; } private: typedef AllArgs, A0, A1> All; All _args; }; /** @}*/ } #endif