[C++-sig] Python-overridable method taking const ref?
Paul Melis
pyplusplus at assumetheposition.nl
Wed Feb 4 21:30:05 CET 2009
Roman Yakovenko wrote:
> On Wed, Feb 4, 2009 at 9:26 PM, Paul Melis
> <pyplusplus at assumetheposition.nl> wrote:
>
>> Hi Roman,
>>
>
> Try this one, works for me:
> // This file has been generated by Py++.
>
> // Copyright 2004-2008 Roman Yakovenko.
> // Distributed under the Boost Software License, Version 1.0. (See
> // accompanying file LICENSE_1_0.txt or copy at
> // http://www.boost.org/LICENSE_1_0.txt)
>
> #include "boost/python.hpp"
>
> #include "__call_policies.pypp.hpp"
>
> #include "__convenience.pypp.hpp"
>
> #include "protected_to_be_exported.hpp"
>
> namespace bp = boost::python;
>
> struct Callback_wrapper : Callback, bp::wrapper< Callback > {
>
> Callback_wrapper( )
> : Callback( )
> , bp::wrapper< Callback >(){
> // null constructor
>
> }
>
> virtual void execute( ::Thing const & t ) {
> namespace bpl = boost::python;
> if( bpl::override func_execute = this->get_override( "execute" ) ){
> bpl::object py_result = bpl::call<bpl::object>(
> func_execute.ptr(), t );
> }
> else{
> Callback::execute( boost::ref(t) );
> }
> }
>
> static void default_execute( ::Callback & inst, ::Thing & t ){
> if( dynamic_cast< Callback_wrapper * >( boost::addressof( inst ) ) ){
> inst.::Callback::execute(t);
> }
> else{
> inst.execute(t);
> }
> }
>
> //hhhh
>
> };
>
> BOOST_PYTHON_MODULE(protected){
> { //::Callback
> typedef bp::class_< Callback_wrapper, boost::noncopyable >
> Callback_exposer_t;
> Callback_exposer_t Callback_exposer = Callback_exposer_t(
> "Callback", "documentation", bp::no_init );
> bp::scope Callback_scope( Callback_exposer );
> Callback_exposer.def( bp::init< >("documentation") );
> { //::Callback::execute
>
> typedef void ( *default_execute_function_type )(
> ::Callback &,::Thing & );
>
> Callback_exposer.def(
> "execute"
> , default_execute_function_type(
> &Callback_wrapper::default_execute )
> , ( bp::arg("inst"), bp::arg("t") )
> , "documentation" );
>
> }
> }
>
> { //::Thing
> typedef bp::class_< Thing, boost::noncopyable > Thing_exposer_t;
> Thing_exposer_t Thing_exposer = Thing_exposer_t( "Thing",
> "documentation", bp::no_init );
> bp::scope Thing_scope( Thing_exposer );
> }
> }
>
>
>
>> Which isn't surprising as your code uses the same method signatures in
>> Callback_wrapper as mine, and does the same set of def()'s...
>>
>
>
> The idea is to remove "const" from Thing argument. This will disallow
> Boost.Python to create temporal object.
>
Right, I knew that was necessary, but just didn't see how to accomplish
it. Thanks, this helps!
> Another difference is that the generated code passes arguments by
> reference: boost::ref.
>
Why is that? It doesn't seem to be needed
Paul
More information about the Cplusplus-sig
mailing list