GNode library (C++)
Loading...
Searching...
No Matches
node.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
15#pragma once
16#include <memory>
17#include <stdexcept>
18#include <vector>
19
20#include "gnode/data.hpp"
21#include "gnode/port.hpp"
22#include "gnode/event.hpp"
23
24namespace gnode
25{
26
27class Graph; // forward
28
34class Node
35{
36public:
41 bool is_dirty = false;
42
47
52
56 Node() = default;
57
63 Node(std::string label) : label(label) {}
64
76 Node(std::string label, std::string id) : label(label), id(id) {}
77
81 virtual ~Node() = default;
82
99 template <typename T, typename... Args>
100 void add_port(PortType port_type,
101 const std::string &port_label,
102 Args &&...args)
103 {
104 if (port_type == PortType::IN)
105 this->ports.push_back(std::make_shared<gnode::Input<T>>(port_label));
106 else
107 this->ports.push_back(
108 std::make_shared<gnode::Output<T>>(port_label,
109 std::forward<Args>(args)...));
110 }
111
116 virtual void compute() = 0;
117
124 std::shared_ptr<BaseData> get_base_data(int port_index);
125
132 std::shared_ptr<BaseData> get_base_data(const std::string &port_label);
133
143 std::string get_data_type(int port_index) const;
144
150 std::string get_graph_id() const;
151
157 std::string get_label() const { return this->label; }
158
164 std::string get_id() const { return this->id; }
165
171 int get_nports() const;
172
179 int get_nports(PortType port_type) const;
180
187 std::shared_ptr<BaseData> get_output_data(int port_index) const;
188
194 Graph *get_p_graph() const { return this->p_graph; }
195
202 int get_port_index(const std::string &port_label) const;
203
210 std::string get_port_label(int port_index) const;
211
218 PortType get_port_type(const std::string &port_label) const;
219
223 const std::vector<std::shared_ptr<Port>> &get_ports() const
224 {
225 return this->ports;
226 };
227
235 template <typename T> T *get_value_ref(const std::string &port_label) const
236 {
237 for (const auto &port : this->ports)
238 {
239 if (port->get_label() == port_label)
240 {
241 if (port->get_port_type() == PortType::IN)
242 {
243 auto inputPort = std::dynamic_pointer_cast<Input<T>>(port);
244 if (!inputPort)
245 throw std::runtime_error("Type mismatch for port: " + port_label);
246 return inputPort->get_value_ref();
247 }
248 else
249 {
250 auto outputPort = std::dynamic_pointer_cast<Output<T>>(port);
251 if (!outputPort)
252 throw std::runtime_error("Type mismatch for port: " + port_label);
253 return outputPort->get_value_ref();
254 }
255 }
256 }
257 throw std::runtime_error("Port not found: " + port_label);
258 }
259
267 template <typename T> T *get_value_ref(int port_index) const
268 {
269 if (port_index < 0 || port_index >= static_cast<int>(this->ports.size()))
270 throw std::out_of_range("Port index is out of range: " +
271 std::to_string(port_index));
272
273 if (this->ports[port_index]->get_port_type() == PortType::IN)
274 {
275 auto port = std::dynamic_pointer_cast<Input<T>>(this->ports[port_index]);
276 if (!port)
277 throw std::runtime_error("Type mismatch for port index: " +
278 std::to_string(port_index));
279 return port->get_value_ref();
280 }
281 else
282 {
283 auto port = std::dynamic_pointer_cast<Output<T>>(this->ports[port_index]);
284 if (!port)
285 throw std::runtime_error("Type mismatch for port index: " +
286 std::to_string(port_index));
287 return port->get_value_ref();
288 }
289 }
290
297 void *get_value_ref_void(int port_index) const
298 {
299 if (port_index < 0 || port_index >= static_cast<int>(this->ports.size()))
300 return nullptr;
301
302 return this->ports[port_index]->get_value_ref_void();
303 }
304
310 bool has_port(const std::string &port_label) const;
311
319 template <typename T> bool has_port(const std::string &port_label) const
320 {
321 int index = this->get_port_index(port_label);
322 if (index == -1) return false;
323 return this->ports[index]->get_data_type() == typeid(T).name();
324 }
325
331 bool is_port_connected(int port_index) const;
332
338 bool is_port_connected(const std::string &port_label) const;
339
349 void set_id(std::string new_id) { this->id = new_id; }
350
357 void set_input_data(std::shared_ptr<BaseData> data, int port_index);
358
364 void set_p_graph(Graph *new_p_graph) { this->p_graph = new_p_graph; }
365
373 template <typename T>
374 void set_value(const std::string &port_label, T new_value)
375 {
376 T *p_value = this->get_value_ref<T>(port_label);
377 if (!p_value)
378 throw std::runtime_error("set_value: cannot set value: " + port_label);
380 }
381
386 void update();
387
388private:
392 std::string label;
393
397 std::string id;
398
402 std::vector<std::shared_ptr<Port>> ports;
403
407 Graph *p_graph = nullptr;
408};
409
410} // namespace gnode
Thread-safe publisher-subscriber event stream.
Definition event.hpp:100
The Graph class provides methods for manipulating nodes and connections in a directed graph structure...
Definition graph.hpp:37
Template class for input ports, specialized by data type.
Definition port.hpp:125
Abstract Node class that represents a basic building block in a graph-based system.
Definition node.hpp:35
Node(std::string label)
Construct a new Node object with a specific label.
Definition node.hpp:63
Graph * get_p_graph() const
Get the reference to the belonging graph.
Definition node.hpp:194
std::vector< std::shared_ptr< Port > > ports
A vector of shared pointers to the node's ports.
Definition node.hpp:402
std::string get_graph_id() const
Get the id of the graph.
Event< Node & > post_update_event
Event triggered after node computation finishes.
Definition node.hpp:51
T * get_value_ref(int port_index) const
Get a reference to the value stored in a port by its index.
Definition node.hpp:267
std::string get_port_label(int port_index) const
Get the label of a port by its index.
Node()=default
Default constructor for Node.
std::string id
The ID of the node.
Definition node.hpp:397
int get_nports() const
Get the total number of ports on the node.
Event< Node & > pre_update_event
Event triggered before node computation starts.
Definition node.hpp:46
Graph * p_graph
Reference to the graph the node belong to, if any.
Definition node.hpp:407
void set_value(const std::string &port_label, T new_value)
Set the value of a port by its label.
Definition node.hpp:374
std::string get_data_type(int port_index) const
Get the data type of a specific port (input or output).
void update()
Update the node, which involves processing its input and output ports.
std::shared_ptr< BaseData > get_base_data(const std::string &port_label)
Retrieves a shared pointer to the data associated with the port after downcasting.
std::shared_ptr< BaseData > get_output_data(int port_index) const
Get the output data from a specific port index.
int get_port_index(const std::string &port_label) const
Get the index of a port by its label.
bool has_port(const std::string &port_label) const
Checks whether a port with the given label exists.
T * get_value_ref(const std::string &port_label) const
Get a reference to the value stored in a port by its label.
Definition node.hpp:235
std::shared_ptr< BaseData > get_base_data(int port_index)
Retrieves a shared pointer to the data associated with the port after downcasting.
std::string get_id() const
Get the ID of the node.
Definition node.hpp:164
PortType get_port_type(const std::string &port_label) const
Get the type of a port (input or output) by its label.
void set_p_graph(Graph *new_p_graph)
Set the reference to the belonging graph.
Definition node.hpp:364
std::string get_label() const
Get the label of the node.
Definition node.hpp:157
std::string label
The label of the node.
Definition node.hpp:392
void set_id(std::string new_id)
Set a new identifier for the node.
Definition node.hpp:349
bool is_dirty
Flag indicating whether the node is marked as dirty (requiring an update).
Definition node.hpp:41
virtual void compute()=0
Pure virtual function that forces derived classes to implement the compute method,...
virtual ~Node()=default
Virtual destructor for Node.
void set_input_data(std::shared_ptr< BaseData > data, int port_index)
Set input data on a specific port by its index.
bool has_port(const std::string &port_label) const
Checks whether a port exists and matches the requested type.
Definition node.hpp:319
Node(std::string label, std::string id)
Construct a new Node object with a specific label and identifier.
Definition node.hpp:76
void * get_value_ref_void(int port_index) const
Get a void* reference to the value stored in a port by its index.
Definition node.hpp:297
const std::vector< std::shared_ptr< Port > > & get_ports() const
Get the ports.
Definition node.hpp:223
bool is_port_connected(int port_index) const
Check if a port is connected by its index.
bool is_port_connected(const std::string &port_label) const
Check if a port is connected by its label.
void add_port(PortType port_type, const std::string &port_label, Args &&...args)
Add a port to the node, specifying whether it is an input or output.
Definition node.hpp:100
int get_nports(PortType port_type) const
Get the number of ports of a specific type (input or output).
Template class for output ports, specialized by data type.
Definition port.hpp:211
Defines the BaseData and Data classes for handling typed data in the gnode namespace.
Thread-safe publisher-subscriber event stream.
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
Defines the Port, Input, and Output classes, along with the PortType enumeration for handling ports i...