[C++-sig] Define class in 2 scopes without re-exposing

John Reid j.reid at mail.cryst.bbk.ac.uk
Fri Apr 13 16:16:07 CEST 2012


Hi,

If I want to define the same class (Inner) in 2 different scopes (Outer1 
and Outer 2), I can do it like this:

#include <boost/python.hpp>

struct Outer1 {};
struct Outer2 {};
struct Inner {};

BOOST_PYTHON_MODULE( _sandbox )
{
     namespace bp = ::boost::python;
     {
         bp::scope scope = bp::class_< Outer1 >( "Outer1" );
         bp::class_< Inner >( "Inner" );
     }

     {
         bp::scope scope = bp::class_< Outer2 >( "Outer2" );
         bp::class_< Inner >( "Inner" );
     }
}


Unfortunately when I import the _sandbox module I get the standard error:

RuntimeWarning: to-Python converter for Inner already registered; second 
conversion method ignored.

and in debug build the second Inner registration asserts.

Is there any way to test if Inner is already registered and if so just 
place it in the correct scope? Perhaps replacing the second scoped block 
with something like this:

     {
         bp::object class_Outer2 = bp::class_< Outer2 >( "Outer2" );
         bp::type_info info = boost::python::type_id< Inner >();
         const bp::converter::registration * reg = 
bp::converter::registry::query( info );
         if( NULL == reg ) {
             bp::class_< Inner >( "Inner" );
         } else {
             // Some magic here!
         }
     }

Also I should say that the 2 scoped blocks of code aren't adjacent in my 
library so I don't really want to pass objects between them. I'm hoping 
for a solution that just extracts info from the boost.python registry.

Thanks for any help,
John.



More information about the Cplusplus-sig mailing list