|
| 1 | +// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @file SlotThreadPool.hpp |
| 17 | + * |
| 18 | + * This file contains class SlotThreadPool definition. |
| 19 | + */ |
| 20 | + |
| 21 | +#ifndef _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ |
| 22 | +#define _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ |
| 23 | + |
| 24 | +#include <functional> |
| 25 | +#include <memory> |
| 26 | + |
| 27 | +namespace eprosima { |
| 28 | +namespace ddsrouter { |
| 29 | +namespace utils { |
| 30 | + |
| 31 | +class IThreadPool |
| 32 | +{ |
| 33 | +public: |
| 34 | + void execute(std::unique_ptr<ITask>&& task){} |
| 35 | +}; |
| 36 | + |
| 37 | +class ITask |
| 38 | +{ |
| 39 | +public: |
| 40 | + void operator() () noexcept; |
| 41 | +}; |
| 42 | + |
| 43 | +class BasicTask : ITask |
| 44 | +{ |
| 45 | +public: |
| 46 | + BasicTask(const std::function<void()>* callback_ptr){} |
| 47 | + void operator() () noexcept; |
| 48 | +}; |
| 49 | + |
| 50 | +class OwnedTask : ITask |
| 51 | +{ |
| 52 | +public: |
| 53 | + OwnedTask(const std::function<void()>& callback_){} |
| 54 | + OwnedTask(std::function<void()>&& callback_){} |
| 55 | + void operator() () noexcept; |
| 56 | +}; |
| 57 | + |
| 58 | +template <typename ... Args> |
| 59 | +class ConnectorOneShotArgs |
| 60 | +{ |
| 61 | +public: |
| 62 | + static void execute(const IThreadPool& tp, const std::function<void(Args...)>& callback, Args... args){} |
| 63 | + static void execute(const IThreadPool& tp, std::function<void(Args...)>&& callback, Args... args){} |
| 64 | +}; |
| 65 | +using ConnectorOneShot = ConnectorOneShotArgs<>; |
| 66 | + |
| 67 | +template <typename ... Args> |
| 68 | +class ConnectorSlotArgs |
| 69 | +{ |
| 70 | +public: |
| 71 | + ConnectorSlotArgs(const IThreadPool& tp, const std::function<void(Args...)>& callback){} |
| 72 | + // ConnectorSlotArgs(const IThreadPool& tp, std::function<void<(Args...)>&& callback){} |
| 73 | + void execute(Args...){} |
| 74 | +}; |
| 75 | +using ConnectorSlot = ConnectorSlotArgs<>; |
| 76 | + |
| 77 | +int main() |
| 78 | +{ |
| 79 | + IThreadPool pool; |
| 80 | + |
| 81 | + ConnectorOneShot::execute(pool, [](){ /* do something */ }); |
| 82 | + ConnectorOneShotArgs<int>::execute(pool, [](int x){ /* do something */ }, 3); |
| 83 | + ConnectorOneShotArgs<std::string, bool>::execute(pool, [](std::string s, bool b){ /* do something */ }, std::string("hello"), true); |
| 84 | + |
| 85 | + ConnectorSlot slot_connector(pool, [](){ /* do something */ }); |
| 86 | + slot_connector.execute(); |
| 87 | + |
| 88 | + ConnectorSlotArgs<int> slot_args(pool, [](int x){}); |
| 89 | + slot_args.execute(2); |
| 90 | + |
| 91 | + ConnectorSlotArgs<std::string, bool> slot_args_2(pool, std::function<void(std::string, bool)>([](std::string s, bool b){})); |
| 92 | + slot_args_2.execute(std::string("hello"), true); |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +} /* namespace utils */ |
| 97 | +} /* namespace ddsrouter */ |
| 98 | +} /* namespace eprosima */ |
| 99 | + |
| 100 | +#endif /* _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ */ |
0 commit comments