[英]How to generate two proxy classes using the same interface in swig
I have some code like below: 我有一些类似下面的代码:
class SingleValue
{
public:
SingleValue()
{}
~SingleValue()
{}
const std::string& getValue()const
{
return m_nSingleValue;
}
private:
int m_nSingleValue;
};
typedef SingleValue RoadType;
typedef SingleValue RoadSubType;
typedef SingleValue FunctionalClass;
now I want to use SWIG to generate a Java wrapper, But it generate only one proxy class 'SingleValue', I want to know how generate the other proxy class using swig, but I can't find some relative information after googling. 现在我想使用SWIG生成Java包装器,但是它仅生成一个代理类'SingleValue',我想知道如何使用swig生成另一个代理类,但是在谷歌搜索后找不到一些相关信息。
I try %rename, but it only generate one proxy class, not three. 我尝试%rename,但是它只生成一个代理类,而不是三个。
What you're trying to achieve seems to be strong typing. 您想要实现的目标似乎是强类型化。 SWIG by default tries to expose an interface with the same behaviours as you would see in C++, so in that case the behaviour is expected - weak typedefs are all C++ offers.
默认情况下,SWIG尝试公开具有与C ++中相同行为的接口,因此在这种情况下,这种行为是可以预期的-C ++都提供弱类型定义。 You can work around this though.
您可以解决此问题。
Given the header file: 给定头文件:
#include <string>
class SingleValue
{
public:
SingleValue()
{}
~SingleValue()
{}
std::string getValue() const
{
return std::to_string(m_nSingleValue);
}
private:
int m_nSingleValue;
};
typedef SingleValue RoadType;
typedef SingleValue RoadSubType;
typedef SingleValue FunctionalClass;
inline RoadType make_road() { return RoadType(); }
FunctionalClass make_func() { return FunctionalClass(); }
which differs from yours only in the corrections to getValue()
and the addition of two inline functions for testing we can wrap it and get close to strong typedef semantics by doing: 这与您的区别仅在于对
getValue()
的更正以及两个用于测试的内联函数的添加,我们可以包装它并通过执行以下操作来接近强类型定义语义:
%module test
%{
#include "test.h"
%}
%include <std_string.i>
class SingleValue
{
public:
SingleValue();
~SingleValue();
std::string getValue() const;
};
struct RoadType : public SingleValue {
};
struct RoadSubType : public SingleValue {
};
struct FunctionalClass : public SingleValue {
};
RoadType make_road();
FunctionalClass make_func();
Notice that I've not shown SWIG the typedef at all and I've lied completely about the type and existance of RoadType
etc., but this is OK like that because all the code that gets generated by SWIG is legal and correct still. 请注意,我根本没有显示SWIG的typedef,我完全对
RoadType
的类型和存在作了RoadType
,但这是可以的,因为SWIG生成的所有代码仍然合法且正确。
This cause an interface to be generated where the make_X
functions return distinct types. 这将导致生成一个接口,其中
make_X
函数将返回不同的类型。
If you wanted to avoid the duplication between the header file and the interface file you could introduce a macro to help, the header file becomes: 如果要避免头文件和接口文件之间的重复,可以引入一个宏来帮助您,头文件将变为:
#include <string>
class SingleValue
{
public:
SingleValue()
{}
~SingleValue()
{}
std::string getValue() const
{
return std::to_string(m_nSingleValue);
}
private:
int m_nSingleValue;
};
#ifndef STRONG_TYPEDEF
#define STRONG_TYPEDEF(o,n) typedef o n
#endif
STRONG_TYPEDEF(SingleValue, RoadType);
STRONG_TYPEDEF(SingleValue, RoadSubType);
STRONG_TYPEDEF(SingleValue, FunctionalClass);
inline RoadType make_road() { return RoadType(); }
FunctionalClass make_func() { return FunctionalClass(); }
Which means the interface file can simply become: 这意味着接口文件可以简单地变成:
%module test
%{
#include "test.h"
%}
%include <std_string.i>
#define STRONG_TYPEDEF(o, n) struct n : o {};
%include "test.h"
This works partly because SingleValue
is a class so the strong typedef can become a sub-class in the Java type system for it to enforce checking on. 这部分起作用是因为
SingleValue
是一个类,因此强类型定义可以成为Java类型系统中的子类,以便其强制执行检查。 If the type isn't a class you can still do the same thing without using inheritance, for example the first part of an answer I gave on a similar problem using D would work - you'd want to elaborate on the empty structs though if there isn't any inheritance in play. 如果类型不是类,您仍然可以在不使用继承的情况下做同样的事情,例如,我使用D 解决类似问题的答案的第一部分会起作用-您可能想详细说明空结构游戏中没有任何继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.