c++ - element typemap + stl_vector.i typemap + ??? --> wrapped function taking list of elems -
Assume that I have a arbitrary non-trialled type of a
for which I write typemaps Can i Specifically, suppose I have to convert std :: string
to A
and type in strings in stl_vector.i
and % templates (AVector) std :: vector & lt; A & gt; I'm importing
.
One of the fastest / simple (or even 'right') void func (const std :: vector & lt; a & gt; and vals)
The process of action in SWIG to create the cover where the target language is the list of expected inputs on the side of strings (like ['a', 'qqq']
in Python)? Unless I am not wrong, it just does not appear to work (in particular, anywhere in the generated code code I have not seen the typemap code for A
).
If you have a specific target language to answer , then say that it is Python. I am able to do this in such a general case, though ... Assume that we have a header file that looks like:
play with a list of strings, or < Code> std :: vector & lt; a & gt; Given that language agnosticism is a stated goal that the best way to obtain it is to help with some types of conversions and overload Starting some more C ++.
We start the initial file You can wrap something simply:
% module exam% contains & lt; std_vector.i & gt;% contains & lt; std_string.i & gt;% {#include "test.h"%}% templates (AVAC) std :: vector & lt; A & gt;; which is enough to run the first half of our Python Test case: < Code> import test test A ("") V1 = test.AVec (2, test.A ("test")) print type (v1) test.run (v1) # There is some more work needed for this However ... v2 = ["Hello", "world", "is not", "this", "fun"] print type (v2) test.run (V2) To begin working with the second part of this test, we need to customize the SWIG interface, I add a function, internal to the wrapper and a surcharge which std :: vector & lt; Std :: string & gt; Takes advantage of SWIG's default typemaps for
:: vector & lt; A & gt; Convert (const std :: vector & lt; std :: string & gt; & amp; in) {std :: vector & lt; A & gt; Retired; Std :: copy (in.begin (), in.end (), std :: back_inserter (ret)); Return writ; }%}% Inline% {// A surcharge zero run (const std :: vector & lt; std: string & gt; & amp; in) {run (convert (in)); }%}
At the end of the initial SWIG interface we have added a surcharge which works to provide std :: vector
in the target language. (Which I have seen that most).
With the version of SWIG I have tested myself with warning as well as running SWIG A (fake) warnings about covering overloads This warning may be incorrect because the generated code Has actually taken the correct overload based on the given logic.
Using the surcharge added in the interface file with the % inline
is the fastest / easiest way to get it up and running. It is important to type the copy / conversion overhead and per function manually. To do this it is necessary to write a single typemap with a typo, but it will be very difficult to do this in a way that is independent of the target language.
Comments
Post a Comment