Property accessors check type

Signed-off-by: James Cherry <cherry@parallaxsw.com>
This commit is contained in:
James Cherry 2024-07-29 22:56:46 -07:00
parent df346d3f51
commit b9b1d22b19
2 changed files with 56 additions and 3 deletions

View File

@ -80,9 +80,9 @@ public:
const Unit *unit() const { return unit_; }
const char *asString(const Network *network) const;
const char *stringValue() const { return string_; }
float floatValue() const { return float_; }
bool boolValue() const { return bool_; }
const char *stringValue() const; // valid for type string
float floatValue() const; // valid for type float
bool boolValue() const; // valid for type bool
const LibertyLibrary *libertyLibrary() const { return liberty_library_; }
const LibertyCell *libertyCell() const { return liberty_cell_; }
const LibertyPort *libertyPort() const { return liberty_port_; }

View File

@ -126,6 +126,35 @@ PropertyUnknown::what() const noexcept
////////////////////////////////////////////////////////////////
class PropertyTypeWrong : public Exception
{
public:
PropertyTypeWrong(const char *accessor,
const char *type);
virtual ~PropertyTypeWrong() {}
virtual const char *what() const noexcept;
private:
const char *accessor_;
const char *type_;
};
PropertyTypeWrong::PropertyTypeWrong(const char *accessor,
const char *type) :
Exception(),
accessor_(accessor),
type_(type)
{
}
const char *
PropertyTypeWrong::what() const noexcept
{
return stringPrint("property accessor %s is only valid for %s properties.",
accessor_, type_);
}
////////////////////////////////////////////////////////////////
PropertyValue::PropertyValue() :
type_(type_none),
unit_(nullptr)
@ -620,6 +649,30 @@ PropertyValue::asString(const Network *network) const
return nullptr;
}
const char *
PropertyValue::stringValue() const
{
if (type_ != Type::type_string)
throw PropertyTypeWrong("stringValue", "string");
return string_;
}
float
PropertyValue::floatValue() const
{
if (type_ != Type::type_float)
throw PropertyTypeWrong("floatValue", "float");
return float_;
}
bool
PropertyValue::boolValue() const
{
if (type_ != Type::type_bool)
throw PropertyTypeWrong("boolValue", "boolt");
return bool_;
}
////////////////////////////////////////////////////////////////
PropertyValue