GNode library (C++)
Loading...
Searching...
No Matches
gnode::Node Class Referenceabstract

Abstract Node class that represents a basic building block in a graph-based system. More...

#include <node.hpp>

Collaboration diagram for gnode::Node:

Public Member Functions

 Node ()=default
 Default constructor for Node.
 
 Node (std::string label)
 Construct a new Node object with a specific label.
 
 Node (std::string label, std::string id)
 Construct a new Node object with a specific label and identifier.
 
virtual ~Node ()=default
 Virtual destructor for Node.
 
template<typename T , typename... Args>
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.
 
virtual void compute ()=0
 Pure virtual function that forces derived classes to implement the compute method, which updates the node's state.
 
std::shared_ptr< BaseDataget_base_data (int port_index)
 Retrieves a shared pointer to the data associated with the port after downcasting.
 
std::shared_ptr< BaseDataget_base_data (const std::string &port_label)
 Retrieves a shared pointer to the data associated with the port after downcasting.
 
std::string get_data_type (int port_index) const
 Get the data type of a specific port (input or output).
 
std::string get_graph_id () const
 Get the id of the graph.
 
std::string get_label () const
 Get the label of the node.
 
std::string get_id () const
 Get the ID of the node.
 
int get_nports () const
 Get the total number of ports on the node.
 
int get_nports (PortType port_type) const
 Get the number of ports of a specific type (input or output).
 
std::shared_ptr< BaseDataget_output_data (int port_index) const
 Get the output data from a specific port index.
 
Graphget_p_graph () const
 Get the reference to the belonging graph.
 
int get_port_index (const std::string &port_label) const
 Get the index of a port by its label.
 
std::string get_port_label (int port_index) const
 Get the label of a port by its index.
 
PortType get_port_type (const std::string &port_label) const
 Get the type of a port (input or output) by its label.
 
const std::vector< std::shared_ptr< Port > > & get_ports ()
 Get the ports.
 
template<typename T >
T * get_value_ref (const std::string &port_label) const
 Get a reference to the value stored in a port by its label.
 
template<typename T >
T * get_value_ref (int port_index) const
 Get a reference to the value stored in a port by its index.
 
voidget_value_ref_void (int port_index) const
 Get a void* reference to the value stored in a port by its index.
 
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 set_id (std::string new_id)
 Set a new identifier for the node.
 
void set_input_data (std::shared_ptr< BaseData > data, int port_index)
 Set input data on a specific port by its index.
 
void set_p_graph (Graph *new_p_graph)
 Set the reference to the belonging graph.
 
template<typename T >
void set_value (const std::string &port_label, T new_value)
 Set the value of a port by its label.
 
void update ()
 Update the node, which involves processing its input and output ports.
 

Public Attributes

bool is_dirty = false
 Flag indicating whether the node is marked as dirty (requiring an update).
 

Private Attributes

std::string label
 The label of the node.
 
std::string id
 The ID of the node.
 
std::vector< std::shared_ptr< Port > > ports
 A vector of shared pointers to the node's ports.
 
Graphp_graph = nullptr
 Reference to the graph the node belong to, if any.
 

Detailed Description

Abstract Node class that represents a basic building block in a graph-based system.

Nodes contain ports, which can be connected to other nodes for data flow.

Constructor & Destructor Documentation

◆ Node() [1/3]

gnode::Node::Node ( )
default

Default constructor for Node.

◆ Node() [2/3]

gnode::Node::Node ( std::string  label)
inline

Construct a new Node object with a specific label.

Parameters
labelThe label for the node.

◆ Node() [3/3]

gnode::Node::Node ( std::string  label,
std::string  id 
)
inline

Construct a new Node object with a specific label and identifier.

Parameters
labelThe label for the node.
idThe unique identifier for the node.

This constructor initializes a Node object with both a label and a unique identifier. The label is used to describe or name the node, while the id uniquely distinguishes the node from others in the system (storage in the node itself is generally optional but may be handy).

◆ ~Node()

virtual gnode::Node::~Node ( )
virtualdefault

Virtual destructor for Node.

Member Function Documentation

◆ add_port()

template<typename T , typename... Args>
void gnode::Node::add_port ( PortType  port_type,
const std::string &  port_label,
Args &&...  args 
)
inline

Add a port to the node, specifying whether it is an input or output.

Template Parameters
TThe data type for the port.
ArgsTypes of the arguments passed to the port constructor.
Parameters
port_typeThe type of port (input or output).
port_labelThe label for the port.
argsAdditional arguments passed to the output port constructor if the port type is output.

This function adds a port of type T to the node. If the port type is PortType::IN, it creates an Input port with the given port_label. If the port type is PortType::OUT, it creates an Output port with the given port_label and forwards additional arguments to the Output port constructor.

◆ compute()

virtual void gnode::Node::compute ( )
pure virtual

Pure virtual function that forces derived classes to implement the compute method, which updates the node's state.

◆ get_base_data() [1/2]

std::shared_ptr< BaseData > gnode::Node::get_base_data ( const std::string &  port_label)

Retrieves a shared pointer to the data associated with the port after downcasting.

Parameters
port_labelThe label of the port.
Returns
A shared pointer to the BaseData, or nullptr if not applicable.

◆ get_base_data() [2/2]

std::shared_ptr< BaseData > gnode::Node::get_base_data ( int  port_index)

Retrieves a shared pointer to the data associated with the port after downcasting.

Parameters
port_indexThe index of the port.
Returns
A shared pointer to the BaseData, or nullptr if not applicable.

