Recommended approaches for getting my code included?

I am currently reorganizing the Tcl plugin library written in C ++. The code was originally written by hand. There is a second library that does the same for Java.

The reorganized library will be a single C ++ library that can be used to create bindings to various languages.

My first tests with SWIG are promising. However, a lot of garbage appears. Various base classes and utilities are exported. They do not make sense in terms of scenarios and only increase the clutter.

Possible solutions that I can think of:

  • Use #ifndef SWIGin source code base to filter out unnecessary code
  • Create a SWIG-compatible wrapper around the API classes.
  • Distinguish between public and private headlines. Public headers are pure abstract base classes that do not contain an implementation. Private headers inherit and implement them. Only SWIG publishes headlines.
  • The opposite of the solution above: inherit a SWIG-compatible class for each API class.

I'm currently leaning toward solution 3. However, I'm not sure, so I would like to know the opinion of the SO community about this. Feel free to share your thoughts.

Update

I forgot to list one solution:

  • Code that should not be exported by SWIG should probably not be in the public section of your class.

Perhaps this is the answer. I will have a different look on Monday.

Update

I decided with a solution. See my answer.

+3
source share
3 answers

, , ++ ++, .

  • #ifdef SWIG .hpp : ++ ,
  • SWIG: , , , SWIG, , ++ API.
  • : , , ++ API? ? ? pImpl idiom
  • SWIG- : , , .

, SWIG- API.

, .hpp SWIG ( SWIG ), , SWIG.i, . , .

-, . , , API SWIG:

// MyClass.i
%{
  #include "MyClass.hpp" // included for the generated .cxx file
%}

%include "MyClass.hpp" // included and parsed directly by SWIG

- , . , SWIG, ++ , SWIG:

// MyClass.i
%{
  #include "MyClass.hpp" // included for the generated .cxx file
%}

%ignore MyClass::someFunction(); // This function really just causes us problems
%include "MyClass.hpp" // included and parsed directly by SWIG

, , , , , - , SWIG.

// MyClass.i
%{
  #include "MyClass.hpp" // included for the generated .cxx file
%}

// With this example we provide exactly as much information to SWIG as we want
// it to have. Want it to know the base class? Add it. Don't want it to know about
// a function? Leave it out. want to add a new function? %extend it.
class MyClass
{
  void importantFunction();
  void importantFunction2();
}
+3

# 3. , COM (, ).

! , , ...

!

+2

: SWIG . , . :

  • swig . , , , .

  • , , SWIG. . SWIG:)

  • %ignore .

  • #ifndef SWIG, . , .

:

  • SWIG- API.
  • . , . . SWIG .
  • : SWIG- API.

SWAG- . , SWIG : . SWIG, JNI.

, , . , .

0
source

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


All Articles