Commit graph

20 commits

Author SHA1 Message Date
Vadim Zeitlin
4748e4f379 Remove more insignificant differences with examples in master
Follow the she style of the other existing examples.
2022-01-15 16:50:17 +01:00
Vadim Zeitlin
9a8ebbb998 Throw SWIG_CException from C++ wrappers automatically
Check for the pending exception after every call to a wrapper function
not marked "noexcept" and throw a C++ exception if necessary.

Add C++ version of the exception example to show how this works.

Also change SWIG_CException to use member functions for checking for and
resetting pending exceptions, this seems better than having separate
functions for it and will make it easier to customize exception handling
later by just replacing SWIG_CException class with something else.

Note that we still use a global (and not a member) function for raising
the exception, but this one is not exported at all, and needs to be a
function in order to be easily callable from other modules (see the
upcoming commit).
2021-11-27 00:19:05 +01:00
Vadim Zeitlin
d2546e23ff Don't use exception specification with C++ compiler in example
Support for the exception specifications using types was removed in
C++17 (and "throw ()" in C++20), so don't use them when using the C++
compiler any longer, as this broke the example with recent g++ versions
that use C++17 by default.

We still need them for SWIG, however, so use SWIG_THROW macro, defined
differently for SWIG and the compiler, to preserve the existing
behaviour.

Using %except might be a better idea, but would require more changes.
2021-11-24 20:16:42 +01:00
Vadim Zeitlin
3765a08743 Allow having C++ test cases for C backend
Support compiling and running either _runme.c or _runme.cxx files for
the given test (but not both).

Add a simple C++ test file to check that it actually works.
2021-11-19 00:08:43 +01:00
Vadim Zeitlin
e8f9bdba80 Remove wrapper aliases generation and use -namespace in the tests
The -namespace option provides a better way of using the wrapped API, so
drop the optional wrapper generation, which is useless when this option
is used and just generates many lines of unwanted junk in the header.

Update the test suite and the examples to compensate to not rely on
being able to define SWIG_DEFINE_WRAPPER_ALIASES and add -namespace
option to all C++ tests, as it's done for C# test suite, and update them
to use the correct prefix and also use the accessors for the global
variables rather than using them directly, as this is impossible when
namespace prefix is used (it would have been possible to define a
preprocessor symbol corresponding to the real variable name, but it's
arguably not worth it).

fixup! Remove wrapper aliases generation and use -namespace in the tests
2021-11-10 02:11:59 +01:00
Vadim Zeitlin
2f6f6df211 Generate wrapper aliases only if requested and not by default
Defining the aliases by default results in conflicts when including
headers from multiple modules as e.g. SWIG_PendingException_get() is
defined in all of them, and could also easily result in other unwanted
clashes, so make this opt-in and update the examples and tests relying
on using the wrappers without the module prefix to define
SWIG_DEFINE_WRAPPER_ALIASES explicitly.
2021-10-20 01:57:20 +02:00
Vadim Zeitlin
03b6e2fbe6 Don't use SWIG_exit() in C examples
There doesn't seem to be any reason for using it rather than just
returning from main() as usual, and it provokes warnings about
implicitly declared function when compiling them.
2021-10-05 00:26:12 +02:00
Vadim Zeitlin
55f7d51979 Fix memory leak in the exception example
Delete the object we allocate.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
dfac9ce325 Show how to handle a custom exception class in the example
Define "throws" typemap for it.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
10d25c7327 Drop longjmp-based exception handling approach
Using longjmp was incompatible with using C++ objects in the code using the
wrappers, and using this C API from C++ to avoid ABI incompatibilities between
different C++ compilers is one of the main reasons for using this module.

Also, this required using a separate SwigObj instead of just using the real
object pointer which inevitably resulted in memory leaks whenever a non owned
object was returned from anywhere, e.g. from a member accessor or any method
returning pointer or reference.

Abandon the attempts to recreate C++ exceptions in C and just use a very
simple approach allowing to pass an error message out of band after any
function call in a global variable. An alternative could be to add a special
"out" error parameter to each and every function, but this risked being too
verbose, especially for the functions which don't really throw, and the calls
to SWIG_PendingException_get() won't need to be made explicitly when using a
C++ wrapper around the generated C API in the future.

