Bug#672035: [Swig-user] Using own string class
On 24/05/12 22:37, David Piepgrass wrote: At ZNC we have an own string class CString, which is inherited from std::string. The goal is to use it as a just string in target languages. How to do that properly? When you use a string class derived from std::string, the main problem tends to be that SWIG's built-in typemaps refer to std::string explicitly, but that's unnecessary. For some target languages (at least C# and Java) if you simply make a copy of the typemap with std::string changed to $*1_ltype, then you can %apply those typemaps to your derived class. Thanks for pointing this out. I've put these modifications into swig-2.0.7. William -- To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Bug#672035: [Swig-user] Using own string class
On 24/05/12 19:45, Alexey Sokolov wrote: Hello! At ZNC we have an own string class CString, which is inherited from std::string. The goal is to use it as a just string in target languages. How to do that properly? When I was writing modperl and modpython ZNC modules, I used an approach described at http://old.nabble.com/Forward-declaration-error--td24064356.html Used swig -E on std_string.i, got a long file as a result, cleaned it up and changed all occurences of std::string to CString. It worked fine before SWIG 2.0.5, but in 2.0.5 it started to give errors while generating sources for modpython: /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (directorout) std::pair CString,CString = std::pair CString,CString DIRECTOROUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (in) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (in) std::pair CString,CString INPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString INPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (argout) std::pair CString,CString *OUTPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (argout) std::pair CString,CString OUTPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString INPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (freearg) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (freearg) std::pair CString,CString INPUT = std::pair CString,CString INOUT Sending copy of the letter to 672...@bugs.debian.org Also there is related issue #174 on github/znc. I can't replicate this bug with 2.0.6, you are going to have to provide a test case. swig-2.0.5 introduced a regression which was fixed in 2.0.7 - incorrect typemaps for templates were being used in conjunction with typedef. Please try 2.0.7 and report back if fixed. William -- To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org
Bug#672035: [Swig-user] Using own string class
27.05.2012 04:14, William S Fulton пишет: I can't replicate this bug with 2.0.6, you are going to have to provide a test case. swig-2.0.5 introduced a regression which was fixed in 2.0.7 - incorrect typemaps for templates were being used in conjunction with typedef. Please try 2.0.7 and report back if fixed. William 2.0.7 shows the errors too. The minimal test case is attached. Run: swig -python -c++ test.i If to use %template(StrPair) std::pairstd::string, std::string; instead of using std::pair, swig executes fine. I didn't test the generated code though. -- Best regards, Alexey DarthGandalf Sokolov %module test %include stl.i using std::pair; %template(StrPair) pairstd::string, std::string; signature.asc Description: OpenPGP digital signature
Bug#672035: [Swig-user] Using own string class
We do the same thing here, we just use basic in/out typemaps. E.g. %typemap(out) YString { $result = PyString_FromString((char *)static_castconst char *($1)); } %typemap(in) const YString { $1 = new YString(PyString_AsString($input)); } -Original Message- From: Alexey Sokolov [mailto:ale...@asokolov.org] Sent: Thursday, May 24, 2012 2:46 PM To: swig-u...@lists.sourceforge.net; 672...@bugs.debian.org Subject: [Swig-user] Using own string class Hello! At ZNC we have an own string class CString, which is inherited from std::string. The goal is to use it as a just string in target languages. How to do that properly? When I was writing modperl and modpython ZNC modules, I used an approach described at http://old.nabble.com/Forward-declaration-error--td24064356.html Used swig -E on std_string.i, got a long file as a result, cleaned it up and changed all occurences of std::string to CString. It worked fine before SWIG 2.0.5, but in 2.0.5 it started to give errors while generating sources for modpython: /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (directorout) std::pair CString,CString = std::pair CString,CString DIRECTOROUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (in) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (in) std::pair CString,CString INPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString INPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (argout) std::pair CString,CString *OUTPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (argout) std::pair CString,CString OUTPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (typecheck) std::pair CString,CString INPUT = std::pair CString,CString INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (freearg) std::pair CString,CString *INPUT = std::pair CString,CString *INOUT /usr/share/swig/2.0.6/std/std_pair.i:31: Error: Can't copy typemap (freearg) std::pair CString,CString INPUT = std::pair CString,CString INOUT Sending copy of the letter to 672...@bugs.debian.org Also there is related issue #174 on github/znc. -- Best regards, Alexey DarthGandalf Sokolov 'Like' us on Facebook for exclusive content and other resources on all Barracuda Networks solutions. Visit http://barracudanetworks.com/facebook
Bug#672035: [Swig-user] Using own string class
At ZNC we have an own string class CString, which is inherited from std::string. The goal is to use it as a just string in target languages. How to do that properly? When you use a string class derived from std::string, the main problem tends to be that SWIG's built-in typemaps refer to std::string explicitly, but that's unnecessary. For some target languages (at least C# and Java) if you simply make a copy of the typemap with std::string changed to $*1_ltype, then you can %apply those typemaps to your derived class. For example, here are the corrected typemaps for Java: namespace std { // Changed lines are marked //fixed %typemap(in) const string %{ if(!$input) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, String param cannot be null); return $null; } const char *$1_pstr = (const char *)jenv-GetStringUTFChars($input, 0); if (!$1_pstr) return $null; $*1_ltype $1_str($1_pstr); //fixed $1 = $1_str; jenv-ReleaseStringUTFChars($input, $1_pstr); %} %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string %{ if(!$input) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, String param cannot be null); return $null; } const char *$1_pstr = (const char *)jenv-GetStringUTFChars($input, 0); if (!$1_pstr) return $null; /* possible thread/reentrant code problem */ static $1_ltype $1_str; //fixed $1_str = $1_pstr; $result = $1_str; jenv-ReleaseStringUTFChars($input, $1_pstr); %} } // namespace std Then you use %apply to support your custom string too: %apply std::string { CString } %apply std::string { CString } // [not supported in Java] %apply const std::string { const CString }