From: William S F. <ws...@fu...> - 2011-06-20 05:53:08
|
On 20/06/11 01:13, Aubrey Barnard wrote: > On 06/16/2011 02:32 PM, Stefan Zager wrote: >> On Wed, Jun 15, 2011 at 2:37 PM, Aubrey Barnard<ba...@cs... >> <mailto:ba...@cs...>> wrote: >> >> Knowing this all has to do with typemaps, the last thing I tried was >> telling Swig to apply an existing typemap to the type in question. I >> don't think I did this correctly as size_type is not a pointer, but I'm >> not clear on the details (syntax, matching) of specifying the arguments >> for most of the Swig directives, including %apply. >> %apply unsigned long *OUTPUT { std::vector::size_type }; >> >> That's where I am at. Clearly there are gaps in my understanding of Swig >> (despite re-reading the apparently relevant sections of the manual), and >> that is why I am asking for some help with this. >> >> >> I think you're on the right track with %typemap. Try providing a >> %typemap for each %template instance, something like this: >> >> %apply size_t { std::vector<int>::size_type }; >> %template(SmallSetInt) SmallSet<int>; >> >> %apply size_t { std::vector<double>::size_type }; >> %template(SmallSetDouble) SmallSet<double>; >> >> Yes, except the %apply should not be necessary. It looks like there is a failure in the typedef resolution for templates with default template parameters - std::vector has a default parameter for the allocator. I'll see if I can track this down and fix... deep breath. >> Stefan > > Stefan, > > Thanks for the suggestion. It worked. I tried a few variants but > > %apply size_t { std::vector<int>::size_type }; > > was the minimal solution I was able to get to work. > > ----- > As a note to all those who might encounter similar problems in the > future, I found it helpful to run with '-debug-tmused' to see what > typemaps Swig applied to the method in question (grep helped). Once I > was able to see that, it was easy to see when I had fixed the problem > and had Swig doing what I want. Further, importing/including > std_vector.i complicates things slightly as the vector type changes and > so the following line is required instead of the one above. > > %apply size_t { std::vector<int,std::allocator<int>>::size_type }; > > You do not need to include a typedef for size_t as Swig already knows > what that is. > ----- > > I am still curious (but rhetorically so) why my first idea of including > std_vector.i did not work. The typedef equating size_t and size_type is > in std_vector.i so it seems like that should be enough. > This is all that *should* be needed: %include <std_vector.i> // Named or unnamed %template can be used in this instance //%template(VectorInt) std::vector<size_t>; %template() std::vector<size_t>; %inline %{ #include <vector> template<typename T> class SmallSet { public: typename std::vector<T>::size_type size() const; }; %} %template(SmallSetInt) SmallSet<size_t>; In fact this does work with Java wrappers because the std::vector declaration used in Java does not have the allocator. William |