你可以痛饮的boost ::可选<&GT ;? [英] Can you SWIG a boost::optional<>?

查看:180
本文介绍了你可以痛饮的boost ::可选<&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用痛饮成功地构建一个包装器界面,使在C#中使用我的C ++库。最近我暴露了一些的boost ::可选<> 对象和痛饮是与他们有问题。是否有处理这个标准的方式吗?一定有人以前碰到这个...

I've been using SWIG successfully to build a wrapper interface to make my C++ libraries available in C#. Recently I exposed some boost::optional<> objects and SWIG is having problems with them. Is there a standard way to deal with this? Someone must have run into this before...

推荐答案

由于痛饮不理解升压型,typemaps必须写。下面是升压一对typemaps的::可选< INT方式>

Since SWIG doesn't understand boost types, typemaps have to be written. Here's a pair of typemaps for boost::optional<int>.

从Python中,或整数可以传递给函数:

From Python, None or an integer can be passed into a function:

%typemap(in) boost::optional<int> %{
    if($input == Py_None)
        $1 = boost::optional<int>();
    else
        $1 = boost::optional<int>(PyLong_AsLong($input));
%}

一个返回的boost ::可选< INT> 将被转换为无或Python的整数:

A returned boost::optional<int> will be converted to a None or a Python integer:

%typemap(out) boost::optional<int> %{
    if($1)
        $result = PyLong_FromLong(*$1);
    else
    {
        $result = Py_None;
        Py_INCREF(Py_None);
    }
%}

这篇关于你可以痛饮的boost ::可选&LT;&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