繁体   English   中英

使用SWIG绑定Python / C ++模板

[英]Binding Python/C++ template with SWIG

我尝试用SWIG绑定Python中的C ++模板。 我希望我的类型名称T可以是浮点数或双精度数。 我使用SWIG 3.0.8

这是我的C ++模板:Personnage.hpp(不阅读所有内容,只有1个函数使我们感兴趣)

#ifndef PERSONNAGE_H_INCLUDED
#define PERSONNAGE_H_INCLUDED

#include <string>
#include <iostream>
#include <sstream>

template <typename T> 
class Personnage
{
    public:

        Personnage();

        ~Personnage();

        void setAge(const int &age);

        void setTaille(const T &taille);

        int getAge() const;

        T getTaille() const;


    protected:
        std::string P_nom;

        int P_age;

        T P_taille;
};

template<typename T>
Personnage<T>::Personnage()
: P_nom("Rigoberto"), P_age(42), P_taille(T(1.10))
{
}

template<typename T>
Personnage<T>::~Personnage(){}

template<typename T>
void Personnage<T>::setAge(const int &age)
{
    P_age = age;
}

template<typename T>
void Personnage<T>::setTaille(const T &taille)
{
    P_taille = taille;
}

template<typename T>
int Personnage<T>::getAge() const
{
    return P_age;
}

template<typename T>
T Personnage<T>::getTaille() const
{
    return P_taille;
}



#endif // PERSONNAGE_H_INCLUDED

我的Personnage.cpp仅包含:#include“ Personnage.hpp”

这是我的界面文件:Personnage.i

%module Personnage

%{
#include "Personnage.hpp"
%}




%include "Personnage.hpp"

%module Personnage

%{
#include "Personnage.hpp"
%}

%rename (PersonnageD) Personnage::Personnage();
%rename (PersonnageC) Personnage::Personnage(const Personnage<T> &personnage);


%include "Personnage.hpp"
%include "std_string.i"

//constructeurs simples

%template(PFloat) Personnage<float>;
%template(PDouble) Personnage<double>;

%template(estGrand) estGrand<float, float>;
%template(estGrand) estGrand<float, double>;
%template(estGrand) estGrand<double, float>;
%template(estGrand) estGrand<double, double>;

我使用这些行来使用SWIG并进行编译:

swig -c++ -python Personnage.i
g++ -fPIC -c Personnage.cpp
g++ -fPIC -c Personnage_wrap.cxx $(python-config --cflags)
g++ -shared Personnage.o Personnage_wrap.o $(python-config --ldflags) -o _Personnage.so

这是我用来测试此内容的python文件:

import Personnage
persSimple = Personnage.PDouble("didier",45,1.59) #Player with a "Taille" in Double
print(persSimple.getTaille())
FpersSimple = Personnage.PFloat("didier",45,1.59) #Player with a "Taille" in Float
print(FpersSimple.getTaille())

第一次打印显示为“ 1.59”,这是正确的。 第二张印刷品显示“ 1.5900000(随机数)”,这正是我在这里寻求帮助的原因:它应该仅显示“ 1.59”。

当我在Python上键入“ FpersSimple”时,我得到:

<Personnage.PFloat; proxy of <Swig Object of type 'Personnage< float > *' at 0x7fd2742b3480> >

所以我的FpersSimple绝对包含一个浮点数。

我不知道我该怎么办。

感谢您的时间 !

谢谢您的时间和回答!

Python只知道double,因此您返回的浮点数将被转换为double,而附加数字将只是未初始化的内存,但是当您打印该值时,这些也会被考虑在内。

您甚至不需要SWIG和Python的复杂示例即可重现此内容。 只需从double初始化浮点数(字面量1.59是double)并以double精度打印即可。

#include <iomanip>
#include <iostream>
#include <limits>

int main() {
    double d = 1.59;
    float f = 1.59;
    std::cout << std::setprecision(std::numeric_limits<double>::digits10)
              << std::fixed
              << d << '\n'
              << f << '\n';
}
1.590000000000000
1.590000033378601

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM