The Sparta Modeling Framework
Loading...
Searching...
No Matches
PortSet.hpp
Go to the documentation of this file.
1// <Port> -*- C++ -*-
2
3
10#pragma once
11#include <set>
12#include <list>
13#include <unordered_map>
14
15#include "sparta/ports/Port.hpp"
16
17namespace sparta
18{
19
28 class PortSet : public TreeNode
29 {
30 public:
31
33 typedef std::unordered_map<std::string, Port *> RegisteredPortMap;
34
40 PortSet(TreeNode * parent, const std::string & desc = "Port Set") :
41 TreeNode(parent, "ports", desc)
42 {}
43
49 Port * getPort(const std::string & named_port) const {
50 for(uint32_t i = 0; i < Port::Direction::N_DIRECTIONS; ++i) {
51 auto found_port = registered_ports_[i].find(named_port);
52 if(found_port != registered_ports_[i].end()) {
53 return found_port->second;
54 }
55 }
56 //Throw an exception if we could not find the port.
57 throw SpartaException("The port with the name : " + named_port + " could not be found");
58 return nullptr;
59 }
60
66 // Port * getDataInPort(const std::string & named_port) {
67 // auto found_port = registered_ports_[Port::IN].find(named_port);
68 // if(found_port != registered_ports_[Port::IN].end()) {
69 // // Right name, wrong direction
70 // return found_port->second;
71 // }
72 // //Throw an exception if we could not find the port.
73 // throw SpartaException("The port with the name : " + named_port + " could not be found");
74 // return nullptr;
75 // }
76
77 // /**
78 // * \brief Get a DataOutPort by the given name (convenience function)
79 // * \param named_port The named DataOutPort to retrieve
80 // * \return Pointer to the port or nullptr if not found
81 // */
82 // Port * getDataOutPort(const std::string & named_port) {
83 // auto found_port = registered_ports_[Port::OUT].find(named_port);
84 // if(found_port != registered_ports_[Port::OUT].end()) {
85 // // Right name, wrong direction
86 // return found_port->second;
87 // }
88 // //Throw an exception if we could not find the port.
89 // throw SpartaException("The port with the name : " + named_port + " could not be found");
90 // return nullptr;
91 // }
92
93
95 PortSet(const PortSet &) = delete;
96
98 PortSet & operator=(const PortSet &) = delete;
99
106 return registered_ports_[direction];
107 }
108
109 private:
111 RegisteredPortMap registered_ports_[static_cast<uint32_t>(Port::Direction::N_DIRECTIONS)];
112
124 virtual void onAddingChild_(TreeNode* child) override {
125 if(isFinalized()){
126 throw SpartaException("Cannot add a child Port once a PortSet is finalized. "
127 "Error with: ")
128 << getLocation();
129 }
130
131 Port* port = dynamic_cast<Port*>(child);
132 if(nullptr != port){
133 sparta_assert(registered_ports_[port->getDirection()].count(port->getName()) == 0,
134 "ERROR: Port '" << port->getName() << "' already registered");
135 registered_ports_[port->getDirection()][port->getName()] = port;
136 return;
137 }
138
139 throw SpartaException("Cannot add TreeNode child ")
140 << child->getName() << " to PortSet " << getLocation()
141 << " because the child is not a Port or derivative";
142 }
143
144 };
145}
File that defines the Port base class.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
virtual bool isFinalized() const
Is this node (and thus the entire tree above it) "finalized".
A TreeNode that represents a set of ports used by a Resource.
Definition PortSet.hpp:29
PortSet(TreeNode *parent, const std::string &desc="Port Set")
Construct a PortSet with a given parent. The parent can be nullptr.
Definition PortSet.hpp:40
Port * getPort(const std::string &named_port) const
Get a port by the given name.
Definition PortSet.hpp:49
RegisteredPortMap & getPorts(Port::Direction direction)
Get the ports in this PortSet for the given direction.
Definition PortSet.hpp:105
PortSet(const PortSet &)=delete
Get a DataInPort by the given name (convenience function)
PortSet & operator=(const PortSet &)=delete
Cannot assign PortSets.
std::unordered_map< std::string, Port * > RegisteredPortMap
Convenience typedef.
Definition PortSet.hpp:33
The port interface used to bind port types together and defines a port behavior.
Definition Port.hpp:59
Direction
The direction of this port.
Definition Port.hpp:68
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
std::string getLocation() const override final
const std::string & getName() const override
Gets the name of this node.
Macros for handling exponential backoff.