Commit graph

128 commits

Author SHA1 Message Date
Vadim Zeitlin
78e09d5372 Remove commented out code from c/std_except.i
This was added in 32e03aa13 (Many major improvements. Almost all
testsuite compiles now., 2009-04-15) and never really used.
2022-01-04 01:10:43 +01:00
Vadim Zeitlin
f06bba320a Make it possible to customize swig_check() definition
Move this function definition to cexcept.swg from the code and only
define it if SWIG_swig_check_DEFINED is not defined yet, which both
simplifies the code in C.cxx and makes exception handling code more
flexible, as it's now possible to predefine SWIG_swig_check_DEFINED
in the code injected into the "cxxheader" section to replace the default
implementation. Show an example of doing this in the documentation and
document handling exceptions with C API too.

The changes above required adding a new "cxxcode" section, corresponding
to the "implementation" part of C++ wrappers and defining a new
SWIG_CXX_WRAPPERS preprocessor symbol to allow only adding C++-specific
code when C++ wrappers are actually generated. Also improve the
documentation of the C-specific sections in the manual.
2021-12-09 00:31:14 +01:00
Vadim Zeitlin
1fd4c647b8 Add support for const references to pointers
They're used in some unit tests and we can support them by handling them
just as pointers in C++ wrappers (this was already the case for C
wrappers).
2021-12-08 03:28:44 +01:00
Vadim Zeitlin
bec35fa0c2 Define "equivalent" typecheck typemap attribute for shared_ptr<>
This is required to handle overloaded functions taking shared_ptr or raw
type correctly and is checked in cpp11_shared_ptr_overload unit test.
2021-12-08 03:09:28 +01:00
Vadim Zeitlin
b072b2dc87 Represent empty shared pointers by null pointers at C level
This is much more convenient and allows checking if the shared pointer
is empty easily, unlike before, when it couldn't be done and adding
support for it would have required adding extra functions.

Also add a way to check whether an object is null in C++ wrappers of the
classes handled via shared pointers and static null() method for
creating null objects of such classes.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
fd11a591a3 Add cxx{in,out}type typemaps and use them for std::string
This makes using returning strings much simpler to use from C++ code as
the returned pointers don't have to be deleted manually -- although, of
course, this does require an extra allocation and copy and so should be
avoided for the very long strings.

Add a new runtime test showing how simple and convenient it is to use
the functions working with string using the C++ wrappers now.
2021-12-07 20:54:28 +01:00
Vadim Zeitlin
a335fff2ec Avoid errors due to generating identical overloads
Add typecheck typemaps for primitive types and string and call
Swig_overload_check() to ensure that we don't generate two wrappers
functions taking the same "const char*" type if we have overloads taking
it and "std::string" (or a reference) in the original code.
2021-12-05 20:49:20 +01:00
Vadim Zeitlin
aacc930023 Streamline and improve std::string typemaps
Avoid unnecessary heap allocations, just use temporary variables.

Actually update the string parameters passed by pointer/non-const
reference. This requires the pointers passed to actually be non-const,
so update the C-specific unit test runme to use a char buffer instead of
a literal string.

Also simplify the code copying the string contents to just use strdup()
(if there are ever any platforms where this POSIX functions is not
available, we could just define it ourselves once instead of using
strlen() + malloc() + memcpy() manually twice).
2021-12-05 03:31:35 +01:00
Vadim Zeitlin
628e2cf6d2 Make std::{set,map} has() and has_key() methods const
They don't modify the object, so should be callable on const objects.
2021-12-04 17:47:27 +01:00
Vadim Zeitlin
bfc6623bbd Make SWIG_CException dtor public to fix memory leak
The actual dtor was never called when the wrapper object was destroyed
before, due to dtor being private.
2021-12-03 03:38:33 +01:00
Vadim Zeitlin
579c343d5f Represent enums as enums, not int, if possible
Use enum types instead of int for the enum-valued parameters and
function return values, this is more type-safe and clear for the users
of the library.

Change cpp_enum unit test to use C++ to check that C++ enum wrappers
can at least be compiled, but still use C API in it.

Note that enum whose underlying type is bigger than int still don't
work, but this is no different from what it was before, so just document
this limitation but don't do anything else about it for now.

