2013-06-03 4 views
0

템플릿이있는 외부 구조체에 내부 구조체가있는 C++ 데이터 구조가 있습니다. 템플릿 매개 변수에 따라 내부 구조체가 같은 유형일 수도 그렇지 않을 수도 있습니다. boost.python을 사용하여 구조를 Python에 노출하면 내부 클래스를 Outer.Inner로 참조 할 수 있기를 원합니다. 내 boost.python 노출 코드에서 각 개별 내부 유형을 한 번만 노출해야합니다. 같은 내부 클래스를 두 번 이상 노출시키지 않도록 boost.python 레지스트리를 쿼리 할 수 ​​있지만 이전에 노출 된 내부 클래스를 외부 클래스의 특성으로 만들려면 어떻게해야합니까? 완성도를 들어boost.python 내부 클래스를 두 번 등록하지 않지만 여전히 파이썬으로 노출합니다.

import inner_classes as IC 

IC.IntOuter.Inner 
IC.DoubleOuter.Inner 

여기 Jamroot 위를 컴파일 할 것 :

import python ; 
use-project boost : $(BOOST_ROOT) ; 

project : requirements <library>/boost/python//boost_python ; 

python-extension inner_classes : inner-classes.cpp ; 

install install 
    : inner_classes 
    : <install-dependencies>on <install-type>SHARED_LIB <install-type>PYTHON_EXTENSION 
    <location>. 
    ; 

local rule run-test (test-name : sources +) 
{ 
    import testing ; 
    testing.make-test run-pyd : $(sources) : : $(test-name) ; 
} 
run-test test : inner_classes test_inner_classes.py ; 
여기
#include <boost/python.hpp> 

struct inner { 
}; 

template< typename T > 
struct outer { 
    typedef inner inner_t; 

    static const char * name(); 

    static 
    void expose() { 
     using namespace boost::python; 

     class_< outer<T> > outer_class(name()); 

     // check if inner type is in registry already 
     const type_info inner_info = boost::python::type_id<inner_t>(); 
     const converter::registration * inner_registration 
      = boost::python::converter::registry::query(inner_info); 
     if(inner_registration == 0 || inner_registration->m_to_python == 0) { 
      // not already in registry 
      scope outer_scope(outer_class); 
      class_<inner_t> inner_class("Inner"); 
     } else { 
      // already in registry because exposed via different outer 
      // what to put here? In python we need Outer.Inner to exist 
     } 
    } 
}; 

template<> 
const char * 
outer<int>::name() { return "IntOuter"; } 

template<> 
const char * 
outer<double>::name() { return "DoubleOuter"; } 

BOOST_PYTHON_MODULE(inner_classes) 
{ 
    outer<int>::expose(); 
    outer<double>::expose(); 
} 

내가 실행하고자하는 파이썬 코드입니다 : 질문이 명확이 옷을 벗었 예를받을 수 있습니다

답변

0

boost.python의 일반적인 경우를 알면 매우 간단합니다. 파이썬에서이 작업은 다음과 같습니다.

그러나 내가 찾던 해결책이 아닙니다. C++에서이 작품 :

나는 이미 시도했다하지만 난> 핸들 <의 클래스 객체를 래핑하는 데 필요한 것을 실현하지 않았다
scope outer_scope(outer_class); 
outer_scope.attr("Inner") = handle<>(inner_registration->m_class_object); 

.