Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind] Unable to pass method to simple function pointer
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-10-28 11:41:20


AMDG

Carlos wrote:
> I am trying to make a wrapper for libxml's SAX api, for learning purpose, I
> am trying to use bind to wrap sax events in a class like this :
>
>
> namespace sax {
> class document {
>
> public:
> document();
> ~document();
>
> protected:
>
> virtual void start_node( void *user_data, const xmlChar *name,
> const xmlChar **attrs );
>
> xmlSAXHandler m_evhdl;
> xmlParserCtxtPtr m_ctxt;
> };
> }
>
>
> The start_node() function is intended to be pointed by the startElement
> member from xmlSAXHandler ( it has same definition than start_node ).
>
> I tried to use bind to copy function pointer in the constructor like this :
>
>
> document::document() {
>
> // binding here
> m_evhdl.startElement = boost::bind( &document::start_node, this, _1, _2,
> _3 );
>
> // xml init
> m_ctxt = xmlCreateFileParserCtxt( "catalogue.xml" );
>
> m_ctxt->sax = &m_evhdl;
> xmlParseDocument( m_ctxt );
> }
>
>
> The issue is that I can't submit the binded function to startElement because
> bind submits "this" as a parameter to startElement, which is incompatible
> definition, so is there a solution to my binding ?
>

I don't know much about libxml, but it looks like you should
pass the this pointer in the user_data argument of start_node.

Something like this:

class document {
    // ...
    static void start_node_func(void* user_data, const xmlChar* name,
const xmlChar** attrs) {
        static_cast<document*>(user_data)->start_node(name, attrs);
    }
};

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net