 |
Cerata
A library to generate structural hardware designs
|
22 #include <unordered_map>
24 #include "cerata/node.h"
25 #include "cerata/array.h"
26 #include "cerata/pool.h"
55 virtual Graph &
Add(
const std::shared_ptr<Object> &
object);
57 virtual Graph &
Add(
const std::vector<std::shared_ptr<Object>> &
objects);
64 std::vector<T *> result;
66 auto co = std::dynamic_pointer_cast<T>(o);
68 result.push_back(co.get());
78 if (o->name() ==
name) {
79 auto result =
dynamic_cast<T *
>(o.get());
80 if (result ==
nullptr) {
81 CERATA_LOG(FATAL,
"Object with name " +
name +
" is not of type " + ::cerata::ToString<T>());
86 CERATA_LOG(FATAL,
"Object with name " +
name +
" does not exist on graph " + this->
name()
91 std::optional<Node *>
FindNode(
const std::string &node_name)
const;
98 std::vector<Node *>
GetNodesOfTypes(std::initializer_list<Node::NodeID> ids)
const;
104 std::vector<Node *>
GetNodes()
const {
return GetAll<Node>(); }
126 Parameter *
par(
const std::shared_ptr<Parameter> ¶m)
const;
129 std::unordered_map<std::string, std::string>
meta()
const {
return meta_; }
133 bool Has(
const std::string &
name);
136 Graph &
SetMeta(
const std::string &key, std::string value);
150 std::unordered_map<std::string, std::string>
meta_;
164 Graph &
Add(
const std::shared_ptr<Object> &
object)
override;
166 Graph &
Add(
const std::vector<std::shared_ptr<Object>> &
objects)
override;
219 std::shared_ptr<Component>
component(std::string name,
220 const std::vector<std::shared_ptr<Object>> &nodes,
223 std::shared_ptr<Component>
component(std::string name,
234 Graph &
Add(
const std::shared_ptr<Object> &obj)
override;
bool was_instantiated
Whether this component was instantiated.
virtual std::vector< const Component * > GetAllInstanceComponents() const
Returns all unique Components that are referred to by child Instances of this graph.
Graph * parent_
The parent of this instance.
virtual Graph & Add(const std::shared_ptr< Object > &object)
Add an object to the component.
SignalArray * sig_arr(const std::string &name) const
Shorthand to Get<SignalArray>(...)
std::string ToString() const
Return a human-readable representation.
std::vector< T * > GetAll() const
Get all objects of a specific type.
An array of signal nodes.
Component(std::string name)
Construct an empty Component.
Graph & Add(const std::shared_ptr< Object > &object) override
Add an object to the component.
bool IsInstance() const
Return true if this graph is an instance, false otherwise.
Graph * parent() const
Return the parent graph.
Instance * Instantiate(Component *comp, const std::string &name="")
Add an Instance of another Component to this component.
Component * component() const
Return the component this is an instance of.
A graph representing a hardware structure.
std::unordered_map< std::string, std::string > meta_
KV storage for metadata of tools or specific backend implementations.
std::vector< T * > ToRawPointers(const std::vector< std::shared_ptr< T >> &list)
Convert a list of shared pointers to raw pointers.
Graph & Add(const std::shared_ptr< Object > &obj) override
Add a node to the component, throwing an exception if the node is a signal.
Graph(std::string name, ID id)
Construct a new graph.
std::unordered_map< std::string, std::string > meta() const
Return a copy of the metadata.
Contains every Cerata class, function, etc...
size_t CountArrays(Node::NodeID id) const
Count nodes of a specific array type.
std::vector< std::shared_ptr< Object > > objects_
Graph objects.
static std::unique_ptr< Instance > Make(Component *component, const std::string &name, Component *parent)
Create an instance.
bool IsComponent() const
Return true if this graph is a component, false otherwise.
NodeMap * comp_to_inst_map()
Return the component node to instance node mapping.
std::vector< Node * > GetNodesOfTypes(std::initializer_list< Node::NodeID > ids) const
Obtain all nodes which ids are in a list of Node::IDs.
std::vector< Node * > GetImplicitNodes() const
Return all graph nodes that do not explicitly belong to the graph.
NodeMap comp_to_inst
Mapping from component port and parameter nodes to instantiated nodes.
std::unordered_map< const Node *, Node * > NodeMap
A mapping from one object to another object, used in e.g. type generic rebinding.
std::vector< Instance * > children() const
Returns all Instance graphs from this Component.
Component * component_
The component that this instance instantiates.
T * Get(const std::string &name) const
Get one object of a specific type.
ID id() const
Return the graph type ID.
std::string name() const
Return the name of the object.
NodeID
Node type IDs with different properties.
PortArray * prt_arr(const std::string &name) const
Shorthand to Get<PortArray>(...)
Graph & Remove(Object *object) override
Remove an object from the component.
std::shared_ptr< Component > component(std::string name, const std::vector< std::shared_ptr< Object >> &objects, ComponentPool *component_pool)
Construct a Component with initial nodes.
Port * prt(const std::string &name) const
Shorthand to Get<Port>(...)
Parameter * par(const std::string &name) const
Shorthand to Get<Parameter>(...)
Graph & SetParent(Graph *parent)
Set the parent.
Graph & SetMeta(const std::string &key, std::string value)
Set metadata.
std::vector< Node * > GetNodes() const
Get all nodes.
ComponentPool * default_component_pool()
Return a global default component pool.
A Cerata Object on a graph.
std::vector< std::unique_ptr< Instance > > children_
Instances.
Convenience structure for anything that is named. Names are case-sensitive.
Signal * sig(const std::string &name) const
Shorthand to Get<Signal>(...)
std::vector< NodeArray * > GetArraysOfType(Node::NodeID id) const
Get all arrays of a specific type.
std::optional< Node * > FindNode(const std::string &node_name) const
Find a node with a specific name.
std::string ToStringAllOjects() const
Return a comma separated list of object names.
@ COMPONENT
A component graph.
bool HasChild(const std::string &name) const
Return true if child graph exists on instance.
std::vector< Node * > GetNodesOfType(Node::NodeID id) const
Get all nodes of a specific type.
Component & AddChild(std::unique_ptr< Instance > child)
Add and take ownership of an Instance graph.
bool Has(const std::string &name)
Return true if object with name already exists on graph.
std::vector< Object * > objects() const
Get all objects.
@ INSTANCE
An instance graph.
Node * GetNode(const std::string &node_name) const
Get a Node of a specific type with a specific name.
size_t CountNodes(Node::NodeID id) const
Count nodes of a specific node type.
virtual Graph & Remove(Object *obj)
Remove an object from the component.
ID
Graph type ID for convenient run-time type checking.
Instance(Component *comp, std::string name, Component *parent)
Construct an Instance of a Component, copying over all its ports and parameters.
ID id_
Graph type id for convenience.
NodeMap * inst_to_comp_map()
Return the component node to instance node mapping.
A port is a terminator node on a graph.
NodeMap inst_to_comp
Mapping for instance nodes that have been connected.
friend Component
Only a Component should be able to make instances.
Node * operator()(const std::string &node_name) const
Obtain a node by name.