Using std_string.i to automatically convert a different C++ string class to ruby String

Stefano Crocco <[email protected]> Tue, 11 Apr 2023 13:58:25 +0200
Newsgroups gmane.comp.programming.swig
Message-ID <2302867.ElGaqSPkdT@casa>
Hello to everyone,
I'm new to SWIG and I'm trying to use SWIG to use the Qt C++ libraries [1] 
from Ruby. After some simple tests, I tried to wrap some functions using 
strings. Unfortunately, Qt doesn't use std::string to represent strings, but 
its own class, called QString. I'd like to be able to automatically convert 
QString to and from ruby String, just as the typemaps in std_string.i does for 
std::string.

I hoped that, since QString can be converted to std::string and vice versa, it 
would be possible to somehow make use of the std_string.i library to tell 
SWIG:
- when, converting from C++ to ruby, you find a QString, call the 
QString::toStdString method to convert it to std::string, then apply the 
default typemap for std::string
- when, converting from ruby to C++, you find String, convert it to a 
std::string using the default typemap for std::string, then call 
QString::fromStdString on the result.

Unfortunately, as far as I can tell, this isn't possible. I hoped that 
typemaps would be applied recursively, so I tried the following:

%typemap(out) QString {
  $result = $1.toStdString();
}

I hoped that SWIG would then notice that the resulting value wasn't a ruby 
object but it still was a C++ class it knows how to convert to ruby. Of 
course, that didn't work and I got the error:
error: cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} 
to ‘VALUE’ {aka ‘long unsigned int’} in assignment

Is there some way to converting QString to and from ruby String using, 
directly or indirectly, the existing conversions of std::string? I'd like to 
avoid having to avoid writing the low-level conversion code myself, unless 
absolutely necessary. I searched both the documentation and Google and looked 
at the code in std_string.i, but I couldn't find anything useful, so I guess 
it's not possible, but I'd like to be sure.

Thanks in advance

Stefano


[1] https://www.qt.io/




_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user