Remove SWIGTYPE from Generated Class name

白昼怎懂夜的黑 提交于 2019-12-20 05:26:16

问题


Is there anyway to remove the SWIGTYPE part from the generated class names and replace with another string literal?

i.e. change SWIGTYPE_p_ex_session.java to ex_session.java (strip off generated "SWIGTYPE_p_")

SWIG .i file:

%module Example
%{
#include "ExampleApi.h"
struct ex_session{};
%}

%include "ExampleApi.h"

ExampleApi.h contains the below:

typedef struct ex_session session_t;

回答1:


Assuming the real issue here is the "ugly name" then I'd solve this one by making sure SWIG has at least an empty definition (not just a declaration) visible when it's generating the wrapper, for example given:

%module test

class ExampleNiceName;

typedef ExampleNiceName example_t;

void func(example_t);

SWIG generates SWIGTYPE_p_ExampleNiceName.java

We can make this a whole lot better just by doing something like:

class ExampleNiceName {};

instead of

class ExampleNiceName;

in this example, which causes SWIG to generate ExampleNiceName.java as a wrapped type instead. It doesn't expose anything more than previously exposed and is perfectly legal/sane. We're telling SWIG not just "this type exists", but "this type exists and we'd like you to wrap nothing beyond it's name" by doing this.

You can also use %rename to make the name on the Java side different from the C++ class name, for example:

%rename(JavaClassName) ExampleNiceName;
class ExampleNiceName {};

with the previous example would cause JavaClassName to be used in place of ExampleNiceName in the generated Java.



来源:https://stackoverflow.com/questions/8189929/remove-swigtype-from-generated-class-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!