sys-sage
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
sys_sage::Relation Class Reference

Abstract base class representing a multi-way connection among Components. More...

#include <Relation.hpp>

Inheritance diagram for sys_sage::Relation:
sys_sage::CouplingMap sys_sage::DataPath sys_sage::QuantumGate

Public Member Functions

 Relation (const std::vector< Component * > &components, int _id=0, bool _ordered=true)
 Construct a new Relation object.
 
void SetId (int _id)
 Sets the id of the relationship.
 
int GetId () const
 Gets the id of the relationship.
 
RelationType::type GetType () const
 Get the type of the relation.
 
std::string GetTypeStr () const
 Return a human-readable name of the relation type.
 
bool IsOrdered () const
 Check if this relation treats component order as meaningful.
 
bool ContainsComponent (Component *c) const
 Check whether the given component is part of this relation.
 
ComponentGetComponent (int index) const
 Get the component at a specific position.
 
const std::vector< Component * > & GetComponents () const
 Access the list of components.
 
virtual void Print () const
 Virtual function to print the details of the relationship.
 
void AddComponent (Component *c)
 Add a new component to the relation.
 
int UpdateComponent (int index, Component *_new_component)
 Replace a component at the given index.
 
int UpdateComponent (Component *_old_component, Component *_new_component)
 Replace the first occurrence of a given component.
 
virtual void Delete ()
 Virtual function to delete the relation.
 
 ~Relation ()=default
 Destructor for the Relation class.
 

Public Attributes

std::map< std::string, void * > attrib
 

Protected Attributes

bool ordered
 Whether order in the component list is meaningful.
 
int id
 The id of the relationship.
 
RelationType::type type
 The type of the relationship (see RelationType::type).
 
std::vector< Component * > components
 A vector of components associated with the relationship.
 

Detailed Description

Abstract base class representing a multi-way connection among Components.

A Relation models a relationship among one or more Components, such as a data path, logical gate, or any other connectivity construct. Derived classes define the specific type and semantics.

Key Features:

Clients can use this interface generically or extend it with domain-specific semantics.

Constructor & Destructor Documentation

◆ Relation()

sys_sage::Relation::Relation ( const std::vector< Component * > & components,
int _id = 0,
bool _ordered = true )

Construct a new Relation object.

Parameters
componentsList of pointers to participating Components.
_idOptional unique ID for the relation.
_orderedWhether the order of components carries semantic meaning.

The type of the relation is set to sys_sage::RelationType::Relation.

◆ ~Relation()

sys_sage::Relation::~Relation ( )
default

Destructor for the Relation class.

This is a virtual destructor to ensure proper cleanup of derived classes.

Member Function Documentation

◆ AddComponent()

void sys_sage::Relation::AddComponent ( Component * c)

Add a new component to the relation.

Parameters
cComponent to append to the internal list.

◆ ContainsComponent()

bool sys_sage::Relation::ContainsComponent ( Component * c) const

Check whether the given component is part of this relation.

Parameters
cPointer to the Component to check.
Returns
True if c is found in the components vector.

◆ Delete()

void sys_sage::Relation::Delete ( )
virtual

Virtual function to delete the relation.

Should be overridden in subclasses if custom destruction logic is needed.

Reimplemented in sys_sage::CouplingMap, and sys_sage::DataPath.

◆ GetComponent()

sys_sage::Component * sys_sage::Relation::GetComponent ( int index) const

Get the component at a specific position.

Parameters
indexIndex in the component list.
Returns
Pointer to the Component at that index.

◆ GetComponents()

const std::vector< sys_sage::Component * > & sys_sage::Relation::GetComponents ( ) const

Access the list of components.

Returns
Read-only reference to the component vector.

This avoids copying and prevents direct modification.

◆ GetId()

int sys_sage::Relation::GetId ( ) const

Gets the id of the relationship.

Returns
The current id of the relationship.

◆ GetType()

sys_sage::RelationType::type sys_sage::Relation::GetType ( ) const

Get the type of the relation.

