Smart pointers to manage DrawingElements

This commit is contained in:
trotFunky 2019-05-22 18:47:48 +02:00
parent 49b815a6db
commit 86273890ae
2 changed files with 6 additions and 6 deletions

View file

@ -6,7 +6,7 @@
namespace xmlParser namespace xmlParser
{ {
Group::Group(std::string label, float x, float y, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), drawingElements({}) Group::Group(std::string label, float x, float y, const sf::Color& color) : DrawingElement(std::move(label),x,y,color), drawingElements()
{} {}
Group::Group(const pugi::xml_node& node) : DrawingElement(node) Group::Group(const pugi::xml_node& node) : DrawingElement(node)
@ -32,7 +32,7 @@ namespace xmlParser
!strncmp(childNode.name(),"Group",5) ? dynamic_cast<DrawingElement*>(new Group(childNode)) : nullptr; !strncmp(childNode.name(),"Group",5) ? dynamic_cast<DrawingElement*>(new Group(childNode)) : nullptr;
if(newElement != nullptr) if(newElement != nullptr)
{ {
drawingElements.insert(std::make_pair(newElement->label,newElement)); drawingElements.emplace(std::make_pair(newElement->label,std::move(std::unique_ptr<DrawingElement>(newElement))));
} }
} }
} }
@ -42,13 +42,13 @@ namespace xmlParser
std::transform(elements.begin(),elements.end(),std::inserter(drawingElements,drawingElements.begin()), std::transform(elements.begin(),elements.end(),std::inserter(drawingElements,drawingElements.begin()),
[](std::pair<std::string,DrawingElement*> pair) [](std::pair<std::string,DrawingElement*> pair)
{ {
return(pair); return(std::make_pair(pair.first,std::move(std::unique_ptr<DrawingElement>(pair.second))));
}); });
} }
bool Group::addDrawingElement(DrawingElement* drawingElement) bool Group::addDrawingElement(DrawingElement* drawingElement)
{ {
return(drawingElements.insert(std::make_pair(drawingElement->label,drawingElement)).second); return(drawingElements.insert(std::make_pair(drawingElement->label,std::move(std::unique_ptr<DrawingElement>(drawingElement)))).second);
} }
bool Group::removeDrawingElement(const std::string& label) bool Group::removeDrawingElement(const std::string& label)
@ -58,7 +58,7 @@ namespace xmlParser
DrawingElement* Group::getDrawingElement(const std::string& label) DrawingElement* Group::getDrawingElement(const std::string& label)
{ {
return(drawingElements.at(label)); return(drawingElements.at(label).get());
} }
void Group::draw(sf::RenderWindow& window) void Group::draw(sf::RenderWindow& window)

View file

@ -36,7 +36,7 @@ namespace xmlParser
const std::string toString(int indent) const override; const std::string toString(int indent) const override;
private: private:
std::map<std::string,DrawingElement*> drawingElements; std::map<std::string,std::unique_ptr<DrawingElement>> drawingElements;
}; };
} }