[C++-sig] No automatic upcasting with std::shared_ptr in function calls?

2012-04-20 Thread VáclavŠmilauer
Hello,

I have a sample hierarchy of polymorphic classes (A from which B inherits).
One of them (A1, B1) is managed with boost::shared_ptr, the other one
(A2, B2) via std::shared_ptr (I defined the get_pointer template for 
std::shared_ptr).

When I call f1(boost::shared_ptr) with an object B1 from python,
it is correctly upcast to the pointer to its base class and the c++
function is called. When I call f2(std::shared_ptr) with B2 argument
from python, no upcasting takes place and I get Boost.Python.Argument
error.

The code is here:

foo.cpp:

#include
/// make boost::python understand std::shared_ptr
#include
namespace boost {
   template T* get_pointer(std::shared_ptr p){ return p.get(); }
}

// define a hierarchy
struct A1{ virtual ~A1(){} };
struct B1: public A1{ virtual ~B1(){} };
void f1(boost::shared_ptr ptr){ std::cerr<<"f1()"< ptr){ std::cerr<<"f2()"<
using namespace boost::python;
BOOST_PYTHON_MODULE(foo){
   class_>("A1");
   class_,bases>("B1");
   def("f1",f1);
   class_>("A2");
   class_,bases>("B2");
   def("f2",f2);
}

compiling with (under Linux):

  g++ -std=c++0x foo.cpp -o foo.so -fPIC -shared -lboost_python
  `pkg-config python --libs --cflags`

Running

  PYTHONPATH=. python -c "import foo; foo.f1(foo.B1()); foo.f2(foo.B2());"

I obtain:

  f1()
  Traceback (most recent call last):
File "", line 1, in 
  Boost.Python.ArgumentError: Python argument types in
  foo.f2(B2)
  did not match C++ signature:
  f2(std::shared_ptr)

Where is the problem?

Cheers, Vaclav

___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Re : Re : boost::python: C++/Python function overload issue

2012-04-20 Thread christophe jean-joseph


Thank you very much.
I knew the link already but it made me read my code again and realize that I 
forgot to reflect the overloaded functions with boost::python.

Christophe Jean-Joseph




 De : JS Unkn0wn 
À : christophe jean-joseph ; Development of Python/C++ 
integration  
Envoyé le : Jeudi 19 avril 2012 1h21
Objet : Re: [C++-sig] Re : boost::python: C++/Python function overload issue
 

Does this (  
http://www.boost.org/doc/libs/1_49_0/libs/python/doc/tutorial/doc/html/python/exposing.html 
 ) answer your question? specifically "Class Virtual Functions".


On Wed, Apr 18, 2012 at 11:55 PM, christophe jean-joseph 
 wrote:


>
>Hi,
>
>
>here is my problem.
>
>
>Let say have a C++ A class and a Ader class in Python, derived from A (through 
>boost::python).
>I have a list of functions f_i in Ader overloading the equivalent functions 
>with same name in A.
>The main is written in Python and call a method g of a C++ class B passing and 
>argument of type Ader (it's written as A type under C++, but I don't have any 
>type issue there).
>Let say B::g call a function f_1, calling f_2, calling f_3. The error occur at 
>f_3 as I could identify it uses A::f_3 instead of Ader::f_3.
>By going down, I could confirm that, from f_1 to f_3, all the 3 called 
>functions are the A version. 
>So my question is, how can I fix this issue so that the B::g function uses 
>Ader functions (meanning Python functions) and not A functions?
>On a side, this is a C++/Python version of a code working fine when fully 
>written in C++.
>
>Christophe Jean-Joseph
> 
>___
>Cplusplus-sig mailing list
>Cplusplus-sig@python.org
>http://mail.python.org/mailman/listinfo/cplusplus-sig
>___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

[C++-sig] boost python writing converters for list to std::vector

2012-04-20 Thread Helfer Thomas
Hi,

using
http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/, 
I tried to write a converter for list to std::vector (In the example, T will 
be double).

The code (see converter.cxx) I wrote is still in early stage of
development (no check) but compiles fine on ubuntu oneiric with boost
python 1.46. I used it to create an std module. However, a simple test
(see test.py) does not work as exepted.

Did I misunderstood something or does boost have a special treatment for
the list object which bypasses converters ? 

Thanks for any help,

Sincerly,

Helfer THomas
#include
#include
#include
#include

#include

template
struct Vector_to_python_list
{
   
  static PyObject* convert(std::vector const& v)
  {
using namespace std;
using namespace boost::python;
using boost::python::list;
list l;
typename vector::const_iterator p;
for(p=v.begin();p!=v.end();++p){
  l.append(object(*p));
}
return incref(l.ptr());
  }
};
 
template
struct Vector_from_python_list
{

Vector_from_python_list()
{
  using namespace boost::python;
  using namespace boost::python::converter;
  registry::push_back(&Vector_from_python_list::convertible,
			  &Vector_from_python_list::construct,
			  type_id >());
}
 
// Determine if obj_ptr can be converted in a std::vector
static void* convertible(PyObject* obj_ptr)
{
  if (!PyList_Check(obj_ptr)){
	return 0;
  }
  return obj_ptr;
}
 
// Convert obj_ptr into a std::vector
static void construct(
PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
  using namespace boost::python;
  // Extract the character data from the python string
  //  const char* value = PyString_AsString(obj_ptr);
  list l(handle<>(borrowed(obj_ptr)));

  // // Verify that obj_ptr is a string (should be ensured by convertible())
  // assert(value);
 
  // Grab pointer to memory into which to construct the new std::vector
  void* storage = (
(boost::python::converter::rvalue_from_python_storage >*)
data)->storage.bytes;
 
  // in-place construct the new std::vector using the character data
  // extraced from the python object
  std::vector& v = *(new (storage) std::vector());
 
  // populate the vector from list contains !!!
  int le = len(l);
  v.resize(le);
  for(int i = 0;i!=le;++i){
	v[i] = extract(l[i]);
  }

  // Stash the memory chunk pointer for later use by boost.python
  data->convertible = storage;
}
};
 
void initializeConverters()
{
}

void
print(std::vector&v)
{
  using namespace std;
  copy(v.begin(),v.end(),
   ostream_iterator(cout," "));
  cout << endl;
}

BOOST_PYTHON_MODULE(std)
{
  using namespace boost::python;
  using namespace boost::python;

  // register the to-python converter
  to_python_converter<
std::vector,
Vector_to_python_list >();
 
  // register the from-python converter
Vector_from_python_list();

  def("display",print);
}
import std
std.display([3.,12.])
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig