conduit 0.6.0
Modern C++23 header-only event-dispatching / event-transport library
Loading...
Searching...
No Matches
filtered_transport.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <conduit/bus.hpp>
10
11#include <functional>
12#include <memory>
13#include <utility>
14
15namespace conduit {
16
27public:
28 using Predicate = std::function<bool(const EventEnvelopeView&)>;
29
30 explicit FilteredTransport(std::shared_ptr<Transport> inner,
31 Predicate outbound = {},
32 Predicate inbound = {})
33 : inner_(std::move(inner)), outbound_(std::move(outbound)), inbound_(std::move(inbound)) {}
34
35 [[nodiscard]] TransportScope scope() const noexcept override {
36 return inner_->scope();
37 }
38
39 void attach(Bus& bus) override {
40 Predicate p = inbound_;
41 inner_->attach_with_sink(bus, [&bus, p = std::move(p)](const EventEnvelopeView& v) {
42 if (!p) {
44 return;
45 }
46 bool allow = false;
47 try {
48 allow = p(v);
49 } catch (...) {
50 allow = false;
51 }
52 if (allow) {
54 }
55 });
56 }
57
58 void attach_with_sink(Bus& bus, InboundSink sink) override {
59 Predicate p = inbound_;
60 inner_->attach_with_sink(
61 bus, [sink = std::move(sink), p = std::move(p)](const EventEnvelopeView& v) {
62 if (!p) {
63 if (sink)
64 sink(v);
65 return;
66 }
67 bool allow = false;
68 try {
69 allow = p(v);
70 } catch (...) {
71 allow = false;
72 }
73 if (allow && sink) {
74 sink(v);
75 }
76 });
77 }
78
79 void detach() noexcept override {
80 inner_->detach();
81 }
82
83 void dispatch(const EventEnvelopeView& v) override {
84 if (outbound_) {
85 bool allow = false;
86 try {
87 allow = outbound_(v);
88 } catch (...) {
89 allow = false;
90 }
91 if (!allow) {
92 return;
93 }
94 }
95 inner_->dispatch(v);
96 }
97
98 void flush() override {
99 inner_->flush();
100 }
101
102 [[nodiscard]] const std::shared_ptr<Transport>& inner() const noexcept {
103 return inner_;
104 }
105
106private:
107 std::shared_ptr<Transport> inner_;
108 Predicate outbound_;
109 Predicate inbound_;
110};
111
112} // namespace conduit
Bus — owns transports, middleware, listeners; dispatches envelopes.
Definition bus.hpp:58
void deliver_to_listeners(const EventEnvelope &v) const
Called by transports (e.g.
Definition bus.hpp:251
Polymorphic envelope cell.
Definition envelope.hpp:62
Wrapper that gates events flowing through an inner transport on both legs.
Definition filtered_transport.hpp:26
void attach_with_sink(Bus &bus, InboundSink sink) override
Attach to a bus using a caller-supplied inbound sink.
Definition filtered_transport.hpp:58
void attach(Bus &bus) override
Attach to a bus.
Definition filtered_transport.hpp:39
void flush() override
Definition filtered_transport.hpp:98
const std::shared_ptr< Transport > & inner() const noexcept
Definition filtered_transport.hpp:102
FilteredTransport(std::shared_ptr< Transport > inner, Predicate outbound={}, Predicate inbound={})
Definition filtered_transport.hpp:30
void detach() noexcept override
Definition filtered_transport.hpp:79
void dispatch(const EventEnvelopeView &v) override
Definition filtered_transport.hpp:83
TransportScope scope() const noexcept override
Definition filtered_transport.hpp:35
std::function< bool(const EventEnvelopeView &)> Predicate
Definition filtered_transport.hpp:28
Definition transport.hpp:30
Bus * bus() const noexcept
Definition transport.hpp:68
EventEnvelope — a parcel cell carrying conduit's envelope metadata plus a polymorphic payload cell.
Transport interface and the local/remote scope enum used for flag-based filtering.
Definition builder.hpp:22
std::function< void(const EventEnvelopeView &)> InboundSink
Callable installed on a Transport at attach time that receives inbound envelopes the transport pulled...
Definition transport.hpp:28
TransportScope
Distinguishes in-process transports from off-machine ones.
Definition transport.hpp:21