GNode library (C++)
Loading...
Searching...
No Matches
port.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 * Public License. The full license is in the file LICENSE, distributed with
3 * this software. */
4
17#pragma once
18#include "gnode/data.hpp"
19#include "gnode/logger.hpp"
20#include <memory>
21#include <string>
22#include <typeinfo>
23
24namespace gnode
25{
26
33{
35 OUT
36};
37
44class Port
45{
46public:
50 Port() = default;
51
56 Port(std::string label) : label(label) {}
57
61 virtual ~Port() = default;
62
67 std::string get_data_type() const { return this->data_type; }
68
73 std::string get_label() const { return this->label; }
74
80 virtual std::shared_ptr<BaseData> get_data_shared_ptr_downcasted() const
81 {
82 return nullptr;
83 }
84
93 virtual PortType get_port_type() const = 0;
94
100 virtual void *get_value_ref_void() const = 0;
101
106 virtual void set_data(std::shared_ptr<BaseData> /* data */) {}
107
108protected:
109 std::string data_type;
110
111private:
112 std::string label = "no label";
113};
114
123template <typename T> class Input : public Port
124{
125public:
129 Input() = default;
130
135 Input(std::string label) : Port(label) { this->data_type = typeid(T).name(); }
136
140 virtual ~Input() = default;
141
150 PortType get_port_type() const override { return PortType::IN; }
151
158 {
159 return this->data.lock() ? this->data.lock()->get_value_ref() : nullptr;
160 }
161
167 void *get_value_ref_void() const override
168 {
169 return this->data.lock() ? (void *)this->data.lock()->get_value_ref()
170 : nullptr;
171 }
172
177 void set_data(std::shared_ptr<BaseData> data) override
178 {
179 this->data = std::dynamic_pointer_cast<Data<T>>(std::move(data));
180 }
181
182private:
183 std::weak_ptr<Data<T>>
185};
186
195template <typename T> class Output : public Port
196{
197public:
202 {
203 this->data_type = typeid(T).name();
204 }
205
218 template <typename... Args>
219 explicit Output(std::string label, Args &&...args)
220 : Port(label),
222 {
223 this->data_type = typeid(T).name();
224 }
225
229 virtual ~Output() = default;
230
236 std::shared_ptr<BaseData> get_data_shared_ptr_downcasted() const override
237 {
238 return std::static_pointer_cast<BaseData>(this->data);
239 }
240
245 std::string get_data_type() const { return typeid(T).name(); }
246
255 PortType get_port_type() const override { return PortType::OUT; }
256
261 T *get_value_ref() const { return this->data->get_value_ref(); }
262
268 void *get_value_ref_void() const override
269 {
270 return (void *)this->data->get_value_ref();
271 }
272
273private:
274 std::shared_ptr<Data<T>>
276};
277
278} // namespace gnode
Template class for holding data of a specific type.
Definition data.hpp:83
Template class for input ports, specialized by data type.
Definition port.hpp:124
T * get_value_ref() const
Retrieves a reference to the data value stored in this input port.
Definition port.hpp:157
void set_data(std::shared_ptr< BaseData > data) override
Sets the data associated with this input port.
Definition port.hpp:177
std::weak_ptr< Data< T > > data
A weak pointer to the data associated with this input port.
Definition port.hpp:184
Input()=default
Default constructor for Input.
virtual ~Input()=default
Virtual destructor for Input.
PortType get_port_type() const override
Returns the type of the port as an input port.
Definition port.hpp:150
void * get_value_ref_void() const override
Retrieves a void* reference to the data value stored in this output port.
Definition port.hpp:167
Input(std::string label)
Constructs an Input port with the specified label.
Definition port.hpp:135
Template class for output ports, specialized by data type.
Definition port.hpp:196
std::string get_data_type() const
Retrieves the type name of the data handled by this output port.
Definition port.hpp:245
PortType get_port_type() const override
Returns the type of the port as an output port.
Definition port.hpp:255
std::shared_ptr< BaseData > get_data_shared_ptr_downcasted() const override
Retrieves a shared pointer to the data associated with this output port after downcasting.
Definition port.hpp:236
virtual ~Output()=default
Virtual destructor for Output.
Output(std::string label, Args &&...args)
Constructs an Output port with the specified label and additional arguments.
Definition port.hpp:219
std::shared_ptr< Data< T > > data
A shared pointer to the data associated with this output port.
Definition port.hpp:275
Output()
Default constructor for Output.
Definition port.hpp:201
T * get_value_ref() const
Retrieves a reference to the data value stored in this output port.
Definition port.hpp:261
void * get_value_ref_void() const override
Retrieves a void* reference to the data value stored in this output port.
Definition port.hpp:268
Abstract base class representing a port in a node.
Definition port.hpp:45
std::string label
The label of the port.
Definition port.hpp:112
virtual std::shared_ptr< BaseData > get_data_shared_ptr_downcasted() const
Retrieves a shared pointer to the data associated with the port after downcasting.
Definition port.hpp:80
std::string data_type
A string representing the type name.
Definition port.hpp:109
virtual void set_data(std::shared_ptr< BaseData >)
Sets the data associated with the port.
Definition port.hpp:106
virtual PortType get_port_type() const =0
Pure virtual function to get the type of the port (IN or OUT).
virtual void * get_value_ref_void() const =0
Retrieves a void* reference to the data value stored in this output port.
Port(std::string label)
Constructs a Port with the specified label.
Definition port.hpp:56
Port()=default
Default constructor for Port.
virtual ~Port()=default
Virtual destructor for Port.
std::string get_label() const
Retrieves the label of the port.
Definition port.hpp:73
std::string get_data_type() const
Retrieves the type name of the data handled by this port.
Definition port.hpp:67
Defines the BaseData and Data classes for handling typed data in the gnode namespace.
Defines the Logger class for managing logging functionality.
Definition data.hpp:23
PortType
Enumeration for port types, indicating whether a port is an input or an output.
Definition port.hpp:33
@ IN
Represents an input port.
Definition port.hpp:34
@ OUT
Represents an output port.
Definition port.hpp:35