◆ get_data_type()

std::string gnode::Node::get_data_type ( int  port_index) const

Get the data type of a specific port (input or output).

This function is pure virtual and must be implemented by derived classes.

Parameters
port_indexThe index of the port.
Returns
A string representing the data type of the node (e.g float, int, image...).

◆ get_graph_id()

std::string gnode::Node::get_graph_id ( ) const

Get the id of the graph.

Returns
std::string The label of the node.

◆ get_id()

std::string gnode::Node::get_id ( ) const
inline

Get the ID of the node.

Returns
std::string The ID of the node.

◆ get_label()

std::string gnode::Node::get_label ( ) const
inline

Get the label of the node.

Returns
std::string The label of the node.

◆ get_nports() [1/2]

int gnode::Node::get_nports ( ) const

Get the total number of ports on the node.

Returns
int The number of ports.

◆ get_nports() [2/2]

int gnode::Node::get_nports ( PortType  port_type) const

Get the number of ports of a specific type (input or output).

Parameters
port_typeThe type of port to count (input or output).
Returns
int The number of ports of the specified type.

◆ get_output_data()

std::shared_ptr< BaseData > gnode::Node::get_output_data ( int  port_index) const

Get the output data from a specific port index.

Parameters
port_indexThe index of the output port.
Returns
std::shared_ptr<BaseData> The data from the specified port.

◆ get_p_graph()

Graph * gnode::Node::get_p_graph ( ) const
inline

Get the reference to the belonging graph.

Returns
Graph* Reference.

◆ get_port_index()

int gnode::Node::get_port_index ( const std::string &  port_label) const

Get the index of a port by its label.

Parameters
port_labelThe label of the port.
Returns
int The index of the port.

◆ get_port_label()

std::string gnode::Node::get_port_label ( int  port_index) const

Get the label of a port by its index.

Parameters
port_indexThe index of the port.
Returns
std::string The label of the port.

◆ get_port_type()

PortType gnode::Node::get_port_type ( const std::string &  port_label) const

Get the type of a port (input or output) by its label.

Parameters
port_labelThe label of the port.
Returns
PortType The type of the port.

◆ get_ports()

const std::vector< std::shared_ptr< Port > > & gnode::Node::get_ports ( )
inline

Get the ports.

◆ get_value_ref() [1/2]

template<typename T >
T * gnode::Node::get_value_ref ( const std::string &  port_label) const
inline

Get a reference to the value stored in a port by its label.

Template Parameters
TThe type of the value.
Parameters
port_labelThe label of the port.
Returns
T* A pointer to the value.
Exceptions
std::runtime_errorIf the port with the given label is not found, or if casting the port to the appropriate type fails.

◆ get_value_ref() [2/2]

template<typename T >
T * gnode::Node::get_value_ref ( int  port_index) const
inline

Get a reference to the value stored in a port by its index.

Template Parameters
TThe type of the value.
Parameters
port_indexThe index of the port.
Returns
T* A pointer to the value.
Exceptions
std::out_of_rangeIf the port index is invalid.

◆ get_value_ref_void()

void * gnode::Node::get_value_ref_void ( int  port_index) const
inline

Get a void* reference to the value stored in a port by its index.

Parameters
port_indexThe index of the port.
Returns
A void* pointer to the value.
Exceptions
std::out_of_rangeIf the port index is invalid.

◆ is_port_connected() [1/2]

bool gnode::Node::is_port_connected ( const std::string &  port_label) const

Check if a port is connected by its label.

Parameters
port_labelLabel of the port.
Returns
True if connected, false otherwise.

◆ is_port_connected() [2/2]

bool gnode::Node::is_port_connected ( int  port_index) const

Check if a port is connected by its index.

Parameters
port_indexIndex of the port.
Returns
True if connected, false otherwise.

◆ set_id()

void gnode::Node::set_id ( std::string  new_id)
inline

Set a new identifier for the node.

Parameters
new_idThe new unique identifier to be assigned to the node.

This method updates the node's identifier with the provided new_id. The identifier is used to uniquely distinguish the node from others in the system.

◆ set_input_data()

void gnode::Node::set_input_data ( std::shared_ptr< BaseData data,
int  port_index 
)

Set input data on a specific port by its index.

Parameters
dataThe data to set on the port.
port_indexThe index of the port.

◆ set_p_graph()

void gnode::Node::set_p_graph ( Graph new_p_graph)
inline

Set the reference to the belonging graph.

Parameters
new_p_graphGraph reference.

◆ set_value()

template<typename T >
void gnode::Node::set_value ( const std::string &  port_label,
T  new_value 
)
inline

Set the value of a port by its label.

Template Parameters
TThe type of the value.
Parameters
port_labelThe label of the port.
new_valueThe new value to set on the port.

◆ update()

void gnode::Node::update ( )

Update the node, which involves processing its input and output ports.

Member Data Documentation

◆ id

std::string gnode::Node::id
private

The ID of the node.

◆ is_dirty

bool gnode::Node::is_dirty = false

Flag indicating whether the node is marked as dirty (requiring an update).

◆ label

std::string gnode::Node::label
private

The label of the node.

◆ p_graph

Graph* gnode::Node::p_graph = nullptr
private

Reference to the graph the node belong to, if any.

◆ ports

std::vector<std::shared_ptr<Port> > gnode::Node::ports
private

A vector of shared pointers to the node's ports.


The documentation for this class was generated from the following file: