2014-12-15 2 views
2

raw_function을 사용하여 클래스 메서드를 노출하려고합니다. 같은 뭔가 : 지금부스트 파이썬 raw_function 메서드

namespace py = boost::python; 

class MyObject { 
public: 
    py::object foo(py::tuple args, py::dict kwargs) { 
     // whatever 
    } 
}; 

, 나는 밖으로 self을 당겨하는 정적 기능을 래핑하는 raw_function을 사용할 수 있습니다, 그래서 뭔가 같은 : 작동하지만, 꽤 장황

static py::object MyObject_foo(py::tuple args, py::dict kwargs) { 
    MyObject& self = py::extract<MyObject&>(args[0]); 

    // the docs allege that I can do args.slice(1), but that doesn't 
    // compile on boost 1.55 at least, with no matching function 
    // call to 'boost::python::tuple::slice(int) const 
    self.foo(py::extract<py::tuple>(args.slice(1, py::len(args))), 
      kwargs); 
} 

py::class_<MyClass>("MyClass") 
    .def("foo", py::raw_function(&MyObject_foo, 1)); 

. 나는 실제로 랩핑이 필요하고 각각의 중간 단계를 거치지 않는 것을 선호하는 몇몇 원시 함수를 가지고있다. MyObject::foo을 줄이는 더 짧은 방법이 있습니까?

답변

1

당신은 즉,

namespace py = boost::python; 

class MyObject { 
public: 
    static py::object foo(py::tuple args, py::dict kwargs) 
    { 
     MyObject& self = py::extract<MyObject&>(args[0]); 
     // Do stuff with the self object 
    } 
}; 

py::class_<MyObject>("MyObject") 
    .def("foo", py::raw_function(&MyObject::foo, 1)); 

그것은 매우 직접 수업 방법을 포장 아니에요, 같은 방법으로 self을 추출 후, 정적 멤버 함수를 사용하여 추가 기능을 건너 뛸 수 있지만 중간 기능을 사용하는 것보다 청소기입니다 .

+0

내 문제가 해결되었지만 여전히 "클래스 내"입니다. 감사! – wgodoy