Re: Using std_string.i to automatically convert a different C++ string class to ruby String
William S Fulton <[email protected]> Fri, 28 Apr 2023 08:33:46 +0100
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CANGqftA2F9gmUYB6s6JyCnysdJ7nupsbhDfBnA7o4hupimA_Pw@mail.gmail.com> |
I would run swig with the -E option using this input interface file: %module example %include <std_string.i> This will output the preprocessed code with the std::string typemaps at the end of the output. I advise this as the input files are full of macros and may be hard to follow. I'd copy the std::string typemaps and tweak for QString. The typemaps for std::string are comprehensive, you may only need a get away with just the in, out, typecheck typemaps for simple usage. Typemaps for QT types ought to go into a specific QT SWIG specific repository, please take a look at https://github.com/swig/swig/issues/88. William On Tue, 11 Apr 2023 at 12:55, Stefano Crocco <[email protected]> wrote: > 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 > _______________________________________________ Swig-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/swig-user