Returns
The current type of the relation (as sys_sage::RelationType::type).

◆ GetTypeStr()

std::string sys_sage::Relation::GetTypeStr ( ) const

Return a human-readable name of the relation type.

Returns
A string like "DataPath" or "QuantumGate".

◆ IsOrdered()

bool sys_sage::Relation::IsOrdered ( ) const

Check if this relation treats component order as meaningful.

Returns
True if the order of components matters.

◆ Print()

void sys_sage::Relation::Print ( ) const
virtual

Virtual function to print the details of the relationship.

Derived classes may implement this function to provide specific printing behavior.

Reimplemented in sys_sage::DataPath, and sys_sage::QuantumGate.

◆ SetId()

void sys_sage::Relation::SetId ( int _id)

Sets the id of the relationship.

Parameters
_idThe id of the relationship to set.

◆ UpdateComponent() [1/2]

int sys_sage::Relation::UpdateComponent ( Component * _old_component,
Component * _new_component )

Replace the first occurrence of a given component.

Uses std::find to locate _old_component and replaces it with _new_component.

Parameters
_old_componentThe component to replace.
_new_componentThe replacement component.
Returns
0 on success, -1 if not found.

◆ UpdateComponent() [2/2]

int sys_sage::Relation::UpdateComponent ( int index,
Component * _new_component )

Replace a component at the given index.

Parameters
indexThe index of the component to replace.
_new_componentNew component to insert.
Returns
0 on success, -1 if the index is invalid.

Member Data Documentation

◆ attrib

std::map<std::string, void*> sys_sage::Relation::attrib

A map for storing arbitrary pieces of information or data.

  • The key denotes the name of the attribute.
  • The value points to the data, stored as a void*.

This data structure is designed to store a wide variety of data types by utilizing pointers to void. Due to its flexibility, it is essential to manage the types and memory allocation/deallocation carefully to avoid issues such as memory leaks or undefined behavior.

Usage:

  1. Adding a new key-value pair:
std::string key = "exampleKey";
int* value = new int(42); // Dynamically allocate memory for the value
attrib[key] = static_cast<void*>(value); // Store the value in the map
std::map< std::string, void * > attrib
Definition Relation.hpp:280
  1. Retrieving data from an existing key:
std::string key = "exampleKey";
if (attrib.find(key) != attrib.end()) {
int* retrievedValue = static_cast<int*>(attrib[key]);
std::cout << "Value: " << *retrievedValue << std::endl;
} else {
std::cout << "Key not found." << std::endl;
}
  1. Checking for the existence of a key:
std::string key = "exampleKey";
if (attrib.find(key) != attrib.end()) {
std::cout << "Key exists." << std::endl;
} else {
std::cout << "Key does not exist." << std::endl;
}
  1. Removing a key-value pair and freeing memory:
std::string key = "exampleKey";
if (attrib.find(key) != attrib.end()) {
int* value = static_cast<int*>(attrib[key]);
delete value; // Free the dynamically allocated memory
attrib.erase(key); // Remove the key-value pair from the map
}
  1. Updating the value for an existing key:
std::string key = "exampleKey";
if (attrib.find(key) != attrib.end()) {
int* oldValue = static_cast<int*>(attrib[key]);
delete oldValue; // Free the old value
int* newValue = new int(100); // Allocate new value
attrib[key] = static_cast<void*>(newValue); // Update the map
}

Note:

  • Proper memory management is crucial when using void* pointers. Always ensure that dynamically allocated memory is freed when no longer needed.
  • Type safety is not enforced, so it is important to cast pointers to the correct type when retrieving values from the map.

◆ components

std::vector<Component*> sys_sage::Relation::components
protected

A vector of components associated with the relationship.

This member variable holds pointers to components that are part of the relationship.

◆ id

int sys_sage::Relation::id
protected

The id of the relationship.

This member variable stores the unique identifier for the relationship.

◆ type

RelationType::type sys_sage::Relation::type
protected

The type of the relationship (see RelationType::type).

This member variable stores the type or category of the relationship.


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