Using on_enter_element

43 views
Skip to first unread message

gosha

unread,
Sep 20, 2017, 12:11:40 PM9/20/17
to SVG++ library
Hi!

Do I understand correctly that in practical imlementation I should use on_enter_element(tag::element::specific_type) for each element, enumerated in processed_elements_t instead of remain on_enter_element(tag::element::any)?

Thanks in advance
gosha

svgpp

unread,
Sep 20, 2017, 12:20:41 PM9/20/17
to SVG++ library
Hi,

It depends on your needs. In most cases you may start with automatic context object construction provided by document_traversal class (see context_factories) like in sample01d. But if you really want to use single context for entire document, then you'll probably need specific element types instead on "any".

FW

unread,
Nov 13, 2018, 4:10:54 AM11/13/18
to SVG++ library
How can I get access to the element that caused the callback to: "on_enter_element"?

I've implemented my context like this:

    class CntLayersCtx
    {
    public:
        CntLayersCtx() : mNumOfLayers(0) {}
        void on_enter_element(svgpp::tag::element::g elem) { .....................}
        void on_exit_element() {}
    };

But how can I access e.g. the element and/or attributes of the current element?


svgpp

unread,
Nov 13, 2018, 4:41:27 AM11/13/18
to SVG++ library
Hi,

At first you should decide whether you want single context object for entire SVG document or SVG++ will create new context object for each SVG element (or some combination of these approaches). It is managed by so called Context Factories (http://svgpp.org/document_traversal.html#context-factories). Simple example of its usage is here https://github.com/svgpp/svgpp/blob/master/src/samples/sample01d.cpp#L74
Let's say you need single context object for entire SVG document but also want to get pointer to each XML element parsed. Unfortunately there is no such option out of the box, but it's just a question of few lines to add:

template<class ParentContext, class ElementTag>
class same_context_with_xml_element: boost::noncopyable
{
public:
typedef ParentContext type;
template<class XMLElement>
same_context_with_xml_element(ParentContext & context, XMLElement const & xml_elem)
: context_(context)
{
context_.on_enter_element(ElementTag(), xml_elem);
}
type & get() const { return context_; }
void on_exit_element() const
{
context_.on_exit_element();
}
private:
type & context_;
};
struct ChildContextFactories
{
template<class ParentContext, class ElementTag, class Enable = void>
struct apply
{

typedef svgpp::factory::context::same_context_with_xml_element<BaseContext> type;
};
};

{

document_traversal<
...
context_factories<ChildContextFactories>
>::load_document(xml_root_element, context);
}
Reply all
Reply to author
Forward
0 new messages