KKirill
KKirill

Reputation: 21

Swig C++ to C#: How to wrap classes from C++ to make methods from template class available in derived class in C#?

I have template class BaseMyPoint in namespace external::internal

It implements public methods X(), Y(), Z() and SetCoord().

Then write

typedef internal::BaseMyPoint MyPointd;

Create derived class MyPoint, that inherits MyPointd with public

Description of both classes is stored in a file "myPoint.h":

#pragma once

#ifdef MYPOINTSDLL_EXPORTS
#define MYPOINTSDLL_API __declspec(dllexport) 
#else
#define MYPOINTSDLL_API __declspec(dllimport) 
#endif

namespace external {

namespace internal {

    template<typename T>
    struct MyPointTraits;

    template<>
    struct MyPointTraits<double>
    {
        typedef double  ValueType;
        static ValueType CoincidenceTolerance() { return 1e-7; }
    };

    template<>
    struct MyPointTraits<float>
    {
        typedef float  ValueType;
        static ValueType CoincidenceTolerance() { return 1e-7f; }
    };

    template<typename T>
    class BaseMyPoint
    {
    public:

        T myX;
        T myY;
        T myZ;

        typedef typename MyPointTraits<T>::ValueType    ValueType;

        BaseMyPoint() {}

        BaseMyPoint(ValueType theX, ValueType theY, ValueType theZ) :
            myX(theX), myY(theY), myZ(theZ) {}

        BaseMyPoint(const BaseMyPoint& theOther) :
            myX(theOther.myX), myY(theOther.myY), myZ(theOther.myZ) {}

        ValueType X() const { return myX; }
        ValueType& X() { return myX; }

        ValueType Y() const { return myY; }
        ValueType& Y() { return myY; }

        ValueType Z() const { return myZ; }
        ValueType& Z() { return myZ; }

        void SetCoord(ValueType theX, ValueType theY, ValueType theZ)
        {
            X() = theX;
            Y() = theY;
            Z() = theZ;
        }
    };

}

typedef internal::BaseMyPoint<double>   MyPointd;

typedef internal::BaseMyPoint<float>    MyPointf;

class MyPoint : public MyPointd
{
public:

    MyPoint() {}

    MyPoint(const MyPointd& theOther) : MyPointd(theOther) {}
};

}

Write in interface file "myPoint.i":

%module myPointsWrapper

%{
#include "myPoint.h"
using namespace external;
using namespace external::internal;
%}

%include <windows.i>
%include "myPoint.h"

In command line I write: C:\swig -csharp -c++ -namespace pointspase -outdir C:\myPoints\myPointcs\Generated myPoint.i

In C# we referring to these methods ( X(), Y(), Z(), SetCoord() ) through an instance MyPoint aPoint:

using System;
using pointspase;

namespace myPointcs
{
        class Program
        {
            static void Main(string[] args)
            {
                    MyPoint aPoint = new MyPoint();

                    double x = 0.2, y = 0.3, z = 0.4;
                    aPoint.SetCoord(x, y, z);

                    double X = aPoint.X(), Y = aPoint.Y(), Z = aPoint.Z();
            }
        }
    }

And I have

Error CS1061 'MyPoint' does not contain a definition for 'Z' and no extension method 'Z' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'MyPoint' does not contain a definition for 'Y' and no extension method 'Y' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'MyPoint' does not contain a definition for 'X' and no extension method 'X' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)

Error CS1061 'MyPoint' does not contain a definition for 'SetCoord' and no extension method 'SetCoord' accepting a first argument of type 'MyPoint' could be found (are you missing a using directive or an assembly reference?)

How to make these methods available in the C# class MyPoint?

Thanks in advance for your attention, Kirill

EDIT 1

Here is the simple example that clearly expresses my problem.

In "file.h" I write

template <typename T>
class myclass {
public:
    T get() const { return 0; }
};

class myintclass : public myclass<int> {};

In "file.i" I write

%template (myclassi) myclass<int>

When I compile the interface file I have warnings:

warning 401: Base class 'myclass< int >' undefined.
warning 401: 'myclass< int >' must be defined before it is used as a base class. 

As a result, C# class myintclass does not contain method get(). How can I change the interface file to get () method has been made available from the class myintclass?

Upvotes: 1

Views: 1749

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177715

Given file.h in your edit, it is a bit tricky to declare everything correctly. Ideally the template should be in a separate header than the myintclass definition, but here's a tested way to do it:

file.i

%module test

%{
#include "file.h"
%}

// Process the template first
template <typename T>
class myclass {
public:
    T get() const { return 0; }
};

// Tell SWIG to generate code for a template instance.

%template(myclassi) myclass<int>;

// Now that SWIG has code for the template instance,
// SWIG can generate code for classes using the template instance.
class myintclass : public myclass<int> {};

Testing the module (I generated for Python):

>>> import test
>>> t=test.myclassi()
>>> t.get()
0
>>> t=test.myintclass()
>>> t.get()
0

Upvotes: 1

Related Questions