[C++-sig] [Py++] How to wrap default arg as enum with complicated scope?

Christopher Bruns cmbruns at stanford.edu
Mon Oct 12 22:51:32 CEST 2009


Below is a simplified example of a tricky wrapping problem I have
encountered.  The example remains somewhat complicated, but further
simplification results in a file that causes no trouble.
I get a compile error when I try to build the boost python code
generated by the following case:

#### test_enum.h ####
struct System {
    struct ProjectOptions {
        enum Option {
            All = 1
        };
        ProjectOptions(Option);
    };
    void project(ProjectOptions = ProjectOptions::All);
};
#### end test_enum.h ####

The trouble comes from the default value ('ProjectOptions::All') for
the project() method.  Notice also the constructor for ProjectOptions
from Options.  That is required for reproducing the trouble here.

The resulting boost.python code is as follows:

#### wrap_enum2.py ####
from pyplusplus import module_builder

mb = module_builder.module_builder_t(["test_enum2.h"]
    , gccxml_path = "gccxml.exe")
mb.build_code_creator( module_name='test' )
mb.write_module( 'test_wrap_enum2.cpp' )
#### end wrap_enum2.py ####

#### test_wrap_enum2.cpp ####
// This file has been generated by Py++.

#include "boost/python.hpp"

#include "test_enum2.h"

namespace bp = boost::python;

BOOST_PYTHON_MODULE(test){
    { //::System
        typedef bp::class_< System > System_exposer_t;
        System_exposer_t System_exposer = System_exposer_t( "System" );
        bp::scope System_scope( System_exposer );
        { //::System::ProjectOptions
            typedef bp::class_< System::ProjectOptions >
ProjectOptions_exposer_t;
            ProjectOptions_exposer_t ProjectOptions_exposer =
ProjectOptions_exposer_t( "ProjectOptions", bp::init<
System::ProjectOptions::Option >(( bp::arg("arg0") )) );
            bp::scope ProjectOptions_scope( ProjectOptions_exposer );
            bp::enum_< System::ProjectOptions::Option>("Option")
                .value("All", System::ProjectOptions::All)
                .export_values()
                ;
            bp::implicitly_convertible<
System::ProjectOptions::Option, System::ProjectOptions >();
        }
        { //::System::project

            typedef void ( ::System::*project_function_type )(
::System::ProjectOptions ) ;

            System_exposer.def(
                "project"
                , project_function_type( &::System::project )
                , ( bp::arg("arg0")=All ) );
        }
    }
}
#### test_wrap_enum2.cpp ####

The symbol "All" in the line ", ( bp::arg("arg0")=All ) );" is out of
scope here.  If I change the line from

  , ( bp::arg("arg0")=All ) ); // compile failure

to

  , ( bp::arg("arg0")=System::ProjectOptions::All ) ); // works

it compiles fine.

Is there any way to instruct pyplusplus to generate the default
argument value for the project() method with the scope
"System::ProjectOptions::All"?

Any enlightenment is much appreciated.

--Chris Bruns


More information about the Cplusplus-sig mailing list