This simplifies both the module and the generated code, in particular we don't
need any runtime code at all any more and there is no need for an extra level
of indirection for every object.

It also makes a couple more tests pass.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
bda731cd8f Change naming convention for wrapped ctors and dtor
Use Foo_{new,delete}() instead of {new,delete}_Foo() to ensure that all
methods of the class Foo start with the corresponding prefix.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
e1a4b02f69 No real changes, just convert files to Unix EOLs
Some tests and examples files as well as the C manual chapter used DOS EOLs,
get rid of them for consistency with all the other text files.
2016-04-16 00:10:22 +02:00
Vadim Zeitlin
3d6880aad1 Start removing proxy layer, just use the wrapped functions directly
The proxy layer, and all the extra complexity associated with it, seemed to be
only necessary in order to try to allow using the same name for the wrapped
global functions as were used for them in the original C or C++ code being
wrapped. However this could simply never work in all cases, notably it didn't
work at all when using ELF shared libraries under Unix as the functions with
the same name defined in the main program were interposed and replaced the
functions defined in the shared library, meaning that the proxy function foo()
called wrapper function _wrap_foo() which called back into proxy function
foo() itself again, resulting in guaranteed stack overflow. The only possible
way to fix this would be to use "protected" ELF visibility for the original
functions, but this is not always possible (e.g. if the sources of the
original library are not available) and not simple even when it is and,
besides, protected visibility has its own problems -- notably by making it
impossible to hook the library functions when you actually want to do it.
Besides, proxy-based approach simply couldn't work at all when using static
linking as it resulted in two copies of the function with the same name

Most importantly, however, the main task of this module is to wrap C++
classes, not C functions, and renaming them in the wrapper is not necessary at
all in this case as there is no conflict with the original names in this case.
So simply drop the idea of generating a separate proxy header and generate a
header declaring the functions declared in the wrapper instead and, also, do
not give them "_wrap_" prefix whenever possible, i.e. only do it for the
global functions.

This simplifies SWIG code itself and makes it simpler to use its output as
it's not necessary to link both with the wrapper (dynamically) and with the
proxy (statically) and it's not enough to link with the wrapper only and it
can be done in any way (i.e. either statically or dynamically).

As a side effect of this change, Swig_name_proxy() is not necessary any more
and was removed, eliminating the only difference with the master branch in any
source file other than c.cxx itself.
2016-04-14 02:44:45 +02:00
Vadim Zeitlin
19ed0b531d Fix and simplify C examples makefiles
Make "make check-c-examples" perform the correct build commands, e.g. not link
the proxy code into the shared library as this can't work because it defines
the same functions that are already present in it.

Also fix the c_xxx targets to work when SWIG is built in a separate build
directory.

Finally, simplify them by removing the unnecessary variables.

Notice that std_vector example still doesn't build, but at least now it is due
to a real problem in the C module and not makefile bugs.
2016-04-10 22:45:51 +02:00
William S Fulton
dcc3756341 Make C example makefiles more standard. Note that valgrind can be used
via the RUNTOOL env variable, see the docs on running the test-suite.

From: William S Fulton <wsf@fultondesigns.co.uk>

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@13041 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-05-07 17:02:51 +00:00
Maciej Drwal
5002c69cd6 Fixed some issues with member variables handling
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@11155 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2009-03-18 15:04:20 +00:00
Maciej Drwal
54860c9595 1. Fixed the bug in enums. Now 'enums' test is compiling OK.
2. -noexcept flag disables generating exception-related code (like array of type names in SwigObj, object registry, etc.). This can be used when we are sure we won't handle exceptions on the C side, and this will generate much less code. 
3. Modified typemaps for object arrays. Multidimensional ones still needs some fixing.
4. Added 'enums' and 'cast_operator' runtime tests.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10771 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2008-08-17 13:42:19 +00:00
Maciej Drwal
948c474a0d Modified examples' Makefiles to generate executables.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10729 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2008-08-02 16:38:27 +00:00
Maciej Drwal
dcec3c3fb0 Added std_string support. Renamed SWIG_exception to SWIG_exc to avoid name collision with macro in Lib/exception.i.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10720 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2008-07-30 22:09:02 +00:00
Maciej Drwal
7e23a5a55e Removing some memory leak problems when handling exceptions.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10713 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2008-07-26 14:18:37 +00:00