[英]Swig C++ to C#: How to wrap classes from C++ to make methods from template class available in derived class in C#?
I have template class BaseMyPoint in namespace external::internal我在命名空间 external::internal 中有模板类 BaseMyPoint
It implements public methods X(), Y(), Z() and SetCoord().它实现了公共方法 X()、Y()、Z() 和 SetCoord()。
Then write然后写
typedef internal::BaseMyPoint MyPointd; typedef internal::BaseMyPoint MyPointd;
Create derived class MyPoint, that inherits MyPointd with public创建派生类 MyPoint,继承 MyPointd 与 public
Description of both classes is stored in a file "myPoint.h":两个类的描述都存储在文件“myPoint.h”中:
#pragma once
#ifdef MYPOINTSDLL_EXPORTS
#define MYPOINTSDLL_API __declspec(dllexport)
#else
#define MYPOINTSDLL_API __declspec(dllimport)
#endif
namespace external {
namespace internal {
template<typename T>
struct MyPointTraits;
template<>
struct MyPointTraits<double>
{
typedef double ValueType;
static ValueType CoincidenceTolerance() { return 1e-7; }
};
template<>
struct MyPointTraits<float>
{
typedef float ValueType;
static ValueType CoincidenceTolerance() { return 1e-7f; }
};
template<typename T>
class BaseMyPoint
{
public:
T myX;
T myY;
T myZ;
typedef typename MyPointTraits<T>::ValueType ValueType;
BaseMyPoint() {}
BaseMyPoint(ValueType theX, ValueType theY, ValueType theZ) :
myX(theX), myY(theY), myZ(theZ) {}
BaseMyPoint(const BaseMyPoint& theOther) :
myX(theOther.myX), myY(theOther.myY), myZ(theOther.myZ) {}
ValueType X() const { return myX; }
ValueType& X() { return myX; }
ValueType Y() const { return myY; }
ValueType& Y() { return myY; }
ValueType Z() const { return myZ; }
ValueType& Z() { return myZ; }
void SetCoord(ValueType theX, ValueType theY, ValueType theZ)
{
X() = theX;
Y() = theY;
Z() = theZ;
}
};
}
typedef internal::BaseMyPoint<double> MyPointd;
typedef internal::BaseMyPoint<float> MyPointf;
class MyPoint : public MyPointd
{
public:
MyPoint() {}
MyPoint(const MyPointd& theOther) : MyPointd(theOther) {}
};
}
Write in interface file "myPoint.i":写入接口文件“myPoint.i”:
%module myPointsWrapper
%{
#include "myPoint.h"
using namespace external;
using namespace external::internal;
%}
%include <windows.i>
%include "myPoint.h"
In command line I write: C:\\swig -csharp -c++ -namespace pointspase -outdir C:\\myPoints\\myPointcs\\Generated myPoint.i在命令行我写: C:\\swig -csharp -c++ -namespace pointspase -outdir C:\\myPoints\\myPointcs\\Generated myPoint.i
In C# we referring to these methods ( X(), Y(), Z(), SetCoord() ) through an instance MyPoint aPoint:在 C# 中,我们通过实例 MyPoint aPoint 引用这些方法( X(), Y(), Z(), SetCoord() ):
using System;
using pointspase;
namespace myPointcs
{
class Program
{
static void Main(string[] args)
{
MyPoint aPoint = new MyPoint();
double x = 0.2, y = 0.3, z = 0.4;
aPoint.SetCoord(x, y, z);
double X = aPoint.X(), Y = aPoint.Y(), Z = aPoint.Z();
}
}
}
And I have我有
Error CS1061 'MyPoint' does not contain a definition for 'Z' and no extension method 'Z' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)错误 CS1061“MyPoint”不包含“Z”的定义,并且找不到接受“MyPoint”类型的第一个参数的扩展方法“Z”(您是否缺少 using 指令或程序集引用?)
Error CS1061 'MyPoint' does not contain a definition for 'Y' and no extension method 'Y' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)错误 CS1061“MyPoint”不包含“Y”的定义,并且找不到接受“MyPoint”类型的第一个参数的扩展方法“Y”(您是否缺少 using 指令或程序集引用?)
Error CS1061 'MyPoint' does not contain a definition for 'X' and no extension method 'X' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)错误 CS1061“MyPoint”不包含“X”的定义,并且找不到接受“MyPoint”类型的第一个参数的扩展方法“X”(您是否缺少 using 指令或程序集引用?)
Error CS1061 'MyPoint' does not contain a definition for 'SetCoord' and no extension method 'SetCoord' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)错误 CS1061“MyPoint”不包含“SetCoord”的定义,并且找不到接受“MyPoint”类型的第一个参数的扩展方法“SetCoord”(您是否缺少 using 指令或程序集引用?)
How to make these methods available in the C# class MyPoint?如何使这些方法在 C# 类 MyPoint 中可用?
Thanks in advance for your attention, Kirill预先感谢您的关注,基里尔
EDIT 1编辑 1
Here is the simple example that clearly expresses my problem.这是一个简单的例子,清楚地表达了我的问题。
In "file.h" I write在“file.h”我写
template <typename T>
class myclass {
public:
T get() const { return 0; }
};
class myintclass : public myclass<int> {};
In "file.i" I write在“file.i”我写
%template (myclassi) myclass<int>
When I compile the interface file I have warnings:当我编译接口文件时,我有警告:
warning 401: Base class 'myclass< int >' undefined.
warning 401: 'myclass< int >' must be defined before it is used as a base class.
As a result, C# class myintclass does not contain method get().因此,C# 类 myintclass 不包含方法 get()。 How can I change the interface file to get () method has been made available from the class myintclass?
如何更改接口文件以从类 myintclass 中获得 get() 方法?
Given file.h
in your edit, it is a bit tricky to declare everything correctly.鉴于您编辑中的
file.h
,正确声明所有内容有点棘手。 Ideally the template should be in a separate header than the myintclass
definition, but here's a tested way to do it:理想情况下,模板应位于与
myintclass
定义不同的标头中,但这是一种经过测试的方法:
%module test
%{
#include "file.h"
%}
// Process the template first
template <typename T>
class myclass {
public:
T get() const { return 0; }
};
// Tell SWIG to generate code for a template instance.
%template(myclassi) myclass<int>;
// Now that SWIG has code for the template instance,
// SWIG can generate code for classes using the template instance.
class myintclass : public myclass<int> {};
Testing the module (I generated for Python):测试模块(我为 Python 生成):
>>> import test
>>> t=test.myclassi()
>>> t.get()
0
>>> t=test.myintclass()
>>> t.get()
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.