【问题标题】:How to unrevel a SWIG data type如何解开 SWIG 数据类型
【发布时间】:2022-02-03 12:59:37
【问题描述】:

我正在使用 Velocity API 编写 C# 脚本来自动化临床试验中耗时的重复程序。 Velocity API 是 Varian Medical Systems 的一个库,最初是用 Python 语言编写的。它通过 SWIG 函数提供给 C# 开发人员。不幸的是,他们没有做好工作。目前,我遇到了 C# 编译器无法识别的数据类型,即: 对 "pair 是 python 和 C++ 的本机数据类型,但它不是 C# 编译器可识别的数据类型。

通过 Visual Studio 调试器,我浏览了 Velocity.dll 程序集。我可以找到对应于上面显示的包装数据类型是: SWIGTYPE_p_std__pairT_bool_std__vectorT_vsc__VectorR2d_t_t 如果我在我的 C# 脚本中使用后一种数据类型,那么 C# 不会抱怨。我可以获得一个无用的可执行文件,因为我需要访问“getStructureHistognam”方法返回的数据对的单个项目。不幸的是,SWIGTYPE 数据类型不支持任何 C# 方法来提取单个项目。它也不像向量那样支持索引。

我的问题是: 如何解开 SWIGTYPE_p_std__pairT_bool_std__vectorT_vsc__VectorR2d_t_t 类型的变量 并访问两个配对数据中的每一个?

【问题讨论】:

    标签: python c# swig


    【解决方案1】:

    SWIG 为它不理解的类型生成不透明的指针。它们可以返回并传递给其他函数,但不能检查。您需要修改 SWIG .i 源文件以通过编写适当的类型映射或通过 #include <std_pair.i>#include <std_vector.i> 包含适当的模板实例来识别类型。

    这是一个基本的 .i 文件,其中显示了 std::pair<bool,std::vector<int>> 作为示例:

    %module test
    
    %include <std_pair.i>    // SWIG support for std::pair templates
    %include <std_vector.i>  // SWIG support for std::vector templates
    %template(IntVector) std::vector<int>;  // Support this template instantiation
    %template(BoolIntVectorPair) std::pair<bool,std::vector<int>>; // and this one
    
    %inline %{
    #include <utility>
    #include <vector>
    
    std::pair<bool,std::vector<int>> get() {
        return std::pair<bool,std::vector<int>>{true,{1,2,3}};
    }
    %}
    

    我的 C# 生锈了,但是用 Python 构建这个文件如下所示。你会使用swig -csharp -c++ test.i 来生成包装器,但我在这里使用swig -python -c++ test.i

    >>> import test
    >>> test.get()
    (True, (1, 2, 3))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      • 2019-06-25
      • 2012-02-12
      相关资源
      最近更新 更多