Fix long long and other warnings.
Add the macros %typemap_asfromcheck() %typemap_asfrom() that can be used to defined all the different typemaps for types where the As/From/Check methods are provided. All the basic type (int, char,...) typemaps are implemented using them. The std::string and std::complex<T> are reimplemented using the new %typemap_asfrom/check macros too. This helps to complete all the previously missing typemaps (consttab, varin, varout,..) and also ilustrates how to define the As/From/Check methods to use with the %typemap_asfrom/check macros. As a byproduct, the C complex typemap was added, and the file complex.i can be used to load the complex support for either C or C++. The original C++ std_complex.i is still there, and the corresponding C ccomplex.i too, if they need to be included explicitly. Also, the As/From methods are declared via %fragment, so, they can be reused as needed, and only appear in the wrapped code if they corresponding typemap is invoked, making the typemaps and the entire code shorter and simpler. Marcelo. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5691 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
9b4df57aa4
commit
b85c9ace6b
11 changed files with 647 additions and 506 deletions
|
|
@ -12,68 +12,50 @@
|
|||
// However, I think I'll wait until someone asks for it...
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
%include exception.i
|
||||
|
||||
%{
|
||||
#include <string>
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
|
||||
class string;
|
||||
|
||||
/* Overloading check */
|
||||
|
||||
%typemap(typecheck) string = char *;
|
||||
%typemap(typecheck) const string & = char *;
|
||||
|
||||
%typemap(in) string {
|
||||
if (PyString_Check($input))
|
||||
$1 = std::string(PyString_AsString($input),
|
||||
PyString_Size($input));
|
||||
else
|
||||
SWIG_exception(SWIG_TypeError, "string expected");
|
||||
}
|
||||
|
||||
%typemap(in) const string & (std::string temp) {
|
||||
if (PyString_Check($input)) {
|
||||
temp = std::string(PyString_AsString($input),
|
||||
PyString_Size($input));
|
||||
$1 = &temp;
|
||||
} else {
|
||||
SWIG_exception(SWIG_TypeError, "string expected");
|
||||
}
|
||||
}
|
||||
|
||||
%typemap(out) string {
|
||||
$result = PyString_FromStringAndSize($1.data(),$1.size());
|
||||
}
|
||||
|
||||
%typemap(out) const string & {
|
||||
$result = PyString_FromStringAndSize($1->data(),$1->size());
|
||||
}
|
||||
|
||||
%typemap(directorin, parse="s") string, const string &, string & "$1_name.c_str()";
|
||||
|
||||
%typemap(directorin, parse="s") string *, const string * "$1_name->c_str()";
|
||||
|
||||
%typemap(directorout) string {
|
||||
if (PyString_Check($input))
|
||||
$result = std::string(PyString_AsString($input),
|
||||
PyString_Size($input));
|
||||
else
|
||||
throw Swig::DirectorTypeMismatchException("string expected");
|
||||
}
|
||||
|
||||
%typemap(directorout) const string & (std::string temp) {
|
||||
if (PyString_Check($input)) {
|
||||
temp = std::string(PyString_AsString($input),
|
||||
PyString_Size($input));
|
||||
$result = &temp;
|
||||
} else {
|
||||
throw Swig::DirectorTypeMismatchException("string expected");
|
||||
}
|
||||
}
|
||||
/* defining the std::string as/from converters */
|
||||
|
||||
%fragment("SWIG_PyObj_AsStdString","header") %{
|
||||
static inline std::string
|
||||
SWIG_PyObj_AsStdString(PyObject* obj) {
|
||||
static swig_type_info* pchar_info = SWIG_TypeQuery("char *");
|
||||
char* buf = 0 ; size_t size = 0;
|
||||
SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &buf, &size);
|
||||
if (PyErr_Occurred() || !buf) {
|
||||
PyErr_Clear();
|
||||
PyErr_SetString(PyExc_TypeError,"a string is expected");
|
||||
return std::string();
|
||||
}
|
||||
return std::string(buf, size);
|
||||
}
|
||||
%}
|
||||
|
||||
%fragment("SWIG_PyObj_CheckStdString","header") %{
|
||||
static inline void
|
||||
SWIG_PyObj_CheckStdString(PyObject* obj) {
|
||||
static swig_type_info* pchar_info = SWIG_TypeQuery("char *");
|
||||
char* buf = 0; size_t size = 0;
|
||||
SWIG_PyObj_AsCharPtrAndSize(obj, pchar_info, &buf, &size);
|
||||
if (PyErr_Occurred() || !buf) {
|
||||
PyErr_Clear();
|
||||
PyErr_SetString(PyExc_TypeError,"a string is expected");
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
||||
%fragment("SWIG_PyObj_FromStdString","header") %{
|
||||
static inline PyObject*
|
||||
SWIG_PyObj_FromStdString(const std::string& s) {
|
||||
return SWIG_PyObj_FromCharArray(s.data(), s.size());
|
||||
}
|
||||
%}
|
||||
|
||||
|
||||
/* declaring the typemaps */
|
||||
|
||||
%typemap_asfromcheck(std::string, STRING,
|
||||
SWIG_PyObj_AsStdString, SWIG_PyObj_FromStdString,
|
||||
SWIG_PyObj_CheckStdString);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue