EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
ResultChannel.hpp
Go to the documentation of this file.
1
9
10#ifndef ECHOMAP_RESULTCHANNEL_HPP
11#define ECHOMAP_RESULTCHANNEL_HPP
12
13#include <sigc++/signal.h>
14
15#include <exception>
16#include <stdexcept>
17#include <type_traits>
18#include <utility>
19
20namespace echomap
21{
22
29template <class ResultT> class ResultChannel
30{
31public:
39 template <class SlotT>
40 [[nodiscard]] sigc::connection observe(
41 SlotT&& slot
42 )
43 {
44 return observers.connect(std::forward<SlotT>(slot));
45 }
46
56 template <class SlotT>
58 [[nodiscard]] sigc::connection nominate_consumer(
59 SlotT&& slot
60 )
61 {
62 if (!consumer.empty())
63 throw std::logic_error("Result channel already has a nominated consumer.");
64
65 return consumer.connect(std::forward<SlotT>(slot));
66 }
67
68private:
69 friend class WorkerResultDespatcher;
70
71 using ObserverSignalT = sigc::signal<void(const ResultT&)>;
72 using ConsumerSignalT = sigc::signal<void(ResultT&&)>;
73
79 void publish(
80 ResultT&& result
81 )
82 {
83 std::exception_ptr observer_exception;
84
85 // Emit const ResultT& to all observers.
86 try {
87 publish(result);
88 } catch (...) {
89 observer_exception = std::current_exception();
90 }
91
92 // If there is a nominated consumer, send it out.
93 if (!consumer.empty())
94 consumer.emit(std::move(result));
95
96 if (observer_exception)
97 std::rethrow_exception(observer_exception);
98 }
99
106 const ResultT& result
107 )
108 {
109 observers.emit(result);
110 }
111
112 ObserverSignalT observers;
113 ConsumerSignalT consumer;
114};
115
116} // namespace echomap
117
118#endif // ECHOMAP_RESULTCHANNEL_HPP
Maintains a type-safe multi-observer single-consumer message bus for WorkerResult objects.
sigc::connection observe(SlotT &&slot)
Provide a new slot as an observer on the channel.
sigc::connection nominate_consumer(SlotT &&slot)
Nominate the consumer slot for the channel.
void publish(const ResultT &result)
Publish a non-consumable result onto the channel for all observers.
void publish(ResultT &&result)
Publish a consumable result onto the channel for all observers, followed by the single consumer.
Manages channels for WorkerResult message routing.
T current_exception(T... args)
T forward(T... args)
T is_const_v
The main EchoMap outermost namespace for all non-exported symbols.
T rethrow_exception(T... args)