Help needed on usage of "on_stack_with_xml_element"

27 views
Skip to first unread message

frank.h...@gmail.com

unread,
Jul 24, 2019, 10:01:47 AM7/24/19
to SVG++ library
Hi @ll,

I need to parse an Inkscape SVG file with which is basically made of
several <g> nodes (layers) each containing lots of shapes (mostly rects but not only).

Therefore I've created 3 context classes:

1. class BaseContext
2. class ShapeContext : public BaseContext
3. class RectContext : public ShapeContext (<- is this still correct or do I have to derive from BaseContext?)

Additionally I've created factories for these three:

struct ChildContextFactories {
    template<class ParentContext, class ElementTag, class Enable = void>
    struct apply {
        // Default definition handles "svg" and "g" elements
        typedef svgpp::factory::context::on_stack<BaseContext> type; };
};

template<class ElementTag>
struct ChildContextFactories::apply<
    BaseContext, ElementTag,
    typename boost::enable_if<boost::mpl::has_key<svgpp::traits::shape_elements, ElementTag> >::type> {
    typedef svgpp::factory::context::on_stack<ShapeContext> type;
};

template<>
struct ChildContextFactories::apply<BaseContext, svgpp::tag::element::rect> {
    typedef svgpp::factory::context::on_stack<RectContext> type;
};

As the Inkscape file is using nonstandard attributes I need access to the XML-Element and decided to switch over to use
"on_stack_with_xml_element".

However I fail to get it compiled and kindly ask if someone can shed some light on this? That means how do I have to formulate
my classes correctly to get the XML_element passed too?

I tried to understand demo/Sample in "../svgpp/sample_animation.cpp" but I get confused because here the factories
are mixing "same" and "on_stack_with_xml_element"?! Is this a bug?

Thanks,
Frank

svgpp

unread,
Jul 24, 2019, 12:13:13 PM7/24/19
to SVG++ library
Hi Frank,

1. Hierarchy of your Context-classes is entirely up to your needs. But usually there is some base that handles common behavior like style, transform etc. 
It's also an answer to you question about sample_animation.cpp - I used such combination for some reason (I don't remember why).

2. For on_stack_with_xml_element you should just add another argument to the context constructor. Could you please cite compiler error here.

frank.h...@gmail.com

unread,
Aug 9, 2019, 4:16:40 AM8/9/19
to SVG++ library
I'm trying to create a modified version of the "on_stack_with_xml_element" factory in order to have non-const element:


namespace svgpp::factory::context
{

template<class ChildContext>
class on_stack_with_mutable_xml_element : boost::noncopyable
{
public:
    typedef ChildContext type;

    template<class ParentContext, class XMLElement>
    on_stack_with_mutable_xml_element(ParentContext& context, XMLElement const& xml_element)
        : context_(context, xml_element)
    {
    }

    type& get() const { return context_; }

    void on_exit_element() const
    {
        context_.on_exit_element();
    }

private:
    type& context_;
};

} // namespace svgpp::factory::context

As my first approach didn't work I just did a copy&paste of the original factory - unmodified to modify later.
But even that one does not work and fails to compile:

Severity    Code    Description    Project    File    Line    Suppression State
Error    C2440    'initializing': cannot convert from 'initializer list' to 'LayoutContext::ShapeContext &'    NetLister    d:\_proj\netlister\include\layoutcontext.h    25   
Error    C2439    'svgpp::factory::context::on_stack_with_mutable_xml_element<LayoutContext::ShapeContext>::context_': member could not be initialized    NetLister    d:\_proj\netlister\include\layoutcontext.h    26   

Does someone have an idea what might be wrong?

svgpp

unread,
Aug 9, 2019, 4:25:09 AM8/9/19
to SVG++ library
Hi Frank,

context_ is the reference to ShapeContext you can only initialize it with ShapeContext lvalue, not with (ParentContext&, XMLElement const&) pair.

frank.h...@gmail.com

unread,
Aug 9, 2019, 4:49:23 AM8/9/19
to SVG++ library
Copy&Paste gone terribly wrong. Plain embarrassing! *FACEPALM*

frank.h...@gmail.com

unread,
Aug 9, 2019, 5:23:05 AM8/9/19
to SVG++ library
Do you have a hint what to change to make this one working now:

    template<class ParentContext, class XMLElement>
    on_stack_with_mutable_xml_element(ParentContext& context, XMLElement const& xml_element)
        : context_(context, const_cast<typename std::remove_const<XMLElement>::type>(xml_element))
    {
    }

produces:

Error    C2664    'LayoutContext::BaseContext::BaseContext(const LayoutContext::BaseContext &)': cannot convert argument 2 from 'const rapidxml_ns::xml_node<char> *' to 'rapidxml_ns::xml_node<char> *'    NetLister    d:\_proj\netlister\include\layoutcontext.h    25   

 

svgpp

unread,
Aug 9, 2019, 5:36:28 AM8/9/19
to SVG++ library
It's C++ - std::remove_const removes topmost const - there isn't such in const rapidxml_ns::xml_node<char> *. May be you can just name type explicitly - "rapidxml_ns::xml_node<char> *"?
Reply all
Reply to author
Forward
0 new messages