Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate C# Interface from C++ using SWIG

How do I use SWIG to generate a C# interface (or at least a C# mock-able base class) from C++ using SWIG?

Given:

C++:

class IWidget
{
public:
     virtual void Flob() = 0;
};

class Widget : public IWidget
{
public:
     void Flob() {};
};

I would like to output C#:

public interface IWidget
{
     void Flob();
}

public class Widget : IWidget
{...}

Note: Solution does not have to be an interface, but I do need to be able to use mocking frameworks such as Moq or Rhino.Mocks to mock out the base of the C# Widget class. My attempts only yielded generated C# with no public constructors.

like image 323
PatrickV Avatar asked Sep 08 '14 23:09

PatrickV


People also ask

How do I generate C++ code in MATLAB?

codegen options files function -args {func_inputs} generates C/C++ code from a MATLAB function that uses custom source code specified in external files. For more information, see Call Custom C/C++ Code from the Generated Code and Configure Build for External C/C++ Code.

How to generate single-precision C/C++ code using Codegen?

When used with the -config option, also generates single-precision C/C++ code. codegen generates the single-precision files in the folder codegen/target/folder_name folder_name is the concatenation of fcn_name and singlesuffix. singlesuffix is the suffix that the coder.SingleConfig property OutputFileNameSuffix specifies.

How to generate random number from seed in C?

The rand () function in C could be used in order to generate the random number and the generated number is totally deleting seed. A seed is a value that is used by rand function to generate the random value.

How do I set the build type for code generation?

Use the options argument to specify settings such as the code generation configuration object. The configuration object controls build type (MEX, lib, dll, or exe) and code generation parameters.


1 Answers

You may be able to get the output you list by using one or more of:

  • csclassmodifiers
  • use %ignore on the pure virtual methods, add necessary method declarations using cscode .
  • csinterfaces typemap
  • csinterfaces_derived typemap

However this may not be the easiest way to achieve the goal of making your class mockable. Consider extending your post to clearly identify what is the c# that you need to achieve this if interface is not used.

Also take a look at this post: http://swig.10945.n7.nabble.com/Multiple-inheritance-and-C-any-easy-workarounds-available-td11516.html.

Sorry I can't provide an actual code answer, hopefully the above gets you there.

like image 156
Oliver Avatar answered Sep 17 '22 16:09

Oliver