Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-22 17:49:02


----- Original Message -----
From: "Pearu Peterson" <boost_at_[hidden]>

>
> Hi!
>
> Is there a way to overload from_python functions defined by class_builder?
> To be specific, consider for example
>
> do_it_yourself_converters.cpp
>
> with the following (and only the following) addition:
>
> IndexingSet(int i) { /* ... */ }; // as a public constructor in
> // IndexingSet
>
> The objective is to have the following Python session:
> >>> s = IndexingSet()
> >>> s.add(4)
> (which would normally result TypeError: int)
>
> My plan was to provide the following from_python function:
>
> IndexingSet from_python(PyObject * o, python::type<const IndexingSet &>)
> {
> if (PyInt_Check(o))
> return IndexingSet(PyInt_AS_LONG(o));
> return from_python(o, python::type<IndexingSet &>());
> }
>
> but then the compiler complains about 'new declaration' and 'ambiguates
> old declaration' of this from_python function.
>
> Is there any other way to jump in into the chain of from_python functions
> that is generated by class_builder?

Nope.

> I do know that the alternative is to wrap IndexingSet with another class
> that defines add(int).

That's not neccessary. How about adding an overload for the
IndexingSet::add() function?
You can do it non-intrusively by using a free function:

void IndexingSet_add(IndexingSet& i, int x) { i.add(MillerIndex(x)); }
...
    ixset_class.def(IndexingSet_add, "add");

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk