diff --git a/include/sta/Property.hh b/include/sta/Property.hh index be06b272..c8bf3ab8 100644 --- a/include/sta/Property.hh +++ b/include/sta/Property.hh @@ -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_; } diff --git a/search/Property.cc b/search/Property.cc index d51b5303..1477856f 100644 --- a/search/Property.cc +++ b/search/Property.cc @@ -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