Browse Source

Make RegisterParser use the lifetime requirement consistent with the rest of the library; remove the Release method from PropertyParser.

Michael Ragazzon 6 years ago
parent
commit
e612bceeb3

+ 1 - 6
Include/RmlUi/Core/PropertyParser.h

@@ -47,9 +47,7 @@ typedef UnorderedMap< String, int > ParameterMap;
 class RMLUICORE_API PropertyParser
 {
 public:
-	virtual ~PropertyParser()
-	{
-	}
+	virtual ~PropertyParser() {}
 
 	/// Called to parse a RCSS declaration.
 	/// @param[out] property The property to set the parsed value on.
@@ -57,9 +55,6 @@ public:
 	/// @param[in] parameters The list of parameters defined for this property.
 	/// @return True if the value was parsed successfully, false otherwise.
 	virtual bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const = 0;
-
-	/// Called when the parser is released. This should free all dynamic memory used by the parser.
-	virtual void Release() = 0;
 };
 
 }

+ 1 - 1
Include/RmlUi/Core/StyleSheet.h

@@ -133,7 +133,7 @@ private:
 	// Map of all styled nodes, that is, they have one or more properties.
 	NodeIndex styled_node_index;
 
-	typedef UnorderedMap< size_t, SharedPtr<ElementDefinition> > ElementDefinitionCache;
+	using ElementDefinitionCache = UnorderedMap< size_t, SharedPtr<ElementDefinition> >;
 	// Index of node sets to element definitions.
 	mutable ElementDefinitionCache node_cache;
 };

+ 5 - 1
Include/RmlUi/Core/StyleSheetSpecification.h

