cppfw/svgdom

Iterator for Elements

Closed this issue · 1 comments

Visitors are used to traverse the dom, since derived Element classes are not homogeneous. It would be convenient, however, to have a custom iterator to access all Elements and their members.

It would be great to access the IDs of all the elements with a simple loop, for example, instead of having to create a new Visitor:

for (auto element : dom)
    cout<<element.id<<endl;

This would be helpful to do simple operations like counting the number of elements, or searching for an element with certain ID.

Edit: Looking at the problem, it seems like the Container's children member would have to be moved to Element in order for this to work.

Looking at the problem, it seems like the Container's children member would have to be moved to Element in order for this to work.

Yes, this is actually a problem. The DOM is supposed to closely represent the xml structure of the SVG document. And far not all SVG's elements can have child elements, i.e. not all of them are containers.

One possible compromise to the problem would be to implement a universal visitor (possible name element_visitor) and include it to the library. This visitor would visit all the elements and call a user supplied handler function to handle each element as simple element. Actually, maybe a special visitor is not needed, as the visitor class provides default_visit() method which can be overridden. I.e. no need to override all the visit() methods, just override one default_visit(element) and then each DOM's element can be visited without casting it to specific element class.