dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Port.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 <optional>
14#include <string>
15
16// Project includes
17#include "../channels/Channel.hpp"
18#include "Kind.hpp"
19#include <dfx-hooks/Port.hpp>
22
23namespace dfx::Core
24{
25class Node;
26class Port;
27
28using NodePtr = std::shared_ptr<Node>;
29using NodeWPtr = std::weak_ptr<Node>;
30
32template<typename T>
33concept DerivedFromPort = std::derived_from<T, Port>;
34
65class Port
66{
67public:
69 using Id = uint32_t;
70
71public:
73 enum class Mode
74 {
77 };
78
79public:
83 virtual ~Port();
84
87 NodePtr node() const noexcept { return _node.lock(); }
88
91
93 Id id() const noexcept { return _id; }
95 std::string const & name() const noexcept { return _name; }
97 Mode mode() const noexcept { return _mode; }
99 Kind kind() const noexcept { return _kind; }
101 bool isInput() const noexcept { return _mode == Mode::Input; }
103 bool isOutput() const noexcept { return _mode == Mode::Output; }
106 std::optional<bool> allowsMimeTypePropagation() const noexcept { return _allowsMimeTypePropagation; }
107
109
115 template<DerivedFromPort T>
116 T & as() noexcept { return static_cast<T &>(*this); }
117
123 template<DerivedFromPort T>
124 T const & as() const noexcept { return static_cast<T const &>(*this); }
125
126public:
129
140
144
146 bool hasChannel(ChannelPtr channel) const noexcept;
147
149 std::size_t channelCount() const noexcept { return _channels.size(); }
150
156 ChannelPtr channel(std::size_t index) const { return _channels.at(index); }
157
159
160public:
163
173
177
179
180public:
190 static void validatePortName(std::string_view name);
191
192protected:
201 Port(Id id, NodeWPtr node, Mode mode, Kind kind, std::string name, std::optional<bool> allowsMimeTypePropagation);
202
203protected:
206 std::vector<Hooks::Port *> hooks;
207
208private:
209 Id _id;
210 NodeWPtr _node;
211 Mode _mode;
212 Kind _kind;
213 std::string _name;
214 std::optional<bool> _allowsMimeTypePropagation;
215
216private:
217 std::vector<ChannelPtr> _channels;
218};
219} // !namespace dfx::Core
220
Convenience macros to explicitly control copy and move semantics.
Macro-based enum <-> string utilities for dfx.
#define DECLARE_ENUM_STRING_FUNCTIONS(E)
Declare the enum string API (and std::formatter) for enum type E.
Definition EnumString.hpp:327
Abstract base class for all nodes in the dfx runtime.
Definition Node.hpp:91
Base class for both input and output ports.
Definition Port.hpp:66
static void validatePortName(std::string_view name)
Validate a port name according to dfx rules.
virtual ~Port()
Virtual destructor for inheritance.
std::size_t channelCount() const noexcept
Number of attached channels.
Definition Port.hpp:149
std::vector< Hooks::Port * > hooks
Registered port hooks. Stored as raw pointers. Managed via registerHook and deregisterHook.
Definition Port.hpp:206
Port(Id id, NodeWPtr node, Mode mode, Kind kind, std::string name, std::optional< bool > allowsMimeTypePropagation)
Construct a port.
NodePtr node() const noexcept
Get the owning node.
Definition Port.hpp:87
Mode mode() const noexcept
Get the port mode.
Definition Port.hpp:97
void registerHook(Hooks::Port *hook)
Register a hook on this port.
ChannelPtr channel(std::size_t index) const
Get the attached channel at the given index.
Definition Port.hpp:156
std::string const & name() const noexcept
Get the port name.
Definition Port.hpp:95
void deregisterHook(Hooks::Port *hook)
Deregister a hook from this port. If the hook is not registered, this is a no-op.
Id id() const noexcept
Get the port id.
Definition Port.hpp:93
Mode
Port direction.
Definition Port.hpp:74
@ Output
Outgoing endpoint.
Definition Port.hpp:76
@ Input
Incoming endpoint.
Definition Port.hpp:75
T const & as() const noexcept
Unchecked cast to a derived port type.
Definition Port.hpp:124
bool isOutput() const noexcept
Check is this is an output port.
Definition Port.hpp:103
void removeChannel(ChannelPtr channel)
Detach a channel from this port. If the channel is not present, this is a no-op.
Kind kind() const noexcept
Get the port kind.
Definition Port.hpp:99
T & as() noexcept
Unchecked cast to a derived port type.
Definition Port.hpp:116
ENABLE_DEFAULT_MOVE_DISABLE_COPY(Port)
Move is allowed, copy is disabled.
std::optional< bool > allowsMimeTypePropagation() const noexcept
Check if this node allow mime-type propagation.
Definition Port.hpp:106
uint32_t Id
Identifier type of a port (unique within a node by not unique accross a graph).
Definition Port.hpp:69
bool isInput() const noexcept
Check is this is an input port.
Definition Port.hpp:101
bool hasChannel(ChannelPtr channel) const noexcept
Check whether a channel is attached to this port.
void addChannel(ChannelPtr channel)
Attach a channel to this port.
Port-level message observation hook.
Definition Port.hpp:44
Concept used by dfx::Core::Port::as() to constrain types.
Definition Port.hpp:33
Definition Channel.hpp:22
std::shared_ptr< Node > NodePtr
Shared ownership pointer type for Nodes..
Definition Node.hpp:61
std::weak_ptr< Node > NodeWPtr
Weak pointer type for Nodes.
Definition Node.hpp:63
Kind
Port kind (connection domain).
Definition Kind.hpp:29
std::shared_ptr< Channel > ChannelPtr
Shared ownership handle for channels.
Definition Channel.hpp:147