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 const void *get_value_ref_void() const = 0;
101 virtual void *get_value_ref_void() = 0;
102
107 virtual void set_data(std::shared_ptr<BaseData> /* data */) {}
108
109protected:
110 std::string data_type;
111
112private:
113 std::string label = "no label";
114};
115
124template <typename T> class Input : public Port
125{
126public:
130 Input() = default;
131
136 Input(std::string label) : Port(label) { this->data_type = typeid(T).name(); }
137
141 virtual ~Input() = default;
142
151 PortType get_port_type() const override { return PortType::IN; }
152
159 {
160 auto locked = this->data.lock();
161 return locked ? locked->get_value_ref() : nullptr;
162 }
163
164 const T *get_value_ref() const
165 {
166 auto locked = this->data.lock();
167 return locked ? locked->get_value_ref() : nullptr;
168 }
169
175 void *get_value_ref_void() override
176 {
177 auto locked = this->data.lock();
178 return locked ? static_cast<void *>(locked->get_value_ref()) : nullptr;
179 }
180
181 const void *get_value_ref_void() const override
182 {
183 auto locked = this->data.lock();
184 return locked ? static_cast<const void *>(locked->get_value_ref())
185 : nullptr;
186 }
187
192 void set_data(std::shared_ptr<BaseData> data) override
193 {
194 this->data = std::dynamic_pointer_cast<Data<T>>(std::move(data));
195 }
196
197private:
198 std::weak_ptr<Data<T>>
200};
201
210template <typename T> class Output : public Port
211{
212public:
217 {
218 this->data_type = typeid(T).name();
219 }
220
233 template <typename... Args>
234 explicit Output(std::string label, Args &&...args)
235 : Port(label),
237 {
238 this->data_type = typeid(T).name();
239 }
240
244 virtual ~Output() = default;
245
251 std::shared_ptr<BaseData> get_data_shared_ptr_downcasted() const override
252 {
253 return std::static_pointer_cast<BaseData>(this->data);
254 }
255
260 std::string get_data_type() const { return typeid(T).name(); }
261
270 PortType get_port_type() const override { return PortType::OUT; }
271
276 const T *get_value_ref() const { return this->data->get_value_ref(); }
277 T *get_value_ref() { return this->data->get_value_ref(); }
278
284 const void *get_value_ref_void() const override
285 {
286 return static_cast<void *>(this->data->get_value_ref());
287 }
288
289 void *get_value_ref_void() override
290 {
291 return static_cast<void *>(this->data->get_value_ref());
292 }
293
294private:
295 std::shared_ptr<Data<T>>
297};
298
299} // namespace gnode
Template class for holding data of a specific type.
Definition data.hpp:84
Template class for input ports, specialized by data type.
Definition port.hpp:125
void set_data(std::shared_ptr< BaseData > data) override
Sets the data associated with this input port.
Definition port.hpp:192
std::weak_ptr< Data< T > > data
A weak pointer to the data associated with this input port.
Definition port.hpp:199
const void * get_value_ref_void() const override
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition port.hpp:181
Input()=default
Default constructor for Input.
const T * get_value_ref() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition port.hpp:164
virtual ~Input()=default
Virtual destructor for Input.
void * get_value_ref_void() override
Retrieves a void* reference to the data value stored in this output port.
Definition port.hpp:175
PortType get_port_type() const override
Returns the type of the port as an input port.
Definition port.hpp:151
T * get_value_ref()
Retrieves a reference to the data value stored in this input port.
Definition port.hpp:158
Input(std::string label)
Constructs an Input port with the specified label.
Definition port.hpp:136
Template class for output ports, specialized by data type.
Definition port.hpp:211
std::string get_data_type() const
Retrieves the type name of the data handled by this output port.
Definition port.hpp:260
PortType get_port_type() const override
Returns the type of the port as an output port.
Definition port.hpp:270
T * get_value_ref()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition port.hpp:277
const void * get_value_ref_void() const override
Retrieves a void* reference to the data value stored in this output port.
Definition port.hpp:284
void * get_value_ref_void() override
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition port.hpp:289
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:251
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:234
std::shared_ptr< Data< T > > data
A shared pointer to the data associated with this output port.
Definition port.hpp:296
Output()
Default constructor for Output.
Definition port.hpp:216
const T * get_value_ref() const
Retrieves a reference to the data value stored in this output port.
Definition port.hpp:276
Abstract base class representing a port in a node.
Definition port.hpp:45
std::string label
The label of the port.
Definition port.hpp:113
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:110
virtual void * get_value_ref_void()=0
This is an overloaded member function, provided for convenience. It differs from the above function o...
virtual const void * get_value_ref_void() const =0
Retrieves a void* reference to the data value stored in this output port.
virtual void set_data(std::shared_ptr< BaseData >)
Sets the data associated with the port.
Definition port.hpp:107
virtual PortType get_port_type() const =0
Pure virtual function to get the type of the port (IN or OUT).
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