swig/Lib/python/std_string.i
Marcelo Matus 68024b15ce Reducing pyrun.swg and splitting python.swg into different files
for clarity and for easier maintainance.

pyrun.swg almost the same than 1.3.20, therefore there will be
compatible again.

code generated is reduced by the use and reuse of %fragments.

as usual, all the test-suite is compiling and a much bigger
"test project" too.

with the new typemaps definition should be much eaiser and
uniform add stl/std and user types.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5706 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-02-10 09:50:24 +00:00

71 lines
1.7 KiB
OpenEdge ABL

//
// SWIG typemaps for std::string
// Luigi Ballabio
// Apr 8, 2002
//
// Python implementation
// ------------------------------------------------------------------------
// std::string is typemapped by value
// This can prevent exporting methods which return a string
// in order for the user to modify it.
// However, I think I'll wait until someone asks for it...
// ------------------------------------------------------------------------
%{
#include <string>
%}
/* defining the std::string as/from/check methods */
%fragment("SWIG_TryStdString","header",
fragment="SWIG_AsCharPtrAndSize") %{
SWIGSTATICINLINE(int)
SWIG_TryStdString(PyObject* obj, char*& buf, size_t& size) {
SWIG_AsCharPtrAndSize(obj, &buf, &size);
if (PyErr_Occurred() || !buf) {
if (PyErr_Occurred()) PyErr_Clear();
return 0;
}
return 1;
}
%}
%fragment("SWIG_CheckStdString","header",
fragment="SWIG_TryStdString") %{
SWIGSTATICINLINE(int)
SWIG_CheckStdString(PyObject* obj) {
char* buf = 0 ; size_t size = 0;
return SWIG_TryStdString(obj, buf, size);
}
%}
%fragment("SWIG_AsStdString","header",
fragment="SWIG_TryStdString") %{
SWIGSTATICINLINE(std::string)
SWIG_AsStdString(PyObject* obj) {
char* buf = 0 ; size_t size = 0;
if (SWIG_TryStdString(obj, buf, size)) {
return std::string(buf, size);
} else {
PyErr_SetString(PyExc_TypeError,"a string is expected");
return std::string();
}
}
%}
%fragment("SWIG_FromStdString","header",
fragment="SWIG_FromCharArray") %{
SWIGSTATICINLINE(PyObject*)
SWIG_FromStdString(const std::string& s) {
return SWIG_FromCharArray(s.data(), s.size());
}
%}
/* declaring the typemaps */
%typemap_asfromcheck(std::string, STRING,
SWIG_AsStdString,
SWIG_FromStdString,
SWIG_CheckStdString);