[Xapian-discuss] Query::Query() in PHP, also QueryParser::prefixes

Francis Irving francis@flourish.org
Fri, 28 May 2004 12:34:53 +0100


On Fri, May 28, 2004 at 12:25:01PM +0100, Sam Liddicott wrote:
> > > It's now called %name, I think. I've managed to get it working for
> > > constructors in the past, but admittedly not for a while.
> >
> > Aha!  Thank you.  Much more success.  I've tried this.
> >
> > %name(QueryCombine) Xapian::Query::Query(Xapian::Query::Query::op op_,
> > %const Xapian::Query::Query & left, const Xapian::Query::Query &
> > %right);
> >
> > It makes a PHP function called query_querycombine.  Unfortunately,
> > the function takes a spurious extra Query argument as its first
> > parameter.  SWIG thinks it is a member function, rather than a
> > constructor.
> 
> Is this observation based on the C++ code that swig generates?

It wasn't, but checking the code it looks like this.  There is an extra self
parameter which isn't used by the function:

Xapian::Query *Xapian_Query_QueryCombine(Xapian::Query *self,Xapian::Query::op op_,Xapian::Query const &left,Xapian::Query const &right){
                return new Xapian::Query(op_, left, right);
            }

The wrappers for the real constructor and the new one look like this:
        ZEND_NAMED_FE(new_query,
                _wrap_new_Query, NULL)
        ZEND_NAMED_FE(query_querycombine,
                _wrap_Query_QueryCombine, NULL)

Looking at the code for _wrap_new_Query, it does some extra fancy stuff,
checking parameters and wrapping the returned new object in some other object.  I
suspect we need this to avoid memory leaks.

How can I persuade swig to generate this for our renamed constructor?

    result = (Xapian::Query *)new Xapian::Query((std::string const &)*arg1,arg2,arg3);
...

    SWIG_SetPointerZval(return_value, (void *)result, SWIGTYPE_p_Xapian__Query, 1);

    /* Wrap this return value */
    {
        /* ALTERNATIVE Constructor, make an object wrapper */
        zval *obj, *_cPtr;
        MAKE_STD_ZVAL(obj);
        MAKE_STD_ZVAL(_cPtr);
        *_cPtr = *return_value;
        INIT_ZVAL(*return_value);
        *return_value=*_cPtr;
    }

Francis