How to unrevel a SWIG data type

Viewed 44

I am writing C# scripts using Velocity API to automate the clinical trials' time-consuming repetitive procedures. Velocity API is a library from Varian Medical Systems that was originally written in Python language. It was made available to C# developers through SWIG functions. Unluckily, they did not make a good job. At present, I am stuck with a data type that is not recognized by the C# compiler, namely: pair <bool, vector> "pair is a native data type for python and C++ but it is not a recognized data type by the C# compiler.

Through Visual Studio debugger, I browsed through Velocity.dll assembly. I could find the wrapped data type corresponding to the one showed above is: SWIGTYPE_p_std__pairT_bool_std__vectorT_vsc__VectorR2d_t_t If I use this latter data type in my C# script, then the C# does not complain. I can get an executable that is useless as I need to get access to the single items of the data pair returned by the method "getStructureHistognam". Unluckily the SWIGTYPE data type does not support any C# method to extract the single items. Nor does it support indexing, as a vector would.

My question for you is: how can I unravel a variable of type SWIGTYPE_p_std__pairT_bool_std__vectorT_vsc__VectorR2d_t_t and get access to each one of the two paired data?

1 Answers

SWIG generates opaque pointers for types it doesn't understand. They can be returned and passed to other functions, but not inspected. You would need to modify the SWIG .i source file to recognize the type by either writing appropriate typemaps or including appropriate template instantiations via #include <std_pair.i> and #include <std_vector.i>.

Here's a basic .i file that reveals a std::pair<bool,std::vector<int>> as an example:

%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}};
}
%}

My C# is rusty but building this file with Python works as shown below. You would use swig -csharp -c++ test.i to generate the wrapper, but I'm using swig -python -c++ test.i here:

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