Search code examples
c#c++swigswig-templatetypemaps

Changing return type in generated CS function using SWIG


I am trying to generate wrappers in C# for C++ library and using generated C3 wrappers I am developing client application.

C++ class:

namespace ns {
    class ISampleInterface {
        public:
            virtual ~ISampleInterface() {}
            virtual void fn(int cnt) = 0;
    };
}

namespace ns {
    class ISampleInterface2 {
        public:
            virtual ~ISampleInterface() {}
            virtual void fn2(int cnt) = 0;
    };
}

namespace ns {
    class SampleClass  {
        public:
            SampleClass() {}
            ~SampleClass() {}
            void InputFunction(int count, ISampleInterface sampleInterface, ISampleInterface2& sampleInterface2){
            }
            ISampleInterface& OutputFunction(void) {
                return null;
            }
    };
}

Expected C# code:

namespace csNS {
    public class SampleClass {
        public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
            // some code
        }
        public SampleInterface OutputFunction() {
            // some code 
        }
    }
}

Swig interface declaration:


%interface_custom("SampleInterfaceImpl", "SampleInterface", ns::ISampleInterface);
%interface_custom("SampleInterface2Impl", "SampleInterface2", ns::ISampleInterface2);

%Outparams(ns:ISampleInterface, SampleInterfaceImpl)
%Outparams(ns:ISampleInterface2, SampleInterface2Impl)

In above C# code, InputFunction takes 3 arguments. However, the implementation generated for SampleInterface and SampleInterface2 does not contain default constructor. Hence I can not create objects for these interfaces. As I am unable to instantiate interfaces at C# side, I want to pass then as out parameter to the CS function and then that will be initialized at C++ side. So to modify cs file generated during swig compilation below typemap I had defined.

Typemap declarations:

%define %Outparams(TYPE, InterfaceImpl)

%typemap (cstype) TYPE,
                  TYPE &,
                  TYPE *,
                  TYPE *& "out TYPE"
%enddef

Using above typemap I am able to change input arguments, however, it is also changing function return values and I am not able to control this.

Actual cs code generated:

namespace csNS {
    public class SampleClass {
        public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
            // some code
        }
        public out SampleInterface OutputFunction() {
            // some code 
        }
    }
}

What am I doing wrong???


Solution

  • I changed typemap declaration, It worked.

    For class:

    %typemap (cstype, out="TYPE") TYPE,
                      TYPE &,
                      TYPE *,
                      TYPE *& "out TYPE" 
    

    For Interface:

    %typemap (cstype, out="InterfaceImpl") TYPE,
                      TYPE &,
                      TYPE *,
                      TYPE *& "out InterfaceImpl"
    

    CS file:

    namespace csNS {
        public class SampleClass {
            public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
                // some code
            }
            public SampleInterfaceImpl OutputFunction() {
                // some code 
            }
        }
    }