dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Channel.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <concepts>
13#include <memory>
14#include <optional>
15
16// Project includes
17#include "../messages/Message.hpp"
18#include <dfx-hooks/Delivery.hpp>
20
21namespace dfx::Core
22{
23class InputPort;
24class OutputPort;
25class Channel;
26
28template<typename T>
29concept DerivedFromChannel = std::derived_from<T, Channel>;
30
54{
55public:
57 using Id = uint32_t;
58
59public:
66
69
71 virtual ~Channel() = default;
72
74 Id id() const noexcept { return _id; }
75
76public:
79
85 virtual bool pushMessage(MessagePtr msg) = 0;
86
88 virtual bool hasPendingMessage() const = 0;
89
91 virtual std::size_t pendingMessageCount() const = 0;
92
95 virtual std::optional<MessagePtr> pop() = 0;
96
98
99public:
102 template<DerivedFromChannel T>
103 bool is() const noexcept { return dynamic_cast<T const *>(this) != nullptr; }
104
109 template<DerivedFromChannel T>
110 T & as() noexcept { return static_cast<T &>(*this); }
111
116 template<DerivedFromChannel T>
117 T const & as() const noexcept { return static_cast<T const &>(*this); }
118
119public:
121 InputPort & inputPort() const noexcept { return _inputPort; }
122
124 OutputPort & outputPort() const noexcept { return _outputPort; }
125
126public:
134 { _deliveryHook = hook; }
135
136protected:
139
141
142private:
143 Id _id;
144};
145
147using ChannelPtr = std::shared_ptr<Channel>;
148} // !namespace dfx::Core
Convenience macros to explicitly control copy and move semantics.
Abstract message channel connecting exactly one output port to one input port.
Definition Channel.hpp:54
virtual bool hasPendingMessage() const =0
Whether at least one message is pending in this channel.
virtual std::size_t pendingMessageCount() const =0
Number of messages currently pending in this channel.
Channel(Id id, InputPort &inputPort, OutputPort &outputPort)
Construct a channel linking one input port to one output port.
InputPort & inputPort() const noexcept
Destination (consumer) endpoint of this channel.
Definition Channel.hpp:121
uint32_t Id
Identifier type (unique within a graph instance, by convention).
Definition Channel.hpp:57
OutputPort & _outputPort
Source port (producer).
Definition Channel.hpp:138
bool is() const noexcept
Check whether this channel is of a given derived type.
Definition Channel.hpp:103
T & as() noexcept
Cast this channel to a derived type (unchecked).
Definition Channel.hpp:110
DISABLE_COPY_AND_MOVE(Channel)
Channel is not copiable nor movable.
InputPort & _inputPort
Destination port (consumer).
Definition Channel.hpp:137
virtual std::optional< MessagePtr > pop()=0
Pop the next message from the channel.
void setDeliveryHook(Hooks::Delivery *hook)
Set the optional delivery hook for this channel.
Definition Channel.hpp:133
Id id() const noexcept
Get the channel id.
Definition Channel.hpp:74
virtual ~Channel()=default
Virtual destructor.
T const & as() const noexcept
Cast this channel to a derived type (unchecked).
Definition Channel.hpp:117
virtual bool pushMessage(MessagePtr msg)=0
Push a message into the channel from the producer side.
Hooks::Delivery * _deliveryHook
Optional delivery hook (non-owning).
Definition Channel.hpp:140
OutputPort & outputPort() const noexcept
Source (producer) endpoint of this channel.
Definition Channel.hpp:124
Incoming message endpoint attached to a node.
Definition InputPort.hpp:55
Outgoing message endpoint attached to a node.
Definition OutputPort.hpp:47
Hook interface invoked around message enqueue in an dfx::Core::Channel.
Definition Delivery.hpp:50
Concept used by dfx::Core::Channel::is() and dfx::Core::Channel::as() to constrain types.
Definition Channel.hpp:29
Definition Channel.hpp:22
std::unique_ptr< Message > MessagePtr
Unique ownership handle for messages.
Definition Message.hpp:27
std::shared_ptr< Channel > ChannelPtr
Shared ownership handle for channels.
Definition Channel.hpp:147