Line data Source code
1 : /**
2 : *
3 : * @file CallbackAdapter.hpp IPM CallbackAdapter class
4 : *
5 : * Manages a thread which performs repeated Receiver::receive calls and calls the given callback method if data is
6 : * received.
7 : *
8 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
9 : * Licensing/copyright details are in the COPYING file that you should have
10 : * received with this code.
11 : */
12 :
13 : #ifndef IPM_SRC_CALLBACKADAPTER_HPP_
14 : #define IPM_SRC_CALLBACKADAPTER_HPP_
15 :
16 : #include "ipm/Receiver.hpp"
17 :
18 : #include <atomic>
19 : #include <functional>
20 : #include <memory>
21 : #include <mutex>
22 : #include <thread>
23 :
24 : namespace dunedaq::ipm {
25 :
26 : class CallbackAdapter
27 : {
28 : public:
29 43 : CallbackAdapter() = default; // Explicitly defaulted
30 :
31 : virtual ~CallbackAdapter() noexcept;
32 :
33 : void set_receiver(Receiver* receiver_ptr);
34 : void set_callback(std::function<void(Receiver::Response&)> callback);
35 : void clear_callback();
36 :
37 : private:
38 : void startup();
39 : void shutdown();
40 : void thread_loop();
41 :
42 : Receiver* m_receiver_ptr{ nullptr }; // Bare pointer used here as CallbackAdapter does not own the Receiver instance,
43 : // and until set_receiver is called, this will be null.
44 : std::function<void(Receiver::Response&)> m_callback{ nullptr };
45 : mutable std::mutex m_callback_mutex;
46 : std::unique_ptr<std::thread> m_thread{ nullptr };
47 : std::atomic<bool> m_is_listening{ false };
48 : };
49 : } // namespace dunedaq::ipm
50 :
51 : #endif // IPM_SRC_CALLBACKADAPTER_HPP_
|