人气 190

[游戏程序] 让你的python脚本支持boost::any的格式。 [复制链接]

九艺网 2017-3-10 17:01:59

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x

当你有个C++函数:
boost::any Get(std::string)
{
     .....
}
void Set(std::string, boost::any)
{
    .....
}
这样的两个API要传给python来执行的话,当你运行
a.set('hehe', 1);
返回的是类型不匹配。那么如何来修改boost.python,让它支持boost::any的格式呢,大家看过来:
首先打开你boost目录下的boost/python/converter/builtin_converters.hpp
在namespace converter里加上一句:
BOOST_PYTHON_DECL PyObject* do_return_to_python(boost::any);
然后加一句宏定义:
BOOST_PYTHON_TO_PYTHON_BY_value(boost::any, converter::do_return_to_python(x))
然后打开libs/python/src/converter/builtin_converters.cpp加上一段:
BOOST_PYTHON_DECL PyObject* do_return_to_python(boost::any x)
{
if(x.type() == typeid(int))
return PyInt_FromLong(any_cast(x));
if(x.type() == typeid(float))
return PyFloat_FromDouble(any_cast(x));
if(x.type() == typeid(double))
return PyFloat_FromDouble(any_cast(x));
try{
std::string str = any_cast(x);
return PyString_FromStringAndSize(str.data(),implicit_cast(str.size()));
} catch (bad_any_cast &)
{
return PyInt_FromLong(0);
}
                // todo, 新加对list,map,vector等的支持
}
ok, bjam一下,发现get好使了,呵呵。。然后来看set如何让它好使,set所需要的是python to c++的类型转换:
在libs/python/src/converter/builtin_converters.cpp里找到initialize_builtin_converters()这个函数,在里头加上一句:
slot_rvalue_from_python();
然后在相同的文件里加上一个函数:
  struct any_rvalue_from_python
  {
  static unaryfunc *get_slot(PyObject *obj)
  {
      if(PyInt_Check(obj) || PyString_Check(obj) || PyFloat_Check(obj))
return &py_object_identity;
  return 0;
  }
  static boost::any extract(PyObject *obj)
  {
  boost::any res = boost::any();
  if(PyInt_Check(obj))
  {
  int a = PyInt_AS_LONG(obj);
  res = a;
  }
  if(PyFloat_Check(obj))
  {
  float f = (float)PyFloat_AS_DOUBLE(obj);
  res = f;
  }
  if(PyString_Check(obj))
  {
  std::string str(PyString_AsString(obj),PyString_Size(obj));
  res = str;
  }
                                  // todo, 新增对map,list,vector等的支持
  return res;
  }
  };
OK,你会发现set也好使了,呵呵,等把对map,list,vector的支持全做完,就打算把这个修改提交到boost的maillist里了:)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|小黑屋|九艺游戏动画论坛 ( 津ICP备2022000452号-1 )

GMT+8, 2024-4-20 12:29 , Processed in 0.096590 second(s), 23 queries .

Powered by Discuz! X3.4  © 2001-2017 Discuz Team.