add assert in XPort get

This commit is contained in:
yaozhicheng 2025-06-27 09:08:50 +08:00 committed by Zhicheng
parent 208f41deb6
commit fe4e0628f1
3 changed files with 8 additions and 0 deletions

View File

@ -26,6 +26,7 @@ public:
XPort &NewSubPort(std::string subprefix);
XPort &SelectPins(std::vector<std::string> pins);
XPort &SelectPins(std::initializer_list<std::string> pins);
bool Has(std::string key, bool raw_key = false);
xspcomm::XData &Get(std::string key, bool raw_key = false);
XPort &FlipIOType();
XPort &AsBiIO();

View File

@ -95,6 +95,11 @@ xspcomm::XData &XPort::operator[](std::string key)
return this->Get(key);
}
bool XPort::Has(std::string key, bool raw_key)
{
return this->port_list.count(raw_key ? key : this->asKey(key)) > 0;
}
xspcomm::XData &XPort::Get(std::string key, bool raw_key)
{
Assert(this->port_list.count(raw_key ? key : this->asKey(key)) > 0,

View File

@ -138,6 +138,8 @@ def XPort__str__(self: XPort):
return f"XPort(prefix={self.prefix}, size={self.PortCount()})"
def XPort__getitem__(self: XPort, key):
assert isinstance(key, str), f"Pin key ({key}) must be str"
assert self.Has(key), f"Pin '{key}' not found in XPort"
return self.Get(key)
def XPort__setitem__(self: XPort, key, value):