BOOST_PYTHON_IMPORT
Şöyle yaparız.
class_ Sınıfı
C++ kodunu python'dan çağırmak için kullanılır
Constructor
Elimizde şöyle bir sınıf olsun
Açıklaması şöyle. C++ kodunda & kullanılsa bile nesnelerin copy constructor metodunun çağrılmasını sağlar.
Şöyle yaparız.
class A {
    ... // constructor is omitted 
public:
    const std::string str1;
};
class B {
public:
    std::shared_ptr<A> a;
}
BOOST_PYTHON_IMPORT(wrapped) {
    class_<A, std::shared_ptr<A>>("APy")
    .def_readonly("str1", &A::str1);
    class_<B>("BPy")
    .def_readwrite("a", &B::a);
}import wrapped as wr
b = wr.BPy()
s1 = b.a.str1 // APy wrapper created
s2 = b.a.str1 // new APy wrapper created even though object is the sameclass_ Sınıfı
C++ kodunu python'dan çağırmak için kullanılır
Constructor
Elimizde şöyle bir sınıf olsun
class Example
{
public:
  Example()
  {
    std::cout << "hello\n";
  }
  Example(const Example& e)
  {
    std::cout << "copy\n";
    counter++;
  }
  ~Example()
  {
    std::cout << "bye\n";
  }
  Example& count()
  {
    std::cout << "Count: " << counter << std::endl;
    return *this;
  }
  static int counter;
};
int Example::counter = 0;class_<Example>("Example", init<>())Açıklaması şöyle. C++ kodunda & kullanılsa bile nesnelerin copy constructor metodunun çağrılmasını sağlar.
Şöyle yaparız.copy_non_const_reference is a model of ResultConverterGenerator which can be used to wrap C++ functions returning a reference-to-non-const type such that the referenced value is copied into a new Python object.
usingclass_ c;
c.def("count", &Example::count,
 return_value_policy<copy_non_const_reference>());obj=Example()
obj.count().count() 
 
Hiç yorum yok:
Yorum Gönder