Boost logo

Boost :

From: Peter.Bienstman_at_[hidden]
Date: 2001-07-27 08:57:47


I'm still trying to find the best solution for the problem I already
mentioned in

This is the code concerned:

#include <iostream>
#include "boost/python/class_builder.hpp"

using namespace boost::python;

struct Expression { Expression() {} };

struct Term
    Term() {}
    Term(const Expression&) {}

void f(const Term& t) {}

struct A { A(const Term&) {} };

  module_builder convert("convert");

  class_builder<Term> Term_(convert, "Term");
  Term_.def(constructor<const Expression&>());
  class_builder<Expression> Expression_(convert, "Expression");

  convert.def(f , "f");

  class_builder<A> A_(convert, "A");
  A_.def(constructor<const Term&>());
  catch(...) {}

>>>from convert import *
>>>e = Expression()
>>>a = A(Term(e))

These work, but in my real-world application, the function signatures
are a lot more compilated (concatenated expressions using operators +
and *), and I don't want to burden my users with the need of
explicitly creating Terms from Expressions. The natural way of
writing would be

>>>a = A(e)

These give

TypeError: extension class 'Expression' is not convertible into 'Term'

So I set out to write a conversion function of my own:


PyObject* to_python(const Expression& e)
  return to_python(Term(e));


I was actually afraid that this would convert all my Expressions
straight to Terms, which is also not really what I want, but instead
this code changes nothing to the previous behaviour.

So, my question: can I add some code, either to my own code or to the
Boost library, to get the behaviour I want?

I started looking through the source, but with all these templates,
the flow of control is not really immediately obvious. Specifically,
when and from where is called? It seems to act like a
preprocessor, but due to my lack of understanding of the BPL big
picture, I currently don't see where it fits in.



Boost list run by bdawes at, gregod at, cpdaniel at, john at