如何完美地为Python包装C ++枚举?

时间:2019-05-03 14:14:05

标签: python c++ boost-python

我在boost.python中找到了Enum wrap方法,但是并不能解决我的问题。

请查看代码:

// wrap.h
#include "./caler.h"
#include "./order_side.h"
#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(caler) { 
  class_<CALER>(
      "CALER", init<std::string>())
    .def("CalNetPnl", &CALER::CalNetPnl)
  enum_<OrderSide::Enum>("OrderSide")
    .value("Buy", OrderSide::Buy)
    .value("Sell", OrderSide::Sell);
}
// all i want is to wrap OrderSide::Enum for python
// the function CalNetPnl uses OrderSide::Enum as its parmeter


// a.py
from caler import *

cl = CALER()
print(cl.CalNetPnl("ni1904", 100.0, 1, 99.0, OrderSide.Buy))

// Caler.cpp
double CALER::CalNetPnl(std::string ticker, float open_price, int open_size, float close_price, int close_size, OrderSide::Enum close_side) {
  if (open_size != close_size) {
    printf("opensize and closesize not equal %d %d\n", open_size, close_size);
    open_size = close_size;
  }
  double gross_pnl = CalPnl(ticker, open_price, open_size, close_price, close_size, close_side);
  Fee fee = CalFee(ticker, open_price, open_size, close_price, close_size);
  double total_fee = fee.open_fee + fee.close_fee;
  return gross_pnl - total_fee;
}

错误消息是:

Boost.Python.ArgumentError: Python argument types in
CALER.CalNetPnl(CALER, str, float, int, float, int, OrderSide.Buy)
did not match C++ signature:
CalNetPnl(CALER {lvalue}, std::string, double, int, double, int, OrderSide::Enum)

我认为OrderSide.BuyOrderSide::Buy,订单边是struct OrderSide {枚举{Unknown,Buy,Sell}}; 有人可以帮忙吗?

0 个答案:

没有答案