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
This commit is contained in:
Maciej Drwal 2008-08-17 13:42:19 +00:00
commit 54860c9595
6 changed files with 505 additions and 295 deletions

View file

@ -1,3 +1,7 @@
/*
* NOTE: this won't run with -noexcept flag
*/
#include <stdio.h>
#include "example_proxy.h"

View file

@ -0,0 +1,12 @@
#include <stdio.h>
#include "cast_operator/cast_operator_proxy.h"
int main() {
A *a = new_A();
if (strcmp(A_tochar(a), "hi"))
fprintf(stderr, "cast failed\n");
delete_A(a);
SWIG_exit(0);
}

View file

@ -0,0 +1,11 @@
#include <stdio.h>
#include "enums/enums_proxy.h"
int main() {
bar2(1);
bar3(1);
bar1(1);
SWIG_exit(0);
}

View file

@ -14,10 +14,7 @@
#include <string.h>
#include <setjmp.h>
typedef struct {
void *obj;
const char **typenames;
} SwigObj;
#define SWIG_STR(x) #x
%}
// typemaps for function parameters
@ -36,10 +33,12 @@ typedef struct {
%typemap(ctype) const void *, const short *, const int *, const long *, const char *, const float *, const double * "$1_type"
%typemap(ctype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$1_basetype *"
%typemap(ctype) void * [ANY], short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY] "$1_basetype **"
%typemap(ctype) void [ANY][ANY], short [ANY][ANY], int [ANY][ANY], long [ANY][ANY], char [ANY][ANY], float [ANY][ANY], double [ANY][ANY] "$1_basetype **"
%typemap(ctype) SWIGTYPE "SwigObj *"
%typemap(ctype) SWIGTYPE * "SwigObj *"
%typemap(ctype) SWIGTYPE & "SwigObj *"
%typemap(ctype) SWIGTYPE * [ANY] "SwigObj *"
%typemap(ctype) SWIGTYPE * [ANY], SWIGTYPE ** "SwigObj **"
%typemap(ctype) SWIGTYPE * [ANY][ANY], SWIGTYPE *** "SwigObj ***"
%typemap(ctype) enum SWIGTYPE "$1_type"
%fragment("stdbool_inc", "proxy_header") {#include <stdbool.h>}
@ -77,18 +76,40 @@ typedef struct {
$1 = ($1_ltype) $input->obj;
}
%typemap(in) SWIGTYPE * [ANY] {
%typemap(in) SWIGTYPE * [ANY], SWIGTYPE ** {
if ($input) {
$1 = ($1_basetype *) malloc($1_dim0 * sizeof($1_basetype));
int i;
for (i = 0; i < $1_dim0; ++i)
$1 = ($1_ltype) malloc($1_dim0 * sizeof($1_basetype));
size_t i = 0;
for ( ; i < $1_dim0; ++i)
if ($input[i])
$1[i] = ($1_basetype) $input[i]->obj;
$1[i] = ($*1_ltype) $input[i]->obj;
else
$1[i] = ($1_basetype) 0;
$1[i] = ($*1_ltype) 0;
}
else
$1 = ($1_basetype*) 0;
$1 = ($1_ltype) 0;
}
%typemap(freearg) SWIGTYPE * [ANY], SWIGTYPE * [ANY][ANY], SWIGTYPE **, SWIGTYPE *** {
if (arg2)
free(arg2);
}
%typemap(in) SWIGTYPE * [ANY][ANY], SWIGTYPE *** {
if ($input) {
$1 = ($1_ltype) malloc($1_dim0 * $1_dim1 * sizeof($1_basetype));
size_t i = 0, j = 0;
for ( ; i < $1_dim0; ++i) {
for ( ; j < $1_dim1; ++j) {
if ($input[i][j])
$1[i][j] = * ($*1_ltype) $input[i][j]->obj;
else
$1[i][j] = * ($*1_ltype) 0;
}
}
}
else
$1 = ($1_ltype) 0;
}
%typemap(in) SWIGTYPE & {
@ -110,6 +131,8 @@ typedef struct {
%typemap(couttype) SWIGTYPE "SwigObj *"
%typemap(couttype) SWIGTYPE * "SwigObj *"
%typemap(couttype) SWIGTYPE & "SwigObj *"
%typemap(couttype) SWIGTYPE * [ANY], SWIGTYPE ** "SwigObj **"
%typemap(couttype) SWIGTYPE * [ANY][ANY], SWIGTYPE *** "SwigObj ***"
%typemap(couttype) enum SWIGTYPE "$1_type"
%typemap(couttype, fragment="stdbool_inc") bool, bool *, const bool, const bool * "$1_type"
@ -134,273 +157,84 @@ typedef struct {
%typemap(out) enum SWIGTYPE "$result = ($1_type) $1;"
// allocate new "object-struct" by default
%typemap(out) SWIGTYPE {
$result = SWIG_create_object(SWIG_STR($1_type));
$result->obj = (void*) &$1;
}
%typemap(out) SWIGTYPE *, SWIGTYPE & {
$result = SWIG_create_object(SWIG_STR($1_basetype));
$result->obj = (void*) $1;
}
// exception handling
%typemap(throws) BASIC_INT_TYPES {
char error_msg[256];
sprintf(error_msg, "C++ $1_type exception thrown, value: %d", $1);
SWIG_CThrowException(0, error_msg);
}
%apply BASIC_INT_TYPES { int, long, short, unsigned int, unsigned long, unsigned short, int &, long &, short &, unsigned int &, unsigned long &, unsigned short & };
%typemap(throws) char *, const char * {
SWIG_CThrowException(0, $1);
}
// this should match only SwigObj objects
%typemap(throws) SWIGTYPE {
SwigObj *c_ex;
c_ex = SWIG_create_object(SWIG_STR($1_basetype));
c_ex->obj = (void*) &$1;
SWIG_CThrowException(c_ex, "C++ $1_type exception thrown");
}
%typemap(throws) SWIGTYPE * {
SwigObj *c_ex;
c_ex = SWIG_create_object(SWIG_STR($1_basetype));
c_ex->obj = (void*) $1;
SWIG_CThrowException(c_ex, "C++ $1_type exception thrown");
}
%insert("runtime") %{
#define SWIG_STR(x) #x
#define SWIG_MAX_RT_STACK 256
#define SWIG_REGISTRY_INIT 256
SWIGINTERN SwigObj **SWIG_registry_base = 0;
SWIGINTERN SwigObj **SWIG_registry = 0;
SWIGINTERN int SWIG_registry_size = SWIG_REGISTRY_INIT;
SWIGINTERN SwigObj *SWIG_create_object(const char *classname);
SWIGINTERN void SWIG_destroy_object(SwigObj *object);
SWIGEXPORTC struct SWIG_exc_struct {
int code;
char *msg;
SwigObj *klass;
int handled;
} SWIG_exc = { 0, 0, 0, 0 };
SWIGEXPORTC jmp_buf SWIG_rt_env;
SWIGEXPORTC int SWIG_rt_init = 0;
SWIGINTERN jmp_buf SWIG_cpp_back_env;
SWIGINTERN jmp_buf *SWIG_rt_stack_base = 0;
SWIGINTERN jmp_buf *SWIG_rt_stack_ptr = 0;
SWIGINTERN void SWIG_rt_stack_push() {
// TODO: check for stack overflow
memcpy(SWIG_rt_stack_ptr, SWIG_rt_env, sizeof(SWIG_rt_env));
SWIG_rt_stack_ptr++;
}
SWIGINTERN void SWIG_rt_stack_pop() {
if (SWIG_rt_stack_ptr == SWIG_rt_stack_base)
return;
SWIG_rt_stack_ptr--;
memcpy(SWIG_rt_env, SWIG_rt_stack_ptr, sizeof(SWIG_rt_env));
}
SWIGINTERN void SWIG_add_registry_entry(SwigObj *entry) {
if (SWIG_registry_base == 0) {
SWIG_registry_base = SWIG_registry = (SwigObj **) malloc(SWIG_registry_size * sizeof(SwigObj *));
memset(SWIG_registry_base, 0, SWIG_registry_size * sizeof(SwigObj *));
}
*SWIG_registry = entry;
SWIG_registry++;
if ((SWIG_registry - SWIG_registry_base) == SWIG_registry_size) {
SWIG_registry = SWIG_registry_base;
SWIG_registry_size += SWIG_REGISTRY_INIT;
int new_size = SWIG_registry_size * sizeof(SwigObj *);
SWIG_registry_base = (SwigObj **) malloc(new_size);
memset(SWIG_registry_base, 0, new_size);
memcpy(SWIG_registry_base, SWIG_registry, (SWIG_registry_size - SWIG_REGISTRY_INIT) * sizeof(SwigObj *));
free(SWIG_registry);
SWIG_registry = SWIG_registry_base + (SWIG_registry_size - SWIG_REGISTRY_INIT);
%typemap(out) SWIGTYPE * [ANY], SWIGTYPE ** {
if ($1) {
$result = (SwigObj**) malloc($1_dim0 * sizeof(SwigObj*));
size_t i = 0;
for ( ; i < $1_dim0; ++i)
if ($1[i]) {
$result[i] = SWIG_create_object(SWIG_STR($1_ltype));
$result[i]->obj = (void*) $1[i];
}
else
$result[i] = (SwigObj*) 0;
}
else
$result = (SwigObj**) 0;
}
SWIGINTERN void SWIG_remove_registry_entry(SwigObj *entry) {
int i;
for (i = 0; i < SWIG_registry_size; ++i) {
if (*(SWIG_registry_base + i) == entry) {
*(SWIG_registry_base + i) = 0;
break;
}
}
}
SWIGINTERN void SWIG_cleanup() {
if (SWIG_rt_stack_base)
free(SWIG_rt_stack_base);
if (SWIG_exc.msg)
free(SWIG_exc.msg);
if (SWIG_exc.klass) {
if (SWIG_exc.klass->typenames)
free(SWIG_exc.klass->typenames);
free(SWIG_exc.klass);
}
int i;
if (SWIG_registry_base) {
for (i = 0; i < SWIG_registry_size; ++i) {
if (*(SWIG_registry_base + i)) {
SWIG_destroy_object(*(SWIG_registry_base + i));
*(SWIG_registry_base + i) = 0;
%typemap(out) SWIGTYPE * [ANY][ANY], SWIGTYPE *** {
if ($1) {
$result = (SwigObj***) malloc($1_dim0 * $1_dim1 * sizeof(SwigObj*));
size_t i = 0, j = 0;
for ( ; i < $1_dim0; ++i) {
for ( ; j < $1_dim1; ++j) {
if ($1[i][j]) {
$result[i][j] = SWIG_create_object(SWIG_STR($1_ltype));
$result[i][j]->obj = (void*) $1[i][j];
}
else
$result[i][j] = (SwigObj*) 0;
}
}
}
free(SWIG_registry_base);
SWIG_registry_base = 0;
else
$result = (SwigObj***) 0;
}
#ifdef SWIG_C_EXCEPT
%insert("runtime") %{
typedef struct {
void *obj;
const char **typenames;
} SwigObj;
%}
%include "cexcept.swg"
#else
%insert("runtime") %{
typedef struct {
void *obj;
} SwigObj;
#ifdef __cplusplus
extern "C" {
#endif
SWIGEXPORTC void SWIG_rt_try() {
SWIG_rt_stack_push();
}
SWIGEXPORTC int SWIG_rt_catch(const char *type) {
int result = 0;
if (!type || (strcmp("SWIG_AnyException", type) == 0)) {
result = 1;
}
else if (SWIG_exc.klass) {
int i = 0;
while (SWIG_exc.klass->typenames[i]) {
if (strcmp(SWIG_exc.klass->typenames[i++], type) == 0) {
result = 1;
break;
}
}
}
if (result) {
SWIG_rt_stack_pop();
SWIG_exc.handled = 1;
}
return result;
}
SWIGEXPORTC void SWIG_rt_throw(SwigObj *klass, const char *msg) {
if (SWIG_exc.msg) {
free(SWIG_exc.msg);
SWIG_exc.msg = (char *) 0;
}
if (msg) {
SWIG_exc.msg = (char *) malloc(strlen(msg) + 1);
strcpy(SWIG_exc.msg, msg);
}
SWIG_exc.klass = klass;
SWIG_exc.handled = 0;
longjmp(SWIG_rt_env, 1);
}
SWIGEXPORTC void SWIG_rt_unhandled() {
if (SWIG_exc.msg) {
free(SWIG_exc.msg);
SWIG_exc.msg = 0;
}
SWIG_rt_stack_pop();
longjmp(SWIG_rt_env, SWIG_exc.code);
}
SWIGEXPORTC void SWIG_rt_endtry() {
if (SWIG_exc.handled) {
if (setjmp(SWIG_rt_env) == 0) {
SWIG_rt_stack_push();
longjmp(SWIG_cpp_back_env, 1);
}
}
else {
SWIG_rt_stack_pop(); // pop the SWIG_try context
}
}
SWIGEXPORTC int SWIG_exit(int code) {
SWIG_cleanup();
exit(code);
}
#ifdef __cplusplus
}
#endif
SWIGINTERN void SWIG_terminate() {
fprintf(stderr, "Unhandled exception: %s\n%s\nExitting...\n",
SWIG_exc.klass->typenames[0],
SWIG_exc.msg ? SWIG_exc.msg : "");
SWIG_exit(SWIG_exc.code);
}
SWIGINTERN void SWIG_Runtime_init() {
int i, code;
if (!SWIG_rt_init) {
SWIG_rt_init = 1;
SWIG_rt_stack_base = SWIG_rt_stack_ptr = (jmp_buf *) malloc(sizeof(jmp_buf) * SWIG_MAX_RT_STACK);
if (SWIG_exc.code = setjmp(SWIG_rt_env)) {
// deallocate C++ exception
if (setjmp(SWIG_rt_env) == 0) {
SWIG_rt_stack_push();
SWIG_exc.handled = 1;
longjmp(SWIG_cpp_back_env, 1);
}
SWIG_terminate();
}
}
}
#define SWIG_CThrowException(klass, msg) \
if (setjmp(SWIG_cpp_back_env) == 0) \
SWIG_rt_throw((SwigObj *) klass, msg);
%}
%insert("proxy_header") %{
// special value indicating any type of exception like 'catch(...)'
#define SWIG_AnyException "SWIG_AnyException"
#include <setjmp.h>
SWIGIMPORT jmp_buf SWIG_rt_env;
typedef struct {
void *obj;
const char **typenames;
} SwigObj;
SWIGIMPORT struct SWIG_exc_struct {
int code;
char *msg;
SwigObj *klass;
} SWIG_exc;
SWIGIMPORT void SWIG_rt_try();
SWIGIMPORT int SWIG_rt_catch(const char *type);
SWIGIMPORT void SWIG_rt_throw(SwigObj *klass, const char * msg);
SWIGIMPORT int SWIG_rt_unhandled();
SWIGIMPORT void SWIG_rt_endtry();
SWIGIMPORT int SWIG_exit(int code);
#define SWIG_try \
SWIG_rt_try(); \
if ((SWIG_exc.code = setjmp(SWIG_rt_env)) == 0)
#define SWIG_catch(type) else if (SWIG_rt_catch(#type))
#define SWIG_throw(klass) SWIG_rt_throw((SwigObj *) klass, 0);
#define SWIG_throw_msg(klass, msg) SWIG_rt_throw((SwigObj *) klass, msg);
#define SWIG_endtry else SWIG_rt_unhandled(); SWIG_rt_endtry();
%}
#endif

260
Lib/c/cexcept.swg Normal file
View file

@ -0,0 +1,260 @@
/* -----------------------------------------------------------------------------
* clabels.swg
*
* Exception handling code and typemaps for C module.
* ----------------------------------------------------------------------------- */
%typemap(throws) BASIC_INT_TYPES {
char error_msg[256];
sprintf(error_msg, "C++ $1_type exception thrown, value: %d", $1);
SWIG_CThrowException(0, error_msg);
}
%apply BASIC_INT_TYPES { int, long, short, unsigned int, unsigned long, unsigned short, int &, long &, short &, unsigned int &, unsigned long &, unsigned short & };
%typemap(throws) char *, const char * {
SWIG_CThrowException(0, $1);
}
// this should match only SwigObj objects
%typemap(throws) SWIGTYPE {
SwigObj *c_ex;
c_ex = SWIG_create_object(SWIG_STR($1_basetype));
c_ex->obj = (void*) &$1;
SWIG_CThrowException(c_ex, "C++ $1_type exception thrown");
}
%typemap(throws) SWIGTYPE * {
SwigObj *c_ex;
c_ex = SWIG_create_object(SWIG_STR($1_basetype));
c_ex->obj = (void*) $1;
SWIG_CThrowException(c_ex, "C++ $1_type exception thrown");
}
%insert("runtime") %{
#define SWIG_MAX_RT_STACK 256
#define SWIG_REGISTRY_INIT 256
SWIGINTERN SwigObj **SWIG_registry_base = 0;
SWIGINTERN SwigObj **SWIG_registry = 0;
SWIGINTERN int SWIG_registry_size = SWIG_REGISTRY_INIT;
SWIGINTERN SwigObj *SWIG_create_object(const char *classname);
SWIGINTERN void SWIG_destroy_object(SwigObj *object);
SWIGEXPORTC struct SWIG_exc_struct {
int code;
char *msg;
SwigObj *klass;
int handled;
} SWIG_exc = { 0, 0, 0, 0 };
SWIGEXPORTC jmp_buf SWIG_rt_env;
SWIGEXPORTC int SWIG_rt_init = 0;
SWIGINTERN jmp_buf SWIG_cpp_back_env;
SWIGINTERN jmp_buf *SWIG_rt_stack_base = 0;
SWIGINTERN jmp_buf *SWIG_rt_stack_ptr = 0;
SWIGINTERN void SWIG_rt_stack_push() {
// TODO: check for stack overflow
memcpy(SWIG_rt_stack_ptr, SWIG_rt_env, sizeof(SWIG_rt_env));
SWIG_rt_stack_ptr++;
}
SWIGINTERN void SWIG_rt_stack_pop() {
if (SWIG_rt_stack_ptr == SWIG_rt_stack_base)
return;
SWIG_rt_stack_ptr--;
memcpy(SWIG_rt_env, SWIG_rt_stack_ptr, sizeof(SWIG_rt_env));
}
SWIGINTERN void SWIG_add_registry_entry(SwigObj *entry) {
if (SWIG_registry_base == 0) {
SWIG_registry_base = SWIG_registry = (SwigObj **) malloc(SWIG_registry_size * sizeof(SwigObj *));
memset(SWIG_registry_base, 0, SWIG_registry_size * sizeof(SwigObj *));
}
*SWIG_registry = entry;
SWIG_registry++;
if ((SWIG_registry - SWIG_registry_base) == SWIG_registry_size) {
SWIG_registry = SWIG_registry_base;
SWIG_registry_size += SWIG_REGISTRY_INIT;
int new_size = SWIG_registry_size * sizeof(SwigObj *);
SWIG_registry_base = (SwigObj **) malloc(new_size);
memset(SWIG_registry_base, 0, new_size);
memcpy(SWIG_registry_base, SWIG_registry, (SWIG_registry_size - SWIG_REGISTRY_INIT) * sizeof(SwigObj *));
free(SWIG_registry);
SWIG_registry = SWIG_registry_base + (SWIG_registry_size - SWIG_REGISTRY_INIT);
}
}
SWIGINTERN void SWIG_remove_registry_entry(SwigObj *entry) {
int i;
for (i = 0; i < SWIG_registry_size; ++i) {
if (*(SWIG_registry_base + i) == entry) {
*(SWIG_registry_base + i) = 0;
break;
}
}
}
SWIGINTERN void SWIG_cleanup() {
if (SWIG_rt_stack_base)
free(SWIG_rt_stack_base);
if (SWIG_exc.msg)
free(SWIG_exc.msg);
if (SWIG_exc.klass) {
if (SWIG_exc.klass->typenames)
free(SWIG_exc.klass->typenames);
free(SWIG_exc.klass);
}
int i;
if (SWIG_registry_base) {
for (i = 0; i < SWIG_registry_size; ++i) {
if (*(SWIG_registry_base + i)) {
SWIG_destroy_object(*(SWIG_registry_base + i));
*(SWIG_registry_base + i) = 0;
}
}
}
free(SWIG_registry_base);
SWIG_registry_base = 0;
}
#ifdef __cplusplus
extern "C" {
#endif
SWIGEXPORTC void SWIG_rt_try() {
SWIG_rt_stack_push();
}
SWIGEXPORTC int SWIG_rt_catch(const char *type) {
int result = 0;
if (!type || (strcmp("SWIG_AnyException", type) == 0)) {
result = 1;
}
else if (SWIG_exc.klass) {
int i = 0;
while (SWIG_exc.klass->typenames[i]) {
if (strcmp(SWIG_exc.klass->typenames[i++], type) == 0) {
result = 1;
break;
}
}
}
if (result) {
SWIG_rt_stack_pop();
SWIG_exc.handled = 1;
}
return result;
}
SWIGEXPORTC void SWIG_rt_throw(SwigObj *klass, const char *msg) {
if (SWIG_exc.msg) {
free(SWIG_exc.msg);
SWIG_exc.msg = (char *) 0;
}
if (msg) {
SWIG_exc.msg = (char *) malloc(strlen(msg) + 1);
strcpy(SWIG_exc.msg, msg);
}
SWIG_exc.klass = klass;
SWIG_exc.handled = 0;
longjmp(SWIG_rt_env, 1);
}
SWIGEXPORTC void SWIG_rt_unhandled() {
if (SWIG_exc.msg) {
free(SWIG_exc.msg);
SWIG_exc.msg = 0;
}
SWIG_rt_stack_pop();
longjmp(SWIG_rt_env, SWIG_exc.code);
}
SWIGEXPORTC void SWIG_rt_endtry() {
if (SWIG_exc.handled) {
if (setjmp(SWIG_rt_env) == 0) {
SWIG_rt_stack_push();
longjmp(SWIG_cpp_back_env, 1);
}
}
else {
SWIG_rt_stack_pop(); // pop the SWIG_try context
}
}
SWIGEXPORTC int SWIG_exit(int code) {
SWIG_cleanup();
exit(code);
}
#ifdef __cplusplus
}
#endif
SWIGINTERN void SWIG_terminate() {
fprintf(stderr, "Unhandled exception: %s\n%s\nExitting...\n",
SWIG_exc.klass->typenames[0],
SWIG_exc.msg ? SWIG_exc.msg : "");
SWIG_exit(SWIG_exc.code);
}
SWIGINTERN void SWIG_runtime_init() {
int i, code;
if (!SWIG_rt_init) {
SWIG_rt_init = 1;
SWIG_rt_stack_base = SWIG_rt_stack_ptr = (jmp_buf *) malloc(sizeof(jmp_buf) * SWIG_MAX_RT_STACK);
if (SWIG_exc.code = setjmp(SWIG_rt_env)) {
// deallocate C++ exception
if (setjmp(SWIG_rt_env) == 0) {
SWIG_rt_stack_push();
SWIG_exc.handled = 1;
longjmp(SWIG_cpp_back_env, 1);
}
SWIG_terminate();
}
}
}
#define SWIG_CThrowException(klass, msg) \
if (setjmp(SWIG_cpp_back_env) == 0) \
SWIG_rt_throw((SwigObj *) klass, msg);
%}
%insert("proxy_header") %{
// special value indicating any type of exception like 'catch(...)'
#define SWIG_AnyException "SWIG_AnyException"
#include <setjmp.h>
SWIGIMPORT jmp_buf SWIG_rt_env;
typedef struct {
void *obj;
const char **typenames;
} SwigObj;
SWIGIMPORT struct SWIG_exc_struct {
int code;
char *msg;
SwigObj *klass;
} SWIG_exc;
SWIGIMPORT void SWIG_rt_try();
SWIGIMPORT int SWIG_rt_catch(const char *type);
SWIGIMPORT void SWIG_rt_throw(SwigObj *klass, const char * msg);
SWIGIMPORT int SWIG_rt_unhandled();
SWIGIMPORT void SWIG_rt_endtry();
SWIGIMPORT int SWIG_exit(int code);
#define SWIG_try \
SWIG_rt_try(); \
if ((SWIG_exc.code = setjmp(SWIG_rt_env)) == 0)
#define SWIG_catch(type) else if (SWIG_rt_catch(#type))
#define SWIG_throw(klass) SWIG_rt_throw((SwigObj *) klass, 0);
#define SWIG_throw_msg(klass, msg) SWIG_rt_throw((SwigObj *) klass, msg);
#define SWIG_endtry else SWIG_rt_unhandled(); SWIG_rt_endtry();
%}

View file

@ -32,8 +32,10 @@ class C:public Language {
String *create_object;
String *destroy_object;
int enum_id;
bool proxy_flag;
bool runtime_flag;
bool except_flag;
public:
@ -47,8 +49,9 @@ public:
enum_values(0),
create_object(0),
destroy_object(0),
enum_id(1),
proxy_flag(true),
runtime_flag(true) {
except_flag(true) {
}
/* ------------------------------------------------------------
@ -57,17 +60,6 @@ public:
virtual void main(int argc, char *argv[]) {
SWIG_library_directory("c");
// add a symbol to the parser for conditional compilation
Preprocessor_define("SWIGC 1", 0);
if (runtime_flag)
Preprocessor_define("SWIG_C_RUNTIME 1", 0);
// add typemap definitions
SWIG_typemap_lang("c");
SWIG_config_file("c.swg");
// look for certain command line options
for (int i = 1; i < argc; i++) {
if (argv[i]) {
@ -79,15 +71,24 @@ public:
} else if (strcmp(argv[i], "-noproxy") == 0) {
proxy_flag = false;
Swig_mark_arg(i);
} else if (strcmp(argv[i], "-noruntime") == 0) {
runtime_flag = false;
} else if (strcmp(argv[i], "-noexcept") == 0) {
except_flag = false;
Swig_mark_arg(i);
}
}
}
if (!CPlusPlus)
runtime_flag = false;
// add a symbol to the parser for conditional compilation
Preprocessor_define("SWIGC 1", 0);
Preprocessor_define("SWIG_C_RUNTME 1", 0);
if (except_flag)
Preprocessor_define("SWIG_C_EXCEPT 1", 0);
SWIG_library_directory("c");
// add typemap definitions
SWIG_typemap_lang("c");
SWIG_config_file("c.swg");
allow_overloading();
}
@ -98,8 +99,9 @@ public:
void start_create_object() {
String *s = create_object = NewString("");
Printf(s, "\nSWIGINTERN SwigObj *SWIG_create_object(const char *classname) {\n");
Printf(s, "SWIG_Runtime_init();\n");
Printv(s, "\nSWIGINTERN SwigObj *SWIG_create_object(", except_flag ? "const char *classname" : "", ") {\n", NIL);
if (except_flag)
Printf(s, "SWIG_runtime_init();\n");
Printf(s, "SwigObj *result;\n");
Printf(s, "result = (SwigObj *) malloc(sizeof(SwigObj));\n");
Printf(s, "result->obj = 0;\n");
@ -123,7 +125,9 @@ public:
void start_destroy_object() {
String *s = destroy_object = NewString("");
Printf(s, "\nSWIGINTERN void SWIG_destroy_object(SwigObj *object) {\n");
Printf(s, "if (object)\nif (object->typenames) {\n");
Printf(s, "if (object) {\n");
if (except_flag)
Printf(s, "if (object->typenames) {\n");
}
/* ---------------------------------------------------------------------
@ -132,9 +136,12 @@ public:
String *finish_destroy_object() {
String *s = destroy_object;
Printf(s, "free(object->typenames);\n");
if (except_flag)
Printf(s, "free(object->typenames);\n");
Printf(s, "free(object);\n");
Printf(s, "object = (SwigObj *) 0;\n");
if (except_flag)
Printf(s, "}\n");
Printf(s, "}\n}\n");
return destroy_object;
}
@ -198,13 +205,15 @@ public:
Printf(f_wrappers, "#endif\n\n");
start_create_object();
start_destroy_object();
if (except_flag)
start_destroy_object();
// emit code for children
Language::top(n);
Append(f_header, finish_create_object());
Append(f_header, finish_destroy_object());
if (except_flag)
Append(f_header, finish_destroy_object());
Printf(f_wrappers, "#ifdef __cplusplus\n");
Printf(f_wrappers, "}\n");
@ -245,6 +254,8 @@ public:
return SWIG_OK;
String *name = Getattr(n, "name");
SwigType *type = Getattr(n, "type");
if (SwigType_isenum(type))
type = make_enum_type(n, type);
String *type_str = SwigType_str(type, 0);
Printv(f_proxy_header, "SWIGIMPORT ", type_str, " ", name, ";\n\n", NIL);
return SWIG_OK;
@ -391,6 +402,8 @@ ready:
Node *node = Swig_symbol_clookup(query, symtab);
if (node)
newtype = NewStringf("enum %s", Getattr(node, "name"));
else
newtype = SwigType_str(enumtype, 0);
return newtype;
}
@ -511,10 +524,10 @@ ready:
Wrapper_add_localv(wrapper, "cppresult", "int", "cppresult", NIL);
}
else if (SwigType_isbuiltin(SwigType_base(type))) {
// type is built-in (int, char, double, etc.)
if (SwigType_isconst(type))
SwigType_del_qualifier(type);
// type is built-in (int, char, double, etc.)
if (SwigType_isreference(type)) {
if (SwigType_isconst(SwigType_del_reference(type))) {
return_var_type = SwigType_base(type);
@ -529,7 +542,12 @@ ready:
}
else if (SwigType_isarray(type)) {
return_var_type = SwigType_base(type);
SwigType_add_pointer(return_var_type);
SwigType *atype = Copy(type);
do {
SwigType_del_array(atype);
SwigType_add_pointer(return_var_type);
} while (SwigType_isarray(atype));
Delete(atype);
}
else {
return_var_type = type;
@ -541,10 +559,21 @@ ready:
// type is class
if (SwigType_ispointer(type))
return_var_type = type;
else if (SwigType_isreference(type) || SwigType_isarray(type)) {
else if (SwigType_isreference(type)) {
return_var_type = SwigType_base(type);
SwigType_add_pointer(return_var_type);
}
else if (SwigType_isarray(type)) {
return_var_type = SwigType_base(type);
SwigType *atype = Copy(type);
do {
SwigType_del_array(atype);
SwigType_add_pointer(return_var_type);
} while (SwigType_isarray(atype));
Delete(atype);
if (Cmp(Getattr(n, "c:retval"), "1"))
SwigType_add_pointer(return_var_type);
}
else {
return_var_type = type;
SwigType_add_pointer(return_var_type);
@ -639,6 +668,7 @@ ready:
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(type, 0));
p = nextSibling(p);
}
Delete(arg_name);
Delete(proxy_parm_type);
Delete(c_parm_type);
@ -669,7 +699,7 @@ ready:
Replaceall(mod, "$mod", "");
else if (SwigType_isenum(type))
Replaceall(mod, "$mod", "(int)");
else if (return_object && Getattr(n, "c:retval"))
else if (return_object && Getattr(n, "c:retval") && !SwigType_isarray(type))
Replaceall(mod, "$mod", "&");
else {
Delete(mod);
@ -832,7 +862,8 @@ ready:
}
}
Printv(f_header, "const char* Swig_typename_", name, " = \"", name, "\";\n\n", NIL);
if (except_flag)
Printv(f_header, "const char* Swig_typename_", name, " = \"", name, "\";\n\n", NIL);
// declare type for specific class in the proxy header
if (proxy_flag)
@ -1006,6 +1037,26 @@ ready:
* --------------------------------------------------------------------- */
virtual int membervariableHandler(Node *n) {
SwigType *type = Copy(Getattr(n, "type"));
SwigType *tdtype;
tdtype = SwigType_typedef_resolve(type);
if (tdtype)
type = tdtype;
int array_count = 0;
while (SwigType_isarray(type)) {
SwigType_del_array(type);
array_count++;
}
if (type) {
if (!SwigType_ispointer(type) && !SwigType_isreference(type))
Setattr(n, "c:retval", "1");
}
while (array_count) {
SwigType_add_pointer(type);
array_count--;
}
Delete(type);
return Language::membervariableHandler(n);
}
@ -1078,10 +1129,16 @@ ready:
Setattr(n, "sym:name", constr_name);
// generate action code
add_to_create_object(n, classname, newclassname);
Printv(code, "result = SWIG_create_object(\"", classname, "\");\n", NIL);
if (except_flag) {
add_to_create_object(n, classname, newclassname);
Printv(code, "result = SWIG_create_object(\"", classname, "\");\n", NIL);
Printf(code, "SWIG_add_registry_entry(result);\n");
}
else {
Printf(code, "result = SWIG_create_object();\n");
}
Printv(code, "result->obj = (void*) new ", classname, arg_lnames, ";\n", NIL);
Printf(code, "SWIG_add_registry_entry(result);\n");
Setattr(n, "wrap:action", code);
@ -1129,10 +1186,17 @@ ready:
Setattr(n, "sym:name", constr_name);
// generate action code
add_to_create_object(n, classname, newclassname);
Printv(code, "result = SWIG_create_object(\"", classname, "\");\n", NIL);
Printv(code, "result->obj = (void*) new ", classname, "((", classname, " const &)*arg1);\n", NIL);
if (except_flag) {
add_to_create_object(n, classname, newclassname);
Printv(code, "result = SWIG_create_object(\"", classname, "\");\n", NIL);
Printf(code, "SWIG_add_registry_entry(result);\n");
}
else {
Printf(code, "result = SWIG_create_object();\n");
}
Printv(code, "result->obj = (void*) new ", classname, "((", classname, " const &)*arg1);\n", NIL);
Setattr(n, "wrap:action", code);
functionWrapper(n);
@ -1150,7 +1214,9 @@ ready:
* --------------------------------------------------------------------- */
virtual int destructorHandler(Node *n) {
String *newclassname = Getattr(Swig_methodclass(n), "sym:name");
Node *klass = Swig_methodclass(n);
String *classname = Getattr(klass, "classtype");
String *newclassname = Getattr(klass, "sym:name");
String *sobj_name = NewString("");
String *ctype;
String *stype;
@ -1176,8 +1242,14 @@ ready:
Setattr(n, "sym:name", destr_name);
// create action code
Printf(code, "SWIG_remove_registry_entry(carg1);\n");
Printf(code, "SWIG_destroy_object(arg1);\n");
if (except_flag) {
Printf(code, "SWIG_remove_registry_entry(carg1);\n");
Printf(code, "SWIG_destroy_object(arg1);\n");
}
else {
Printv(code, "if (carg1->obj)\ndelete (", classname, " *) (carg1->obj);\n", NIL);
}
Setattr(n, "wrap:action", code);
functionWrapper(n);
@ -1201,20 +1273,32 @@ ready:
virtual int enumDeclaration(Node *n) {
if (!proxy_flag)
return SWIG_OK;
if (Cmp(Getattr(n, "access"), "public") != 0)
return SWIG_OK;
String *newclassname = Getattr(Swig_methodclass(n), "sym:name");
String *name = Getattr(n, "sym:name");
String *code = NewString("");
String *tdname = Getattr(n, "tdname");
String *newname = newclassname ? NewStringf("%s_", newclassname) : Copy(name);
Symtab *symtab = Getattr(n, "sym:symtab");
if (tdname)
if (tdname) {
Printf(code, "typedef ");
name = Getattr(n, "name");
String *td_def_name = NewStringf("enum %s", name);
SwigType_typedef(td_def_name, name);
Delete(td_def_name);
SwigType_istypedef(name);
}
Symtab *symtab = Getattr(n, "sym:symtab");
String *newname = newclassname ? NewStringf("%s_", newclassname) : Copy(name);
Printf(code, "enum ");
if (!name) {
String *anonymous_name = NewStringf("enum%d ", enum_id++);
Setattr(n, "sym:name", anonymous_name);
Setattr(n, "unnamed", "1");
name = Getattr(n, "sym:name");
Delete(anonymous_name);
}
if (Delattr(n, "unnamed")) {
// unnamed enum declaration: create new symbol
Replaceall(name, "$unnamed", "enum");
@ -1226,15 +1310,21 @@ ready:
Setattr(entry, "sym:name", name);
Setattr(entry, "sym:symtab", symtab);
Swig_symbol_add(name, entry);
}
}
if (newclassname) {
if (symtab) {
Node *node = Swig_symbol_clookup(name, symtab);
if (node) {
if (node)
Append(newname, name);
Setattr(node, "name", newname);
}
}
else
Append(newname, "enum");
Setattr(n, "name", newname);
}
else {
Delete(newname);
newname = name;
}
Printv(code, newname ? newname : "", " {\n$enumvalues\n} ", tdname ? tdname : "", ";\n\n", NIL);
emit_children(n);
@ -1259,8 +1349,6 @@ ready:
virtual int enumvalueDeclaration(Node *n) {
String *name = Getattr(n, "sym:name");
if (Cmp(Getattr(n, "access"), "public") != 0)
return SWIG_OK;
String *enumvalue = Getattr(n, "enumvalue");
String *init = 0;
if (enumvalue) {
@ -1303,9 +1391,10 @@ ready:
if (strncmp(c, "enum", 4) != 0) {
if (name && type) {
String *code = NewStringf("typedef %s %s;\n\n", type, name);
String *code = NewStringf("typedef %s %s;\n\n", name, type);
Append(f_proxy_header, code);
Delete(code);
SwigType_typedef(type, name);
}
}
return SWIG_OK;
@ -1345,6 +1434,6 @@ extern "C" Language *swig_c(void) {
const char *C::usage = (char *) "\
C Options (available with -c)\n\
-noproxy - do not generate proxy interface\n\
-noruntime - disable runtime error checking\n\
-noexcept - do not generate exception handling code\n\
\n";