boost python and boost/thread.hpp

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

boost python and boost/thread.hpp

Guillaume Carbonneau
Hi,

I'm having compilation errors as soon as I give my classes member variables that come from the boost thread library.
Wrapping those in shared_ptr seems to work.

Any idea why? Is there a special wrapping directive I should give it to?


#include <vector>
#include <boost/python.hpp>
#include <boost/thread.hpp>


struct Hello {
  Hello(){}

  bool run(int argc, char **argv, char **envp);
  std::string hello_string_;
  std::vector<std::string> hello_vector_ ;
  size_t hello_num_;
  boost::shared_ptr<std::string> hello_shared_ptr_string_;
  bool hello_bool_;
  boost::shared_ptr<boost::condition_variable> condition_variable_shared_ptr_;
  
  // does not compile
  //boost::condition_variable condition_variable_;
};


char const* greet()
{
  return "hello, world";
}


BOOST_PYTHON_MODULE(hello)
{
  using namespace boost::python;
  def("greet", greet);
  class_<Hello>("Hello");
}
    


$ make
g++ -c -o hello.o -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/ -Wall -O3 -fPIC -g  hello.cpp
/usr/local/include/boost/python/object/value_holder.hpp: In constructor ‘boost::python::objects::value_holder<Value>::value_holder(PyObject*, A0) [with A0 = boost::reference_wrapper<const Hello>, Value = Hello]’:
/usr/local/include/boost/python/object/make_instance.hpp:71:   instantiated from ‘static Holder* boost::python::objects::make_instance<T, Holder>::construct(void*, PyObject*, boost::reference_wrapper<const T>) [with T = Hello, Holder = boost::python::objects::value_holder<Hello>]’
/usr/local/include/boost/python/object/make_instance.hpp:45:   instantiated from ‘static PyObject* boost::python::objects::make_instance_impl<T, Holder, Derived>::execute(Arg&) [with Arg = const boost::reference_wrapper<const Hello>, T = Hello, Holder = boost::python::objects::value_holder<Hello>, Derived = boost::python::objects::make_instance<Hello, boost::python::objects::value_holder<Hello> >]’
/usr/local/include/boost/python/object/class_wrapper.hpp:29:   instantiated from ‘static PyObject* boost::python::objects::class_cref_wrapper<Src, MakeInstance>::convert(const Src&) [with Src = Hello, MakeInstance = boost::python::objects::make_instance<Hello, boost::python::objects::value_holder<Hello> >]’
/usr/local/include/boost/python/converter/as_to_python_function.hpp:27:   instantiated from ‘static PyObject* boost::python::converter::as_to_python_function<T, ToPython>::convert(const void*) [with T = Hello, ToPython = boost::python::objects::class_cref_wrapper<Hello, boost::python::objects::make_instance<Hello, boost::python::objects::value_holder<Hello> > >]’
/usr/local/include/boost/python/to_python_converter.hpp:87:   instantiated from ‘boost::python::to_python_converter<T, Conversion, has_get_pytype>::to_python_converter() [with T = Hello, Conversion = boost::python::objects::class_cref_wrapper<Hello, boost::python::objects::make_instance<Hello, boost::python::objects::value_holder<Hello> > >, bool has_get_pytype = true]’
/usr/local/include/boost/python/object/class_wrapper.hpp:26:   instantiated from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::maybe_register_class_to_python(T2*, mpl_::false_) [with T2 = Hello, T = Hello, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]’
/usr/local/include/boost/python/object/class_metadata.hpp:229:   instantiated from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::register_aux2(T2*, Callback) [with T2 = Hello, Callback = boost::integral_constant<bool, false>, T = Hello, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]’
/usr/local/include/boost/python/object/class_metadata.hpp:219:   instantiated from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::register_aux(void*) [with T = Hello, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]’
/usr/local/include/boost/python/object/class_metadata.hpp:205:   instantiated from ‘static void boost::python::objects::class_metadata<T, X1, X2, X3>::register_() [with T = Hello, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]’
/usr/local/include/boost/python/class.hpp:496:   instantiated from ‘void boost::python::class_<T, X1, X2, X3>::initialize(const DefVisitor&) [with DefVisitor = boost::python::init<mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_>, W = Hello, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]’
/usr/local/include/boost/python/class.hpp:629:   instantiated from ‘boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*) [with W = Hello, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]’
hello.cpp:32:   instantiated from here
/usr/local/include/boost/python/object/value_holder.hpp:137: error: no matching function for call to ‘Hello::Hello(const Hello&)’
hello.cpp:7: note: candidates are: Hello::Hello()
hello.cpp:6: note:                 Hello::Hello(Hello&)
make: *** [hello.o] Error 1


_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: boost python and boost/thread.hpp

Stefan Seefeld-2
On 02/12/2012 01:34 PM, Guillaume Carbonneau wrote:
> Hi,
>
> I'm having compilation errors as soon as I give my classes member
> variables that come from the boost thread library.
> Wrapping those in shared_ptr seems to work.

By default, boost.python treats objects as copyable. That doesn't work
if some of your members aren't copyable themselves. You need to figure
out the semantics of your struct 'Hello'. Do you want it to be copyable,
than you need to change the definition of the non-copyable members, such
as you have done.

However, you could as well make the wrapped type non-copyable, in which
case that's not necessary.

>
> Any idea why? Is there a special wrapping directive I should give it to?

See http://www.boost.org/doc/libs/1_48_0/libs/python/doc/v2/class.html

    Stefan


--

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: boost python and boost/thread.hpp

Jim Bosch-2
In reply to this post by Guillaume Carbonneau
On 02/12/2012 01:34 PM, Guillaume Carbonneau wrote:
> Hi,
>
> I'm having compilation errors as soon as I give my classes member
> variables that come from the boost thread library.
> Wrapping those in shared_ptr seems to work.
>
> Any idea why? Is there a special wrapping directive I should give it to?
>

The problem is that boost::condition_variable doesn't have a copy
constructor.  When you declare a class with Boost.Python, it assumes it
can be returned by value (which requires a copy constructor).  The
boost::condition_variable data member prevents the compiler from
generating an implicit copy constructor.

You can solve this by instead using:

class_<Hello,boost::noncopyable>("Hello");

That tells Boost.Python not to assume this class can be returned by
value.  If it makes sense to define an explicit copy constructor for
your class, that will work too.

Jim
_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig