Browse Source

Make Element::GetProperty<T> safe for invalid names, see #56.

Michael Ragazzon 6 years ago
parent
commit
63e2666865
3 changed files with 14 additions and 12 deletions
  1. 3 4
      Include/RmlUi/Core/Element.h
  2. 5 2
      Include/RmlUi/Core/Element.inl
  3. 6 6
      Source/Core/Element.cpp

+ 3 - 4
Include/RmlUi/Core/Element.h

@@ -197,13 +197,12 @@ public:
 	/// @param[in] property The parsed property to set.
 	/// @return True if the property was set successfully, false otherwise.
 	bool SetProperty(PropertyId id, const Property& property);
-	/// Removes a local property override on the element; its value will revert to that defined in
-	/// the style sheet.
+	/// Removes a local property override on the element; its value will revert to that defined in the style sheet.
 	/// @param[in] name The name of the local property definition to remove.
 	void RemoveProperty(const String& name);
 	void RemoveProperty(PropertyId id);
-	/// Returns one of this element's properties. If this element is not defined this property, or a parent cannot
-	/// be found that we can inherit the property from, the default value will be returned.
+	/// Returns one of this element's properties. If the property is not defined for this element and not inherited 
+	/// from an ancestor, the default value will be returned.
 	/// @param[in] name The name of the property to fetch the value for.
 	/// @return The value of this property for this element, or nullptr if no property exists with the given name.
 	const Property* GetProperty(const String& name);		

+ 5 - 2
Include/RmlUi/Core/Element.inl

@@ -30,12 +30,15 @@ namespace Rml {
 namespace Core {
 
 // Returns the values of one of this element's properties.
-// We can assume the property will exist based on the RCSS inheritance.
 template < typename T >
 T Element::GetProperty(const String& name)
 {
 	const Property* property = GetProperty(name);
-	RMLUI_ASSERTMSG(property, "Invalid property name.");
+	if (!property)
+	{
+		Log::Message(Log::LT_WARNING, "Invalid property name %s.", name.c_str());
+		return T{};
+	}
 	return property->Get< T >();
 }
 

+ 6 - 6
Source/Core/Element.cpp

@@ -596,6 +596,12 @@ bool Element::SetProperty(const String& name, const String& value)
 	return true;
 }
 
+// Sets a local property override on the element to a pre-parsed value.
+bool Element::SetProperty(PropertyId id, const Property& property)
+{
+	return style->SetProperty(id, property);
+}
+
 // Removes a local property override on the element.
 void Element::RemoveProperty(const String& name)
 {
@@ -608,12 +614,6 @@ void Element::RemoveProperty(PropertyId id)
 	style->RemoveProperty(id);
 }
 
-// Sets a local property override on the element to a pre-parsed value.
-bool Element::SetProperty(PropertyId id, const Property& property)
-{
-	return style->SetProperty(id, property);
-}
-
 // Returns one of this element's properties.
 const Property* Element::GetProperty(const String& name)
 {