Add convenience methods for DOM creation
Closed this issue · 5 comments
Using unique_ptr
and utki::Unique
makes the construction of DOMs a bit convoluted, and it can be confusing for users that don't understand the concept of unique pointers. It would be much easier to write something dom.push_back(path)
instead of dom->children.push_back(utki::makeUnique<svgdom::PathElement>(path))
. Maybe it would be a good idea to add convenience methods to create DOMs and hide the management of unique pointers.
Could you elaborate more on your proposal?
Like what is the path
variable, its type?
I created a branch called container-push-back
with a very rough function called pushBackUnique
to simplify appending elements to a Container
: master...JaimeIvanCervantes:container-push-back
Appending a path element can be done with this line dom->pushBackUnique(path)
instead of this one dom->children.push_back(utki::makeUnique<svgdom::PathElement>(path))
.
This is just a quick suggestion, so I didn't do a PR. Maybe there are other ways to handle unique pointers automatically and hide the complexity when one wants to create DOMs dynamically.
So, as I understood from your example dom->pushBackUnique(path)
the path
is svgdom::PathElement
, right?
But then it will do the copy of the object using copy constructor without the need.
So, as I understood from your example dom->pushBackUnique(path) the path is svgdom::PathElement, right?
That's correct.
But then it will do the copy of the object using copy constructor without the need.
I would be doing the same constructor copy as in the line dom->children.push_back(utki::makeUnique<svgdom::PathElement>(path))
. Is there an alternative?
Well, it depends on how you prepare the element before adding it. I would say it should be something like this:
{
auto e = utki::makeUnique<svgdom::PathElement>();
e->path.push_back(...)
...
dom->children.push_back(std::move(e));
}