22#include <unordered_map>
25namespace shamrock::solvergraph {
58 std::unordered_map<std::string, std::shared_ptr<INode>> nodes;
61 std::unordered_map<std::string, std::shared_ptr<IEdge>> edges;
76 const std::string &name, std::shared_ptr<INode> node) {
77 const auto [it, inserted] = nodes.try_emplace(name, std::move(node));
80 shambase::format(
"Node already exists: {}", name));
93 const std::string &name, std::shared_ptr<IEdge> edge) {
94 const auto [it, inserted] = edges.try_emplace(name, std::move(edge));
97 shambase::format(
"Edge already exists: {}", name));
110 auto it = nodes.find(name);
111 if (it == nodes.end()) {
113 shambase::format(
"Node does not exist: {}", name));
120 auto it = nodes.find(name);
121 if (it == nodes.end()) {
123 shambase::format(
"Node does not exist: {}", name));
136 auto it = edges.find(name);
137 if (it == edges.end()) {
139 shambase::format(
"Edge does not exist: {}", name));
146 auto it = edges.find(name);
147 if (it == edges.end()) {
149 shambase::format(
"Edge does not exist: {}", name));
207 inline std::shared_ptr<T>
register_node(
const std::string &name, T &&node) {
208 static_assert(std::is_base_of<INode, T>::value,
"T must derive from INode");
210 return get_node_ptr<T>(name);
226 inline std::shared_ptr<T>
register_edge(
const std::string &name, T &&edge) {
227 static_assert(std::is_base_of<IEdge, T>::value,
"T must derive from IEdge");
229 return get_edge_ptr<T>(name);
248 shambase::format(
"Node exists but is not from the requested type: {}", name));
255 inline std::shared_ptr<T>
get_node_ptr(
const std::string &name)
const {
259 shambase::format(
"Node exists but is not from the requested type: {}", name));
280 shambase::format(
"Edge exists but is not from the requested type: {}", name));
287 inline std::shared_ptr<T>
get_edge_ptr(
const std::string &name)
const {
291 shambase::format(
"Edge exists but is not from the requested type: {}", name));
Inode is node between data edges, takes multiple inputs, multiple outputs.
A graph container for managing solver nodes and edges with type-safe access.
std::shared_ptr< INode > & get_node_ptr_base(const std::string &name)
Retrieve a node by name as a shared pointer to the base interface.
T & get_edge_ref(const std::string &name)
Get a typed reference to an edge by name.
const std::shared_ptr< INode > & get_node_ptr_base(const std::string &name) const
const variant
std::shared_ptr< T > get_edge_ptr(const std::string &name)
Get a typed shared pointer to an edge by name.
IEdge & get_edge_ref_base(const std::string &name)
Get a reference to an edge by name through the base interface.
std::shared_ptr< T > get_edge_ptr(const std::string &name) const
const variant
std::shared_ptr< T > register_edge(const std::string &name, T &&edge)
Register an edge with automatic type deduction and shared pointer creation.
const std::shared_ptr< IEdge > & get_edge_ptr_base(const std::string &name) const
const variant
std::shared_ptr< T > get_node_ptr(const std::string &name)
Get a typed shared pointer to a node by name.
std::shared_ptr< INode > register_node_ptr_base(const std::string &name, std::shared_ptr< INode > node)
Register a node with the graph using a shared pointer.
const T & get_edge_ref(const std::string &name) const
const variant
std::shared_ptr< T > get_node_ptr(const std::string &name) const
const variant
const INode & get_node_ref_base(const std::string &name) const
const variant
std::shared_ptr< IEdge > & get_edge_ptr_base(const std::string &name)
Retrieve an edge by name as a shared pointer to the base interface.
std::shared_ptr< T > register_node(const std::string &name, T &&node)
Register a node with automatic type deduction and shared pointer creation.
const IEdge & get_edge_ref_base(const std::string &name) const
const variant
INode & get_node_ref_base(const std::string &name)
Get a reference to a node by name through the base interface.
const T & get_node_ref(const std::string &name) const
const variant
T & get_node_ref(const std::string &name)
Get a typed reference to a node by name.
std::shared_ptr< IEdge > register_edge_ptr_base(const std::string &name, std::shared_ptr< IEdge > edge)
Register an edge with the graph using a shared pointer.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...