This commit is best viewed ignoring whitespace-only changes.
2021-11-30 02:22:25 +01:00
Vadim Zeitlin
38b76bb7c6 Make global SWIG_CException::PendingException thread-local
This allows different threads to use the same library concurrently
without corrupting each other's exceptions.

Note that this requires C++11 support for wrapper compilation too. If
this really turns out to be a problem, we could always just #define
thread_local as compiler-specific equivalent such as "__thread" for gcc
or "__declspec(thread)" for MSVC, which are available since forever.
2021-11-27 00:19:05 +01:00
Vadim Zeitlin
727a65c0e8 Fix using exception in modules importing other modules
Ensure that we have only a single SWIG_CException_Raise() function
across all modules instead of having per-module functions and, worse,
per-module PendingException variables, which resulted in compile-time
errors and couldn't work anyhow because function checking for the
current exception didn't necessarily use the same "global" variable
where it was stored. More formally, old version resulted in ODR
violations and undefined behaviour.

The way we avoid it now is rather ugly and consists in excluding
SWIG_CException from wrapping using the hack in the source code which
postpones wrapping this class until the very end and checks if we had
encountered any %import directives and simply doesn't wrap it if we did.

The same code is used to define the special SWIG_CException_DEFINED
preprocessor symbol which is then used in the generated code to prevent
the SWIG_CException class declaration from being compiled as part of the
wrapper too (because this still happens due to %inline being used for
its definition, and there doesn't seem to be any better way to avoid
this).

This is definitely not pretty, but at least adding "throw(char*)" to a
couple of functions in mod_[ab].i test suite files works now instead of
failing (even without linking and running) as before. This commit
doesn't modify the test suite to avoid possible problems with the other
languages, however.
2021-11-27 00:19:05 +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
54af856d09 Fix typemaps for arrays of objects and object pointers
These typemaps never worked, but this went unnoticed until now because
the nonsensical "ctype" expansion generated by them still compiled.

With types possibly having a namespace prefix, they didn't any longer,
so define them correctly for the objects using "$resolved_type" and
define the typemap for "void*[]" separately, as "$resolved_type" can't
be used for it.
2021-11-10 00:53:39 +01:00
Vadim Zeitlin
d65d7d7df3 Make SWIGIMPORT definition more consistent with SWIGEXPORT
Use the same checks as for the definition of SWIGEXPORT in
swiglabels.swg.

Also avoid defining __DLL_IMPORT symbol with a reserved name.
2021-10-20 01:57:18 +02:00
Vadim Zeitlin
e41234c5bc Inject just the relevant part of changes.swg in generated code
Don't include the entire file, including its header comment, into the
generated code, but just the part that we want to appear there.

This looks nicer and is also more explicit and hence clear.
2021-10-15 23:16:42 +02:00
Vadim Zeitlin
e78c8f39ed Add minimal shared_ptr support
Enable the tests and support of shared_ptr in them for C (which required
disabling a previously passing, because not doing anything, attributes
test which is currently broken for unrelated reasons).
2021-10-15 01:26:22 +02:00
Vadim Zeitlin
3dd96d5b95 Allow using vector with classes without default ctor in C too
This is similar to be491506a (Java std::vector improvements for types
that do not have a default constructor., 2019-03-01) for Java, except we
don't have to bother with any compatibility constraints for this, not
yet used by anyone. module.
2021-10-07 22:03:47 +02:00
Vadim Zeitlin
5cbdae42d3 Revert "Use std/std_vector.i instead of a poor copy in c/std_vector.i"
This reverts commit d89a95a48c as,
finally, it's not worth trying to use UTL typemaps from C: this required
bad hacks for std::vector and would be even worse for std::map, so don't
bother with them and just correct the "poor" C typemaps to be slightly
less poor and use them instead.
2021-10-07 22:03:47 +02:00
Vadim Zeitlin
0706ed4d35 Add simple std::set<> typemaps too
This is similar to the previous commit for std::map<>.
2021-10-07 22:03:47 +02:00
Vadim Zeitlin
9efb508eda Fix std::map<> typemap
Use simple fixed typemap instead of trying to use the much more complex
one from the UTL which doesn't work for C.

Add a simple test case for std::map<>.
2021-10-07 22:03:47 +02:00
Vadim Zeitlin
fc1ebd7c18 Add trivial but working std::pair typemaps implementation
The existing typemaps didn't work at all and making them work would
require defining all the typemaps needed by the Unified Typemaps
Library, which seems to be geared towards dynamic languages.

