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

时间:2012-11-06 08:34:23

标签: java c++ swig typedef proxy-classes

我有一些代码如下:

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;

现在我想使用SWIG生成一个Java包装器,但它只生成一个代理类' SingleValue',我想知道如何使用swig生成另一个代理类,但我可以'在谷歌搜索后找到一些相关信息。

我尝试%重命名,但它只生成一个代理类,而不是三个。

1 个答案:

答案 0 :(得分:1)

你想要实现的目标似乎是强力打字。默认情况下,SWIG会尝试使用与您在C ++中看到的行为相同的行为来公开接口,因此在这种情况下行为是预期的 - 弱typedef都是C ++提供的。你可以解决这个问题。

给定头文件:

#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(); }

与你的不同之处仅在于对getValue()的修正以及添加两个用于测试的内联函数,我们可以通过执行以下操作来包装它并接近强大的typedef语义:

%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();

请注意,我根本没有向SWIG显示typedef,而且我完全谎称RoadType等的类型和存在,但是这样就可以了,因为SWIG生成的所有代码都是如此是合法和正确的。

这会导致生成一个接口,其中make_X函数返回不同的类型。


如果你想避免头文件和接口文件之间的重复,你可以引入一个宏来帮助,头文件变成:

#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(); }

这意味着接口文件可以简单地变为:

%module test

%{
#include "test.h"
%}

%include <std_string.i>

#define STRONG_TYPEDEF(o, n) struct n : o {};

%include "test.h"

这部分起作用是因为SingleValue是一个类,因此强类型定义可以成为Java类型系统中的子类,以便强制执行检查。如果类型不是类,你仍然可以在不使用继承的情况下做同样的事情,例如the first part of an answer I gave on a similar problem使用D会起作用 - 你想要详细说明空结构但是如果没有任何继承播放。

相关问题