On 08/09/2011 05:17 PM, fjanoos wrote:
> Hello,
>
> I'm new to boost::python and was having trouble wrapping a C++ function of
> the form 
>  void FindVCs(int vId, vector<int>& vcs);
>
> Here vcs is allocated in the caller and populated by FindVCs. 

Note that this should work, but requires you to instruct Python that
'vcs' is an "inout" value, so it gets passed into the function
by-reference, not by-value (the default). Please read the tutorial (and
reference) sections on call policies:
http://www.boost.org/doc/libs/1_46_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies

>
> Initially, I considered wrapping it something like this:
>
>       boost::python::list* FindVCs_wrap(int vid)
>       {
>               vector<int> vcs;
>               
>                FindVCs(vid,vcs);
>               //wrap and return
>               boost::python::list* out_list = new boost::python::list;
>               unsigned int num_elems = vcs.size();
>               for( unsigned int idx = 0; idx < num_elems; idx++){
>                       out_list->append(vcs[idx]);
>               }
>               return out_list;
>       }
>
> however, this gives me compile time errors 
> Error 5       error C2027: use of undefined type
> 'boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<T>'

This is a little trick (you could call it a kludge) to flag a user error
using metaprogramming: the last line above should be spoken out aloud:
Boost.Python noticed an ambiguous situation, and instead of guessing
what (return) value ownership semantics you intended, it requests you to
specify it (return value policies are very similar to call policies, so
the same docs above apply).


>       
>
> Then I change the return type to
>
> boost::shared_ptr<boost::python::list> FindVCs_wrap(int vid)
> {
>  ...
>         return boost::shared_ptr<boost::python::list>(out_list);
> }
>
> This compiles fine but then at runtime Python raises:
>
> TypeError: No to_python (by-value) converter found for C++ type: class
> boost::shared_ptr<class boost
> ::python::list>

Python list objects are themselves shared pointers, so Python wouldn't
know what to do with a shared_ptr<python::list>.

Regards,
        Stefan

-- 

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

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

Reply via email to