Just implement completely straightforward (and not very convenient to
use) typemaps instead.

Enable the now passing unit tests and add a runme for one of them.
2019-08-07 02:16:33 +02:00
Vadim Zeitlin
fd3e763656 Streamline and fix returning objects by value
Remove the code related to "_result_ref" which was confusing and plain
wrong, as it generated something that compiled but crashed during
run-time due to the use of a pointer to an already destroyed stack
object.

Instead, correct the "out" typemap to create a new copy of the object,
which mostly works fine on its own, except that it depends on using
SwigValueWrapper if necessary, so add the call to cplus_value_type()
does this. This also required removing the code resolving typedefs in
the "type" attribute because it confused the base class logic and still
needs an explicit cast to the actual return type due to the use of (and
probable bug in) get_wrapper_func_return_type().

These changes mean that "cppouttype" typemap is not used any longer, so
remove it too.

A couple more tests pass now.
2019-08-06 02:35:25 +02:00
Vadim Zeitlin
8eda01fd75 Get rid of extra unnecessary blocks in common typemaps
Use "..." instead of "{...}" to avoid generating extra lines with
opening/closing curly braces, which are completely unnecessary.

Also remove an unnecessary "if" check in "in" typemap, which was
useless.

No real changes.
2019-08-06 00:24:07 +02:00
Vadim Zeitlin
649a1c3c98 Provide trivial SWIG_exception definition even in C mode
This is not really useful, but gets rid of an annoying warning when
running li_constraints.ctest unit test.
2019-08-05 14:15:18 +02:00
Vadim Zeitlin
fb0579a0df Simplify SWIGTYPE "in" typemap
There is no need to check if the input is non-null, as the effect of
both branches of "if" is the same anyhow.

Also get rid of an extra and unnecessary block.

No real changes.
2019-08-04 15:23:35 +02:00
Vadim Zeitlin
93ca7451e6 Also remove SWIGTYPE*& typemaps for C
They seem unnecessary too and look suspicious, notably the use of "static" in
the "out" typemap is almost certainly wrong.

As all the tests still pass without them, don't bother fixing these typemaps
but just remove them completely.
2016-09-15 01:46:28 +02:00
Vadim Zeitlin
7a6edf8b82 Remove nonsensical typemaps for SWIGTYPE**
It's not clear why they were defined in the first place, but they broke
li_std_vector_ptr unit test, which now uses "T**", after the latest merge with
master, so just remove them to let the test pass again.
2016-09-15 01:41:20 +02:00
Vadim Zeitlin
fe8c6fad44 Ignore std::vector<T>::get_allocator()
This is not really useful in C wrappers and results in ugly descriptor for the
allocator type appearing in the output, better to avoid it.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
d89a95a48c Use std/std_vector.i instead of a poor copy in c/std_vector.i
At the very least, this gives us a working vector<bool> and allows
"li_std_vector" unit test to pass. It is also just nice to reuse the existing
typemaps instead of having another copy of them.

As C doesn't have UTL, some parts of std_vector.i had to be excluded from it
however.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
29563ec98e Fix typemaps for bool
The existing typemaps didn't make much sense, simplify (there doesn't seem to
be any point in using $1_basetype when dealing with "bool") them and treat
"const bool&" as "bool", not as pointer, as this is much more convenient in C.

This also allows another unit test to pass.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
f533e3c488 Pass const references to primitive types by value
Change the typemaps to use values, not pointers, for arguments of "T const&"
type, this is much more natural in C.
2016-09-15 01:27:41 +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
8cae88dd39 Get rid of separate cmodtype typemap
Use the same ctype for wrapper declarations and definitions and just expand
$resolved_type differently in the two cases.

This simplifies the typemaps and ensures that the declarations and definitions
use the same types, at least for all non-object parameters.
2016-04-21 18:58:32 +02:00
Vadim Zeitlin
5de29df38e Simplify typemaps by merging both const and "unconst" versions
We can always just use the "unconst" one because it works when the type is
used for a return value and is the same as the other one when it's used for a
parameter anyhow because top-level const is ignored in function signatures, so
don't bother defining two sets of macros when one will do.
2016-04-21 17:55:47 +02:00
Vadim Zeitlin
806f79de14 No real changes, just rename "proxy_header" to "cheader"
Continue getting rid of "proxy" terminology, there is no proxy when using C
module, so it's confusing to use this term.
2016-04-21 15:18:22 +02:00
Vadim Zeitlin
e934c51425 Include stddef.h instead of stdio.h in the generated header
The sole reason for including this header seems to be to get size_t
declaration, so include the smallest header sufficient for this.

It would be even better to use a fragment for all size_t typemaps to only
include it if necessary, but it doesn't seem to be worth doing this right now.
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
e8c6bfbc8f Remove completely broken handling of member function pointers
Member function pointers can't possibly be represented as function pointers,
they have a strictly bigger size and attempting to do it resulted in code
which, with a lot of bad casts, compiled, but crashed during run-time.

The proper solution is to represent C++ method pointers with an appropriate
opaque type, but for now this remains broken -- just make it explicitly broken
instead of pretending that it works when it actually has no chance to.

Let "namespace_spaces" unit test pass, as it's not really related to the
function pointers, by adding an explicit SWIGC test to it.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
7a392fd21f Fix typemaps for "void*"
Pass void pointers directly to C, don't handle them objects which was done by
default previously because we didn't define any specific typemaps for them.

This allows to reenable another test.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
85c9b87f9b Fix ctype typemap for bool& too
Don't add an extra pointer, this is already done at the code level currently.

This makes the "bools" unit test pass.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
18cb946405 Fix cmodtype typemap for "const bool &"
We need bool to be "const", not the pointer.

This fixes a few of the errors in the "bools" unit test.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
8104c24f71 Fix typemaps for templates with more than one parameter
Ensure that any commas inside SWIG_STR() used in "out" typemaps for class
types are not interpreted as macro argument separators by using an extra pair
of parenthesis around the type.
2016-04-20 01:53:26 +02:00
Vadim Zeitlin
0be93325a9 Fix "out" typemap for enum pointers/references
Taking the address of C++ result doesn't make sense here, the C++ result is
already a pointer and so should be just returned as is.
2016-04-16 00:44:41 +02:00
Vadim Zeitlin
27abb84113 Fix runtime code when not using exceptions
Add the missing "%insert" line which had been somehow lost.
2016-04-16 00:09:09 +02:00
Vadim Zeitlin
818b399c9e Drastically simplify and optimize object creation and destruction
Don't populate the typenames array for each and every created object and don't
search it when deleting every object, this is O(N) in number of classes and is
completely impractical for any big library where N can easily be several
thousands. Use destructor function which will correctly destroy the object
instead.

Also don't store each created objects in the global registry, there doesn't
seem to be any point in it.

And, in fact, don't bother with the typenames at all, just use another
pseudo-virtual function for checking whether a class inherits from a class
with the given name.

There is unfortunately one problem with the new approach: it doesn't work when
the same C++ type is wrapped under different names as this results in multiple
specializations of SWIG_derives_from<> for this type. But this is a relatively
rare situation (but which does arise in template_default2 unit test, which had
to be disabled) and could be fixed in the future by completely resolving the
type, including the default template parameters values, and checking if
SWIG_derives_from had been already specialized for it. In the meanwhile, this
regression is not a big deal compared to the advantages of the new approach.
2016-04-15 23:25:32 +02:00
Vadim Zeitlin
42c3d420b2 Remove SWIG_temporary from the generated code
This doesn't seem to be used anywhere.
2016-04-14 02:44:46 +02:00
Vadim Zeitlin
e8ec13f56b Remove SWIG_MAKE_DELETE() macro from C runtime
This doesn't seem to be necessary nor even useful at all, just get rid of it
completely and expand it manually in the only place where it was used.
2016-04-13 18:10:36 +02:00
Vadim Zeitlin
6bbe375a37 Remove unused SWIGPROTECT
It doesn't make any sense to use protected visibility for the symbols in the
proxy code, the only use for it would be if it could be applied to the
original functions being wrapped, to prevent them from being shadowed by the
proxy versions, but this cannot be done, in general.

Apparently this was already understood as the code using SWIGPROTECT was
commented out, but ef85d0d43f didn't give any
reason for doing this, nor for leaving this code.

Assume there was none and just remove it completely now to avoid confusion.
2016-04-13 18:10:35 +02:00
Leif Middelschulte
49735451ee Update comments in typemaps file.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-c@13682 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2012-08-20 13:23:29 +00:00