如何创建 SWIG 接口文件? [英] How to create SWIG interface file?

查看:21
本文介绍了如何创建 SWIG 接口文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SWIG 的新手

做事情的时间更少.我正在尝试将 C++ 类绑定到 python.我已经在 Windows 中设置了 SWIG 并尝试运行它.成功了

我的example.i文件就像

/* 文件:example.i */%模块示例%{#define SWIG_FILE_WITH_INIT#include "Item.h"}%#include "Item.h"

但它似乎必须包含或声明头文件类、构造函数、模板等...

谁能建议如何创建 SWIG 接口文件.

以下是我需要创建接口文件的头文件(Item.h).

#ifndef __ITEM_H__#define __ITEM_H__#include <complex>#include <功能>#include <字符串>模板类项目{std::string name_;T val_;民众:项目(std::string name, T val) : name_(name), val_(val) {}项目(项目<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}项目(const 项目<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}〜项目(){}std::string name() const { return name_;}T operator()() const { return val_;}double norm() const { return sqrt(val_ * val_);}};模板<>类 Item>{std::string name_;std::complexval_;民众:项目(std::string name, std::complex val) : name_(name), val_(val) {}项目(项目<std::complex<double>>&rhs):name_(rhs.name_),val_(rhs.val_){}项目(常量项目>&rhs):name_(rhs.name_),val_(rhs.val_){}〜项目(){}std::string name() const { return name_;}std::complex运算符()()const { 返回 val_;}double norm() const { return sqrt(val_.real() * val_.real() + val_.imag() * val_.imag());}};模板struct ItemComparator : public std::binary_function<Item<T>, Item<T>, bool>{内联 bool operator()(Item lhs, Item rhs){返回 lhs.norm() <rhs.norm();}};#万一

解决方案

这是您示例的接口文件.SWIG 内置了对 std::string 和 std::complex 的支持.您必须通过 %template 声明要使用的模板的具体类:

%module 项目%{#include "Item.h"%}%include %include %include "Item.h"%template(Int) Item;%template(Complex) Item>;

像这样使用:

<预><代码>>>>进口商品>>>a=Item.Int('Int1',5)>>>b=Item.Complex('Cplx1',2+3j)>>>一个名字()'Int1'>>>b.name()'Cplx1'>>>a.范数()5.0>>>b.范数()3.605551275463989

I am new to SWIG

And less time to do things. I am trying to bind c++ classes to python. I have set up SWIG in windows and tried running it. It was successful

My example.i file is like

/* File: example.i */
%module example

%{
#define SWIG_FILE_WITH_INIT
#include "Item.h"
}%
#include "Item.h"

But it seems it has to include or declare the header files class, constructor, templates etc...

Can anyone suggest how to create a SWIG interface file.

Follwing are the header file (Item.h) that i need to create interface file.

#ifndef __ITEM_H__
#define __ITEM_H__

#include <complex>
#include <functional>
#include <string>

template<typename T>
class Item
{
  std::string name_;
  T val_;

public:
  Item(std::string name, T val) : name_(name), val_(val) {}
  Item(Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  T operator()() const { return val_; }
  double norm() const { return sqrt(val_ * val_); }
};

template<>
class Item<std::complex<double> >
{
  std::string name_;
  std::complex<double> val_;

public:
  Item(std::string name, std::complex<double> val) : name_(name), val_(val) {}
  Item(Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  std::complex<double> operator()() const { return val_; }
  double norm() const { return sqrt(val_.real() * val_.real() + val_.imag() * val_.imag()); }
};

template<typename T>
struct ItemComparator : public std::binary_function<Item<T>, Item<T>, bool>
{
  inline bool operator()(Item<T> lhs, Item<T> rhs)
  {
    return lhs.norm() < rhs.norm();
  }
};

#endif

解决方案

Here's an interface file for your example. SWIG has built-in support for std::string and std::complex. You must declare the concrete classes of the templates you want to use via %template:

%module Item

%{
#include "Item.h"
%}

%include <std_string.i>
%include <std_complex.i>
%include "Item.h"
%template(Int) Item<int>;
%template(Complex) Item<std::complex<double> >;

Use it like:

>>> import Item
>>> a=Item.Int('Int1',5)
>>> b=Item.Complex('Cplx1',2+3j)
>>> a.name()
'Int1'
>>> b.name()
'Cplx1'
>>> a.norm()
5.0
>>> b.norm()
3.605551275463989

这篇关于如何创建 SWIG 接口文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