From: <ta...@gm...> - 2006-10-19 08:21:49
|
> There is of course a way to do this with typemaps. > > %typemap(in) const boost::optional<int>& (boost::optional<int> tmp) { > if ($input == Py_None) { > tmp = boost::optional<int>(); > } else { > long v = PyLong_AsLong($input); > tmp = boost::optional<int>(static_cast<int>(v)); > } > $1 = &tmp; > } > > Of course this could be improved to add in error checking and to take a > generic type and to use with %template. The the out typemap needs > writing too, but this should be enough for you to get going... It works as described above. Thank you. The out typemap has been just the other way round without the tmp parameter. Great. On more question: Does this code only work for Python? When I want to use another target language like Java, do I have to write adapted typemaps for it? You wrote about using a generic type for the typemaps. How would I do that? If you use boost::optional with ints, longs, chars and doubles and floats then different conversion functions have to be used (PyLong_*, PyFloat_*). Would it be possible to delegate a generic conversion to SWIG? I read about the %apply directive. I could write a typemap for int and double and add: %apply boost::optional<int> { boost::optional<long>, boost::optional<bool>, boost::optional<char> }; %apply boost::optional<double> { boost::optional<float> }; Adding std::string as a template argument for boost::optional would lead to two more typemaps, wouldn't it? Regards, Jan |