Combining modules and namespaces

Hans Packet <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CADnEfQL2SFxDgfNDbpMYppFwZwYw_9P+V=+aM6-3mkEtxLQ+3Q@mail.gmail.com>
I'm trying generate C# bindings for a c++ API, and I'm running into
problems with the combination of modules and namespaces.

Some context on the problem I'm trying to solve: most of my c++ API is
defined in a single namespace. For the C# API, it was preferred to have a
more finegrained namespace structure. As far as I can see, the nspace
feature can't accomplish that exactly though. For that reason, I have tried
to use an alternative approach: split up the swig binding into multiple
modules (one per namespace), and use the -namespace cl argument for each of
the modules. Note that many of the modules depend on other modules. This
works well, except that I run into a few problems:

a) Other modules don't know anything about the namespace of an other
module. I'm working around this by adding
  - %typemap(csimports)
  - %pragma(csharp) imclassimports
This works ok, but it is manual work, i.e. harder to maintain. This is not
a critical problem, but it makes the definition files harder to write.

b) I end up with naming clashes in a very specific case. When a method
argment type has the same name as the method itself, C# doesn't correctly
resolve this. Example of the generated C# code:

using Ns;

void Rectangle(Rectangle rect) {
  xxxPINVOKE.Xxx_Rectangle(swigCPtr, Rectangle.getCPtr(rect));
  // ...
}

When using the nspace feature (which I probably can't use), this is not an
issue, because the generated C# code ends up as:

void Rectangle(Ns.Rectangle rect) {
  xxxPINVOKE.Xxx_Rectangle(swigCPtr, Ns.Rectangle.getCPtr(rect));
  // ...
}

You could argue that a method name should not be the same as the method
argument's type, but in many cases (within the naming conventions that are
used), it does make sense. So I would like to avoid renaming the methods if
possible.


My general question would be: Am I taking the correct approach to solve the
'single c++ namespace -> multiple C# namespaces' problem ? Or is there a
better way to do this ? If you have suggestions to solve the sub-problems
(a) and (b), that would be welcome too.

Note: I came across a similar question in this mailing list, but it doesn't
give a complete solution to my problem. It's also 10 years old, so a better
solution might be available now.
https://sourceforge.net/p/swig/mailman/message/24058732/

Let me know if you need more information.

Thanks,
Hans

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.