Classes

Constructor 构造函数

多个构造函数

使用def 添加第二个及以后的Constructor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  #include <boost/python.hpp>
  using namespace boost::python;

  // 单个构造函数
  BOOST_PYTHON_MODULE(hello)
  {
class_<World>("World", init<std::string>())
.def("greet", &World::greet)
.def("set", &World::set)
;
  }

  // 多个构造函数
  class_<World>("World", init<std::string>())
  .def(init<double, double>())
  .def("greet", &World::greet)
  .def("set", &World::set)
  ;

禁用构造函数

使用no_init

1
class_<Abstract>("Abstract", no_init)

数据成员

  • 普通类型

    • def_readwrite
    1
    2
    3
    
    int a; // public
    
    def_readwrite("a", &World::a);
  • const 类型

    • def_readonly

      1
      2
      3
      
      const int a; // public
      
      def_readonly("a", &World::a);

属性函数(getter, setter)

  • 直接指定getter, setter即可

    • 使用add_property

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      struct Num
      {
      Num();
      float get() const;
      void set(float value);
      
      };
      
      
      class_<Num>("Num")
      .add_property("rovalue", &Num::get)
      .add_property("value", &Num::get, &Num::set);
      • 注意
  • 要么只有getter
  • 要么getter, setter都有

继承问题

普通继承

直接声明继承关系即可

  • class_<Derived, bases<Base>>(…)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  // 继承关系类
  struct Base { virtual ~Base(); };
  struct Derived : Base {};

  // 调用基类与子类
  void b(Base*);
  void d(Derived*);
  Base* factory() { return new Derived; }



  // Boost.Python声明继承关系
  class_<Base>("Base")
/*...*/
;
  class_<Derived, bases<Base>>("Derived")
// ...
;

  // 声明调用类的函数
  def("b", b);
  def("d", d);
  def("factory", factory,
return_value_policy<manage_new_object>());

多态与虚函数

非纯虚函数(有定义default implementations)
  • 在纯虚函数的基础上在加一些修饰

    • override f = this->get_override("f") 判断是否是子类调用重载函 数
1
2
3
4
5
6
7
8
9
  // 1) 获取重载的情况,即子类的调用情况
  if (override f = this->get_override("f"))
    // for unix like
    return f();
    // for windows
    return call<char const*>(f.ptr());

  // 2) 非重载的情况,即当前基类Base 的默认定义f
  return this->f();
  • 同时保留原来的版本default_f
  • Boost.Python 添加 非纯虚函数的特殊格式

    1
    2
    3
    
    	 class_<BaseWrap, boost::noncopyable>("Base")
    .def("f", &Base::f, &BaseWrap::default_f)
    ;
  • 完全实例

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    // * 抽象类
    struct Base
    {
    virtual ~Base() {}
    
    // 纯虚函数
    virtual int f() { return 0; };
    };
    
    // * 包装方法
    struct BaseWrap : Base, wrapper<Base>
    {
    int f()
    {
    
     // 1) 获取重载的情况,即子类的调用情况
     if (override f = this->get_override("f"))
    // for unix like
    return f();
    // for windows
    return call<char const*>(f.ptr());
    
     // 2) 非重载的情况,即当前基类Base 的默认定义f
     return this->f();
    
    }
    
    
    // 存储默认定义
    int default_f()
    {
     return this->Base::f();
    }
    };
    
    
    // * Boost.Python 声明抽象类的方法
    //   同时暴露(exposing) Base::f 和 &BaseWrap::default_f, 这是Boost.Python 的一种特例
    class_<BaseWrap, boost::noncopyable>("Base")
    .def("f", &Base::f, &BaseWrap::default_f)
    ;

运算符重载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  // 原型
  class FilePos { /*...*/ };

  FilePos     operator+(FilePos, int);
  FilePos     operator+(int, FilePos);
  int         operator-(FilePos, FilePos);
  FilePos     operator-(FilePos, int);
  FilePos&    operator+=(FilePos&, int);
  FilePos&    operator-=(FilePos&, int);
  bool        operator<(FilePos, FilePos);

  // Boost.Python 暴露声明
  class_<FilePos>("FilePos")
.def(self + int())          // __add__
.def(int() + self)          // __radd__
.def(self - self)           // __sub__
.def(self - int())          // __sub__
.def(self += int())         // __iadd__
.def(self -= other<int>())
.def(self < self);          // __lt__

Python 的特殊函数

str, abs, pow, float

  • 这些特殊函数,Boost.Python 有规定的形式,因此要按照标准方式来声明
  • 注意

    • 使用输出流运算符 << 实现python str 函数功能
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  class Rational
  { public: operator double() const; };

  Rational pow(Rational, Rational);
  Rational abs(Rational);
  ostream& operator<<(ostream&,Rational);

  class_<Rational>("Rational")
.def(float_(self))                  // __float__
.def(pow(self, other<Rational>))    // __pow__
.def(abs(self))                     // __abs__
.def(str(self))                     // __str__
;