Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_COMMON_I_EVENT_THREAD_H_
21 : #define FIREBOLT_RIALTO_COMMON_I_EVENT_THREAD_H_
22 :
23 : #include <atomic>
24 : #include <condition_variable>
25 : #include <functional>
26 : #include <list>
27 : #include <memory>
28 : #include <mutex>
29 : #include <string>
30 : #include <thread>
31 : #include <utility>
32 : #include <vector>
33 :
34 : namespace firebolt::rialto::common
35 : {
36 : class IEventThread;
37 :
38 : /**
39 : * @brief IEventThreadFactory factory class, returns a concrete implementation of IEventThread
40 : */
41 : class IEventThreadFactory
42 : {
43 : public:
44 382 : IEventThreadFactory() = default;
45 382 : virtual ~IEventThreadFactory() = default;
46 :
47 : /**
48 : * @brief Creates a IEventThreadFactory instance.
49 : *
50 : * @retval the factory instance or null on error.
51 : */
52 : static std::shared_ptr<IEventThreadFactory> createFactory();
53 :
54 : /**
55 : * @brief Creates an IEventThread object.
56 : *
57 : * @param[in] threadName : The name of the thread
58 : *
59 : * @retval the new event thread instance or null on error.
60 : */
61 : virtual std::unique_ptr<IEventThread> createEventThread(std::string threadName = std::string()) const = 0;
62 : };
63 :
64 : class IEventThread
65 : {
66 : public:
67 400 : IEventThread() = default;
68 400 : virtual ~IEventThread() = default;
69 :
70 : IEventThread(const IEventThread &) = delete;
71 : IEventThread &operator=(const IEventThread &) = delete;
72 : IEventThread(IEventThread &&) = delete;
73 : IEventThread &operator=(IEventThread &&) = delete;
74 :
75 : /**
76 : * @brief Flush any waiting events.
77 : */
78 : virtual void flush() = 0;
79 :
80 : /**
81 : * @brief Add the event handler function.
82 : *
83 : * @param[in] func : Function to call on event.
84 : */
85 52 : template <class Function> inline void add(Function &&func) { this->addImpl(std::forward<Function>(func)); }
86 :
87 : /**
88 : * @brief Add the event handler function with arguments.
89 : *
90 : * @param[in] func : Function to call on event.
91 : * @param[in] args : Arguments to pass into the function.
92 : */
93 94 : template <class Function, class... Args> inline void add(Function &&func, Args &&...args)
94 : {
95 94 : this->addImpl(std::bind(std::forward<Function>(func), std::forward<Args>(args)...));
96 : }
97 :
98 : private:
99 : virtual void addImpl(std::function<void()> &&func) = 0;
100 : };
101 :
102 : }; // namespace firebolt::rialto::common
103 :
104 : #endif // FIREBOLT_RIALTO_COMMON_I_EVENT_THREAD_H_
|