简体   繁体   English

如何在Swig中使用相同的接口生成两个代理类

[英]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.

相关问题 如何从C ++生成SWIG接口? - How to generate SWIG interface from C++? 如何给两个不同的类相同的接口? - How to give two different classes the same interface? 如何在Spring 3中使用纯注释从接口生成代理类? - How to generate proxy class from interface using pure annotations in Spring 3? 如何分离实现相同接口的两个类的函数模拟? - How to separate function mocks of two classes that implement the same interface? 如何为实现相同接口的两个类制作双向适配器? - How to make bidirectional adapter for two classes that implement the same interface? 如何使用接口在Java中的两个类之间进行通信? - How is it possible to communicate between two classes in Java using an interface? 无法弄清楚如何使SWIG / Java强制一个代理类来实现一个接口 - can't figure out how to make SWIG/Java force a proxy class to implement an interface 如何注入实现同一接口的两个不同类的两个实例? - How to inject two instances of two different classes which implement the same interface? 使用Java反射找出两个类是否具有相同类型(类,接口,枚举等) - Find out if two classes are of the same type (Class, Interface, Enum, etc) using java reflect 如何注册实现相同接口的类的对象 - How to register objects of classes implementing the same interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM