Creating an Object Oriented API through C with SWIG

I use SWIG to create wrappers for library C. The C library interface has a natural mapping to an object-oriented API (which I would like to show in the target language), but using SWIG directly to create wrappers will create a single object in the target language with all interfaces of the C library .

I see several options:

  • Create a C ++ interface for the C library, and then wrap C ++ with SWIG
  • Create custom classes in each target language that use a simple, non-OIO SWIG output inside

I would prefer bullet point 2, but my question is, is this OK? This is attractive because I would like to have complete control over the interface in the target language with minimal emphasis on the advanced features of SWIG.

+3
source share
2 answers

I also prefer option 2 ("Build custom classes in each target language that use a simple, outside OIO SWIG output inside").

My reasons are as follows:

  • You need to execute "less code." the difficult part is the C + SWIG wrapper library, so it's best to keep it as small as possible. Creating another C ++ port will complicate this part a lot.

  • It is usually easier to create interface classes in Python (or Java?), Just because it has a higher level of abstraction.

  • You can apply an adapter template that is very well suited for this case. http://en.wikipedia.org/wiki/Adapter_pattern

+1
source

ctypes / cython? , SWIG; , ctypes , python ( , ).

0

Source: https://habr.com/ru/post/1791568/


All Articles