@@ -37,6 +37,7 @@ namespace Rml {
 namespace Core {
 
 class PropertyParser;
+struct DefaultStyleSheetParsers;
 
 /**
 	@author Peter Curry
@@ -53,8 +54,9 @@ public:
 
 	/// Registers a parser for use in property definitions.
 	/// @param[in] parser_name The name to register the new parser under.
-	/// @param[in] parser The parser to register. This parser will be released by the specification.
+	/// @param[in] parser A non-owning pointer to the parser to register.
 	/// @return True if the parser was registered successfully, false otherwise.
+	/// @lifetime The parser must be kept alive until after the call to Core::Shutdown.
 	static bool RegisterParser(const String& parser_name, PropertyParser* parser);
 	/// Returns the parser registered with a specific name.
 	/// @param[in] parser_name The name of the desired parser.
@@ -130,6 +132,8 @@ private:
 
 	// The properties defined in the style sheet specification.
 	PropertySpecification properties;
+
+	UniquePtr<DefaultStyleSheetParsers> default_parsers;
 };
 
 }

+ 0 - 5
Source/Core/PropertyParserAnimation.cpp

@@ -373,10 +373,5 @@ bool PropertyParserAnimation::ParseValue(Property & property, const String & val
 	return result;
 }
 
-void PropertyParserAnimation::Release()
-{
-	delete this;
-}
-
 }
 }

+ 0 - 3
Source/Core/PropertyParserAnimation.h

@@ -58,9 +58,6 @@ public:
 	/// @param[in] parameters The parameters defined for this property.
 	/// @return True if the value was validated successfully, false otherwise.
 	bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const override;
-
-	// Destroys the parser.
-	void Release() override;
 };
 
 

+ 0 - 6
Source/Core/PropertyParserColour.cpp

@@ -170,11 +170,5 @@ bool PropertyParserColour::ParseValue(Property& property, const String& value, c
 	return true;
 }
 
-// Destroys the parser.
-void PropertyParserColour::Release()
-{
-	delete this;
-}
-
 }
 }

+ 0 - 3
Source/Core/PropertyParserColour.h

@@ -54,9 +54,6 @@ public:
 	/// @return True if the value was parsed successfully, false otherwise.
 	bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const override;
 
-	/// Destroys the parser.
-	void Release() override;
-
 private:
 	typedef UnorderedMap< String, Colourb> ColourMap;
 	ColourMap html_colours;

+ 0 - 6
Source/Core/PropertyParserKeyword.cpp

@@ -53,11 +53,5 @@ bool PropertyParserKeyword::ParseValue(Property& property, const String& value,
 	return true;
 }
 
-// Destroys the parser.
-void PropertyParserKeyword::Release()
-{
-	delete this;
-}
-
 }
 }

+ 0 - 3
Source/Core/PropertyParserKeyword.h

@@ -52,9 +52,6 @@ public:
 	/// @param[in] parameters The parameters defined for this property.
 	/// @return True if the value was validated successfully, false otherwise.
 	bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const override;
-
-	// Destroys the parser.
-	void Release() override;
 };
 
 }

+ 0 - 6
Source/Core/PropertyParserNumber.cpp

@@ -140,11 +140,5 @@ bool PropertyParserNumber::ParseValue(Property& property, const String& value, c
 	return false;
 }
 
-// Destroys the parser.
-void PropertyParserNumber::Release()
-{
-	delete this;
-}
-
 }
 }

+ 0 - 3
Source/Core/PropertyParserNumber.h

@@ -53,9 +53,6 @@ public:
 	/// @return True if the value was validated successfully, false otherwise.
 	bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const override;
 
-	// Destroys the parser.
-	void Release() override;
-
 private:
 	// Stores a bit mask of allowed units.
 	int units;

+ 0 - 6
Source/Core/PropertyParserString.cpp

@@ -51,11 +51,5 @@ bool PropertyParserString::ParseValue(Property& property, const String& value, c
 	return true;
 }
 
-// Destroys the parser.
-void PropertyParserString::Release()
-{
-	delete this;
-}
-
 }
 }

+ 0 - 3
Source/Core/PropertyParserString.h

@@ -52,9 +52,6 @@ public:
 	/// @param[in] parameters The parameters defined for this property; not used for this parser.
 	/// @return True if the value was validated successfully, false otherwise.
 	bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const override;
-
-	// Destroys the parser.
-	void Release() override;
 };
 
 }

+ 0 - 6
Source/Core/PropertyParserTransform.cpp

@@ -182,12 +182,6 @@ bool PropertyParserTransform::ParseValue(Property& property, const String& value
 	return true;
 }
 
-// Destroys the parser.
-void PropertyParserTransform::Release()
-{
-	delete this;
-}
-
 // Scan a string for a parameterized keyword with a certain number of numeric arguments.
 int PropertyParserTransform::Scan(const char* str, const char* keyword, const PropertyParser** parsers, Transforms::NumericValue* args, int nargs) const
 {

+ 0 - 3
Source/Core/PropertyParserTransform.h

@@ -56,9 +56,6 @@ public:
 	/// @return True if the value was validated successfully, false otherwise.
 	bool ParseValue(Property& property, const String& value, const ParameterMap& parameters) const override;
 
-	// Destroys the parser.
-	void Release() override;
-
 private:
 	/// Scan a string for a parameterized keyword with a certain number of numeric arguments.
 	/// @param[in] str The string to search for the parameterized keyword

+ 32 - 15
Source/Core/StyleSheetSpecification.cpp

@@ -44,12 +44,29 @@ namespace Core {
 
 static StyleSheetSpecification* instance = nullptr;
 
+
+struct DefaultStyleSheetParsers {
+	PropertyParserNumber number = PropertyParserNumber(Property::NUMBER);
+	PropertyParserNumber length = PropertyParserNumber(Property::LENGTH, Property::PX);
+	PropertyParserNumber length_percent = PropertyParserNumber(Property::LENGTH_PERCENT, Property::PX);
+	PropertyParserNumber number_length_percent = PropertyParserNumber(Property::NUMBER_LENGTH_PERCENT, Property::PX);
+	PropertyParserNumber angle = PropertyParserNumber(Property::ANGLE, Property::RAD);
+	PropertyParserKeyword keyword = PropertyParserKeyword();
+	PropertyParserString string = PropertyParserString();
+	PropertyParserAnimation animation = PropertyParserAnimation(PropertyParserAnimation::ANIMATION_PARSER);
+	PropertyParserAnimation transition = PropertyParserAnimation(PropertyParserAnimation::TRANSITION_PARSER);
+	PropertyParserColour color = PropertyParserColour();
+	PropertyParserTransform transform = PropertyParserTransform();
+};
+
 StyleSheetSpecification::StyleSheetSpecification() : 
 	// Reserve space for all defined ids and some more for custom properties
 	properties(2 * (size_t)PropertyId::NumDefinedIds, 2 * (size_t)ShorthandId::NumDefinedIds)
 {
 	RMLUI_ASSERT(instance == nullptr);
 	instance = this;
+
+	default_parsers.reset(new DefaultStyleSheetParsers);
 }
 
 StyleSheetSpecification::~StyleSheetSpecification()
@@ -85,9 +102,6 @@ void StyleSheetSpecification::Shutdown()
 {
 	if (instance != nullptr)
 	{
-		for (ParserMap::iterator iterator = instance->parsers.begin(); iterator != instance->parsers.end(); ++iterator)
-			(*iterator).second->Release();
-
 		delete instance;
 	}
 }
@@ -97,7 +111,10 @@ bool StyleSheetSpecification::RegisterParser(const String& parser_name, Property
 {
 	ParserMap::iterator iterator = instance->parsers.find(parser_name);
 	if (iterator != instance->parsers.end())
-		(*iterator).second->Release();
+	{
+		Log::Message(Log::LT_WARNING, "Parser with name %s already exists!", parser_name.c_str());
+		return false;
+	}
 
 	instance->parsers[parser_name] = parser;
 	return true;
@@ -221,17 +238,17 @@ const PropertySpecification& StyleSheetSpecification::GetPropertySpecification()
 // Registers RmlUi's default parsers.
 void StyleSheetSpecification::RegisterDefaultParsers()
 {
-	RegisterParser("number", new PropertyParserNumber(Property::NUMBER));
-	RegisterParser("length", new PropertyParserNumber(Property::LENGTH, Property::PX));
-	RegisterParser("length_percent", new PropertyParserNumber(Property::LENGTH_PERCENT, Property::PX));
-	RegisterParser("number_length_percent", new PropertyParserNumber(Property::NUMBER_LENGTH_PERCENT, Property::PX));
-	RegisterParser("angle", new PropertyParserNumber(Property::ANGLE, Property::RAD));
-	RegisterParser("keyword", new PropertyParserKeyword());
-	RegisterParser("string", new PropertyParserString());
-	RegisterParser(ANIMATION, new PropertyParserAnimation(PropertyParserAnimation::ANIMATION_PARSER));
-	RegisterParser(TRANSITION, new PropertyParserAnimation(PropertyParserAnimation::TRANSITION_PARSER));
-	RegisterParser(COLOR, new PropertyParserColour());
-	RegisterParser(TRANSFORM, new PropertyParserTransform());
+	RegisterParser("number", &default_parsers->number);
+	RegisterParser("length", &default_parsers->length);
+	RegisterParser("length_percent", &default_parsers->length_percent);
+	RegisterParser("number_length_percent", &default_parsers->number_length_percent);
+	RegisterParser("angle", &default_parsers->angle);
+	RegisterParser("keyword", &default_parsers->keyword);
+	RegisterParser("string", &default_parsers->string);
+	RegisterParser("animation", &default_parsers->animation);
+	RegisterParser("transition", &default_parsers->transition);
+	RegisterParser("color", &default_parsers->color);
+	RegisterParser("transform", &default_parsers->transform);
 }