blob: 71f13951b9a0716927029f9526e71997f03b4de4 [file] [log] [blame]
Georgios Pinitasc0d1c862018-03-23 15:13:15 +00001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_MISC_SIGNAL_H__
25#define __ARM_COMPUTE_MISC_SIGNAL_H__
26
27#include <functional>
28
29namespace arm_compute
30{
31namespace utils
32{
33namespace signal
34{
35namespace detail
36{
37/** Base signal class */
38template <typename SignalType>
39class SignalImpl;
40
41/** Signal class function specialization */
42template <typename ReturnType, typename... Args>
43class SignalImpl<ReturnType(Args...)>
44{
45public:
46 using Callback = std::function<ReturnType(Args...)>;
47
48public:
49 /** Default Constructor */
50 SignalImpl() = default;
51
52 /** Connects signal
53 *
54 * @param[in] cb Callback to connect the signal with
55 */
56 void connect(const Callback &cb)
57 {
58 _cb = cb;
59 }
60
61 /** Disconnects the signal */
62 void disconnect()
63 {
64 _cb = nullptr;
65 }
66
67 /** Checks if the signal is connected
68 *
69 * @return True if there is a connection else false
70 */
71 bool connected() const
72 {
73 return (_cb != nullptr);
74 }
75
76 /** Calls the connected callback
77 *
78 * @param[in] args Callback arguments
79 */
80 void operator()(Args &&... args)
81 {
82 if(_cb)
83 {
84 _cb(std::forward<Args>(args)...);
85 }
86 }
87
88private:
89 Callback _cb{}; /**< Signal callback */
90};
91} // namespace detail
92
93/** Signal alias */
94template <class T>
95using Signal = detail::SignalImpl<T>;
96} // namespace signal
97} // namespace utils
98} // namespace arm_compute
99#endif /* __ARM_COMPUTE_MISC_SIGNAL_H__ */