Я пытаюсь сделать четверть плавающего типа в C ++ и обернуть его в Python, используя SWIG.Работа в процессе.Проблема в том, что% typemap (in) (int dim_count, int * shape) не применяется к конструктору, если имеется более одного конструктора.
Если я использую только один конструктор, это выглядит так:
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logchar
>>> logchar.floatTensor(1.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "logchar.py", line 263, in __init__
this = _logchar.new_floatTensor(dim_count)
ValueError: Expecting a list
>>> logchar.floatTensor([1])
<logchar.floatTensor; proxy of <Swig Object of type 'logchar::Tensor< float > *' at 0x7f1fa68da390> >
>>>
Но с несколькими конструкторами я получаю беспорядок:
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from logchar import *
>>> a=quarterTensor([1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "logchar.py", line 230, in __init__
this = _logchar.new_quarterTensor(*args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'new_quarterTensor'.
Possible C/C++ prototypes are:
logchar::Tensor< logchar::quarter >::Tensor(logchar::quarter)
logchar::Tensor< logchar::quarter >::Tensor(int,int *)
logchar::Tensor< logchar::quarter >::Tensor(logchar::Tensor< logchar::quarter > const &)
>>> num=quarter(1.0)
>>> num
1.000000
>>> a=quarterTensor(num)
>>> a._reshape([1,1,1])
>>>
В C ++:
namespace logchar {
template <typename T> class Tensor {
public:
//Tensor(T scalar);
//causes trouble
Tensor(const int dim_count, int* shape);
//Tensor(const Tensor<T>& from);
//causes trouble
~Tensor();
Tensor<T> reshape(const int dim_count, int* shape) const;
int get_dim_count() const;
void get_shape(const int dim_count, int* shape) const;
std::shared_ptr<T> operator*() const;
void _reshape(const int dim_count, int* shape);
private:
int dim_count;
int* shape;
int size_flattened;
std::shared_ptr<T> buffer;
} ;
В SWIG:
%module logchar
%{
#include "logchar.h"
%}
%typemap(in) (const int dim_count, int *shape) {
if (!PyList_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting a list");
SWIG_fail;
}
$1 = PyList_Size($input);
$2 = new int[$1];
for (int i = 0; i < $1; i++) {
PyObject *s = PyList_GetItem($input,i);
if (!PyInt_Check(s)) {
delete[] $2;
PyErr_SetString(PyExc_ValueError, "List items must be integers");
SWIG_fail;
}
$2[i] = PyInt_AsLong(s);
}
}
%typemap(freearg) int *shape {
if ($1) {
delete[] $1;
}
}
%include "logchar.h"
%template(quarterTensor) logchar::Tensor<logchar::quarter>;
%template(floatTensor) logchar::Tensor<float>;
%template(doubleTensor) logchar::Tensor<double>;
%include exception.i
%exception {
try {
$function
} catch(const char* e) {
SWIG_exception(SWIG_RuntimeError, e);
}
}
%include "carrays.i"
%array_class(int, intArray);
Единственная соответствующая ссылка, которую я нашел.Не думаю, что ответ.http://swig.10945.n7.nabble.com/multi-argument-typemap-and-default-parameter-in-python-td6721.html