Browse Source

Remove reference countable from element definition

Michael Ragazzon 6 năm trước cách đây
mục cha
commit
6833c4d5bc

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

@@ -105,7 +105,7 @@ public:
 
 	/// Returns the compiled element definition for a given element hierarchy. A reference count will be added for the
 	/// caller, so another should not be added. The definition should be released by removing the reference count.
-	ElementDefinition* GetElementDefinition(const Element* element) const;
+	std::shared_ptr<ElementDefinition> GetElementDefinition(const Element* element) const;
 
 protected:
 	/// Destroys the style sheet.
@@ -113,7 +113,7 @@ protected:
 
 private:
 	// Root level node, attributes from special nodes like "body" get added to this node
-	StyleSheetNode* root;
+	std::unique_ptr<StyleSheetNode> root;
 
 	// The maximum specificity offset used in this style sheet to distinguish between properties in
 	// similarly-specific rules, but declared on different lines. When style sheets are merged, the
@@ -136,7 +136,7 @@ private:
 	// Map of every node, even empty, un-styled, nodes.
 	NodeIndex complete_node_index;
 
-	typedef UnorderedMap< size_t, ElementDefinition* > ElementDefinitionCache;
+	typedef UnorderedMap< size_t, std::shared_ptr<ElementDefinition> > ElementDefinitionCache;
 	// Index of node sets to element definitions.
 	mutable ElementDefinitionCache node_cache;
 };

+ 0 - 11
Source/Core/ElementDefinition.cpp

@@ -40,10 +40,6 @@ ElementDefinition::ElementDefinition(const std::vector< const StyleSheetNode* >&
 		properties.Merge(style_sheet_nodes[i]->GetProperties());
 }
 
-ElementDefinition::~ElementDefinition()
-{
-}
-
 // Returns a specific property from the element definition's base properties.
 const Property* ElementDefinition::GetProperty(PropertyId id) const
 {
@@ -57,12 +53,5 @@ void ElementDefinition::GetDefinedProperties(PropertyNameList& property_names) c
 		property_names.insert((*i).first);
 }
 
-// Destroys the definition.
-void ElementDefinition::OnReferenceDeactivate()
-{
-	delete this;
-}
-
-
 }
 }

+ 1 - 6
Source/Core/ElementDefinition.h

@@ -44,11 +44,10 @@ class ElementDefinitionIterator;
 	@author Peter Curry
  */
 
-class ElementDefinition : public ReferenceCountable
+class ElementDefinition : public NonCopyMoveable
 {
 public:
 	ElementDefinition(const std::vector< const StyleSheetNode* >& style_sheet_nodes);
-	virtual ~ElementDefinition();
 
 	/// Returns a specific property from the element definition's base properties.
 	/// @param[in] name The name of the property to return.
@@ -64,10 +63,6 @@ public:
 
 	const PropertyDictionary& GetProperties() const { return properties; }
 
-protected:
-	/// Destroys the definition.
-	void OnReferenceDeactivate();
-
 private:
 	// The attributes for the default state of the element, with no pseudo-classes.
 	PropertyDictionary properties;

+ 5 - 21
Source/Core/ElementStyle.cpp

@@ -59,16 +59,9 @@ ElementStyle::ElementStyle(Element* _element) : dirty_properties(true)
 	definition_dirty = true;
 }
 
-ElementStyle::~ElementStyle()
-{
-	if (definition)
-		definition->RemoveReference();
-}
-
-
 const ElementDefinition* ElementStyle::GetDefinition() const
 {
-	return definition;
+	return definition.get();
 }
 
 // Returns one of this element's properties.
@@ -177,7 +170,7 @@ void ElementStyle::UpdateDefinition()
 	{
 		definition_dirty = false;
 
-		ElementDefinition* new_definition = nullptr;
+		std::shared_ptr<ElementDefinition> new_definition;
 		
 		if (const StyleSheet * style_sheet = GetStyleSheet())
 		{
@@ -202,21 +195,12 @@ void ElementStyle::UpdateDefinition()
 			if (new_definition)
 				new_definition->GetDefinedProperties(properties);
 
-			TransitionPropertyChanges(element, properties, inline_properties, definition, new_definition);
-
-			if (definition)
-				definition->RemoveReference();
+			TransitionPropertyChanges(element, properties, inline_properties, definition.get(), new_definition.get());
 
 			definition = new_definition;
 			
 			DirtyProperties(properties);
 		}
-		else if (new_definition)
-		{
-			// We got the same definition
-			RMLUI_ASSERT(new_definition == definition);
-			new_definition->RemoveReference();
-		}
 
 		// Even if the definition was not changed, the child definitions may have changed as a result of anything that
 		// could change the definition of this element, such as a new pseudo class.
@@ -337,13 +321,13 @@ void ElementStyle::RemoveProperty(PropertyId id)
 // Returns one of this element's properties.
 const Property* ElementStyle::GetProperty(PropertyId id) const
 {
-	return GetProperty(id, element, inline_properties, definition);
+	return GetProperty(id, element, inline_properties, definition.get());
 }
 
 // Returns one of this element's properties.
 const Property* ElementStyle::GetLocalProperty(PropertyId id) const
 {
-	return GetLocalProperty(id, inline_properties, definition);
+	return GetLocalProperty(id, inline_properties, definition.get());
 }
 
 const PropertyMap& ElementStyle::GetLocalStyleProperties() const

+ 1 - 2
Source/Core/ElementStyle.h

@@ -49,7 +49,6 @@ public:
 	/// Constructor
 	/// @param[in] element The element this structure belongs to.
 	ElementStyle(Element* element);
-	~ElementStyle();
 
 	/// Returns the element's definition.
 	const ElementDefinition* GetDefinition() const;
@@ -158,7 +157,7 @@ private:
 	// Any properties that have been overridden in this element.
 	PropertyDictionary inline_properties;
 	// The definition of this element, provides applicable properties from the stylesheet.
-	ElementDefinition* definition;
+	std::shared_ptr<ElementDefinition> definition;
 	// Set if a new element definition should be fetched from the style.
 	bool definition_dirty;
 

+ 7 - 13
Source/Core/StyleSheet.cpp

@@ -50,22 +50,18 @@ static bool StyleSheetNodeSort(const StyleSheetNode* lhs, const StyleSheetNode*
 
 StyleSheet::StyleSheet()
 {
-	root = new StyleSheetNode("", StyleSheetNode::ROOT);
+	root = std::make_unique<StyleSheetNode>("", StyleSheetNode::ROOT);
 	specificity_offset = 0;
 }
 
 StyleSheet::~StyleSheet()
 {
-	delete root;
-
-	for (ElementDefinitionCache::iterator cache_iterator = node_cache.begin(); cache_iterator != node_cache.end(); ++cache_iterator)
-		(*cache_iterator).second->RemoveReference();
 }
 
 bool StyleSheet::LoadStyleSheet(Stream* stream)
 {
 	StyleSheetParser parser;
-	specificity_offset = parser.Parse(root, stream, *this, keyframes, decorator_map, spritesheet_list);
+	specificity_offset = parser.Parse(root.get(), stream, *this, keyframes, decorator_map, spritesheet_list);
 	return specificity_offset >= 0;
 }
 
@@ -75,8 +71,8 @@ StyleSheet* StyleSheet::CombineStyleSheet(const StyleSheet* other_sheet) const
 	RMLUI_ASSERT(other_sheet);
 
 	StyleSheet* new_sheet = new StyleSheet();
-	if (!new_sheet->root->MergeHierarchy(root) ||
-		!new_sheet->root->MergeHierarchy(other_sheet->root, specificity_offset))
+	if (!new_sheet->root->MergeHierarchy(root.get()) ||
+		!new_sheet->root->MergeHierarchy(other_sheet->root.get(), specificity_offset))
 	{
 		delete new_sheet;
 		return NULL;
@@ -308,7 +304,7 @@ FontEffectListPtr StyleSheet::InstanceFontEffectsFromString(const String& font_e
 
 
 // Returns the compiled element definition for a given element hierarchy.
-ElementDefinition* StyleSheet::GetElementDefinition(const Element* element) const
+std::shared_ptr<ElementDefinition> StyleSheet::GetElementDefinition(const Element* element) const
 {
 	RMLUI_ASSERT_NONRECURSIVE;
 
@@ -353,19 +349,17 @@ ElementDefinition* StyleSheet::GetElementDefinition(const Element* element) cons
 	auto cache_iterator = node_cache.find(seed);
 	if (cache_iterator != node_cache.end())
 	{
-		ElementDefinition* definition = (*cache_iterator).second;
-		definition->AddReference();
+		std::shared_ptr<ElementDefinition>& definition = (*cache_iterator).second;
 		applicable_nodes.clear();
 		return definition;
 	}
 
 	// Create the new definition and add it to our cache. One reference count is added, bringing the total to two; one
 	// for the element that requested it, and one for the cache.
-	ElementDefinition* new_definition = new ElementDefinition(applicable_nodes);
+	auto new_definition = std::make_shared<ElementDefinition>(applicable_nodes);
 
 	// Add to the node cache.
 	node_cache[seed] = new_definition;
-	new_definition->AddReference();
 
 	return new_definition;
 }