你能SWIG提升::可选<>吗?
Can you SWIG a boost::optional<>?
我已经成功地使用SWIG构建了一个包装器接口,使我的c++库在c#中可用。最近我暴露了一些boost::optional<>
对象和SWIG有问题。有没有一个标准的方法来处理这个问题?
由于SWIG不理解boost类型,因此必须编写类型映射。这是boost::optional<int>
的一对类型映射。
在Python中,可以将None
或整数传递给函数:
%typemap(in) boost::optional<int> %{
if($input == Py_None)
$1 = boost::optional<int>();
else
$1 = boost::optional<int>(PyLong_AsLong($input));
%}
返回的boost::optional<int>
将被转换为None或Python整数:
%typemap(out) boost::optional<int> %{
if($1)
$result = PyLong_FromLong(*$1);
else
{
$result = Py_None;
Py_INCREF(Py_None);
}
%}
一个可能的c#解决方案,使用std::vector
#if SWIGCSHARP
// C++
%typemap(ctype) boost::optional<int32_t> "void *"
%typemap(out) boost::optional<int32_t> %{
std::vector<int32_t> result_vec;
if (!!$1)
{
result_vec = std::vector<int32_t>(1, $1.get());
}
else
{
result_vec = std::vector<int32_t>();
}
$result = new std::vector< uint32_t >((const std::vector< uint32_t > &)result_vec);
%}
// C#
%typemap(imtype) boost::optional<int32_t> "global::System.IntPtr"
%typemap(cstype) boost::optional<int32_t> "int?"
%typemap(csout, excode=SWIGEXCODE) boost::optional<int32_t> {
SWIG_IntVector ret = new SWIG_IntVector($imcall, true);$excode
if (ret.Count > 1) {
throw new System.Exception("Return vector contains more then one element");
}
else if (ret.Count == 1) {
return ret[0];
}
else {
return null;
}
}
#endif //SWIGCSHARP
相关文章:
- Python 3.7 和 excess_args 的 SWIG 问题
- EASTL矢量<向量<int>>连续的
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 为重写std::exception的库生成swig接口时出错
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- 在模板化函数中重新定义(忽略)的 SWIG 标识符
- 使用 swig 追加到字节数组
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- Java SWIG wrapper vs direct function calling
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- 类型为 'std::map< char,int > 的 <Swig 对象的代理
- 使用SWIG从C++回调到C#
- 从 C++ 到 Python 的 SWIG:未定义的符号导入问题
- 如何使用 swig C++命名空间作为 python 模块公开
- 分解SWIG Python接口 - 容器会产生命名空间冲突
- 在自定义类型图中重用 SWIG 映射
- 使用 Swig 在成员中包装具有互斥锁的 C++ 类时出现问题