On 12/14/2013 9:06 AM, Macumber, Daniel wrote:

Thanks for looking into this Bob!  I think you are right that I want to do an explicit conversion, but I am not understanding how to do that with typemaps.  The problem I am having is that in ruby the input type is String.  SWIG has a map somewhere that converts String to std::string, I want to add one that converts String to path.  I’ve tried looking in the files related to strings in \SWIG\Lib\ruby\ but I am not seeing anything that makes sense to me.

 

  1) Error:

test_funcOnlyTakesAPath(Path_Test):

TypeError: Expected argument 0 of type openstudio::path const &, but got String

"./here"

        in SWIG method 'funcOnlyTakesAPath'

    Path_Test.rb:48:in `funcOnlyTakesAPath'

    Path_Test.rb:48:in `test_funcOnlyTakesAPath'


I can't couch this in terms of Ruby, because I've never wrapped that language.  I have done Python extensively, so here is a SWIG typemap that performs the conversion you're after.

I've defined a simple class:

    class path
    {
    public:
        path(const std::string& str) {}
    };


and a function prototype:

    void funcOnlyTakesAPath(const path& p);

in the same header for SWIG to process.

In my SWIG interface file, I have the following typemap that converts a Python None or string into a path class instance:

    %typemap(in) const path& {
        $1 = NULL;
        if($input == Py_None)
            $1 = new path("");
        else if(PyString_Check($input))
            $1 = new path(PyString_AsString($input));
        else
            SWIG_fail;
    }

    %typemap(freearg) const path& {
        if($1) delete $1;
    }

In Python, invoking this as:

    funcOnlyTakesAPath('./here')

functions as you want, with an instance of the path class being constructed and passed to the internal C++ funcOnlyTakesAPath() function as a 'const path&' type.  You just need to replace the Python API idioms for Ruby in this code to detect and convert the types.