Now constructors use SWIG_create_object() function, which fills type information for SwigObj struct. 'out' typemaps for objects and 'throws' typemaps also use this.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10696 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Maciej Drwal 2008-07-21 18:22:43 +00:00
commit 96a52c763c
3 changed files with 68 additions and 48 deletions

View file

@ -33,22 +33,9 @@
}
#endif
/*
* Additional typemaps needed to handle exception order in C.
*/
#if defined(SWIGC)
%typemap(throws) SWIGTYPE {
SwigObj *_c_ex = _wrap_new_$1_basetype();
SWIG_CThrowException(_c_ex, "C++ $1_type exception thrown");
}
%typemap(throws) ET<int>, ET<double> {
SWIG_CThrowException(NULL, "C++ $1_type exception thrown");
}
#endif
%catches(E1,E2*,ET<int>,ET<double>,...) A::barfoo(int i);
%allowexception efoovar;
%allowexception A::efoovar;

View file

@ -18,6 +18,11 @@ typedef struct {
void *obj;
const char **typenames;
} SwigObj;
SWIGINTERN SwigObj *SWIG_registry_base = 0;
SWIGINTERN SwigObj *SWIG_registry = 0;
#define SWIG_STR(x) #x
%}
// typemaps for function parameters
@ -131,27 +136,14 @@ typedef struct {
// allocate new "object-struct" by default
%typemap(out) SWIGTYPE {
$result = (SwigObj *) malloc(sizeof(SwigObj));
$result = SWIG_create_object(SWIG_STR($1_type));
$result->obj = (void*) &$1;
#if SWIG_C_RUNTIME
$result->typenames[0] = 0; // FIXME
#endif
}
%typemap(out) SWIGTYPE * {
$result = (SwigObj *) malloc(sizeof(SwigObj));
$result->obj = (void*) $1;
#if SWIG_C_RUNTIME
$result->typenames[0] = 0; // FIXME
#endif
}
%typemap(out) SWIGTYPE & {
$result = (SwigObj *) malloc(sizeof(SwigObj));
%typemap(out) SWIGTYPE *, SWIGTYPE & {
$result = SWIG_create_object(SWIG_STR($1_basetype));
$result->obj = (void*) $1;
#if SWIG_C_RUNTIME
$result->typenames[0] = 0; // FIXME
#endif
}
// exception handling
@ -166,13 +158,22 @@ typedef struct {
SWIG_CThrowException(0, $1);
}
// this should match only non-built-in and non-template objects
// this should match only non-built-in objects
%typemap(throws) SWIGTYPE {
SWIG_CThrowException(0, "C++ $1_type exception thrown");
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 *, 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
SWIGEXPORTC struct {

View file

@ -29,6 +29,7 @@ class C:public Language {
String *empty_string;
String *int_string;
String *enum_values;
String *create_object;
bool proxy_flag;
bool runtime_flag;
@ -44,6 +45,7 @@ public:
empty_string(NewString("")),
int_string(NewString("int")),
enum_values(0),
create_object(0),
proxy_flag(true),
runtime_flag(true),
typecheck_flag(false) {
@ -90,6 +92,30 @@ public:
allow_overloading();
}
/* ---------------------------------------------------------------------
* start_create_object()
* --------------------------------------------------------------------- */
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");
Printf(s, "SwigObj *result;\n");
Printf(s, "result = (SwigObj *) malloc(sizeof(SwigObj));\n");
Printf(s, "result->obj = 0;\n");
}
/* ---------------------------------------------------------------------
* finish_create_object()
* --------------------------------------------------------------------- */
String *finish_create_object() {
String *s = create_object;
Printf(s, "return result;\n");
Printf(s, "}\n\n");
return create_object;
}
/* ---------------------------------------------------------------------
* top()
* --------------------------------------------------------------------- */
@ -147,10 +173,14 @@ public:
Printf(f_wrappers, "#ifdef __cplusplus\n");
Printf(f_wrappers, "extern \"C\" {\n");
Printf(f_wrappers, "#endif\n\n");
start_create_object();
// emit code for children
Language::top(n);
Append(f_header, finish_create_object());
Printf(f_wrappers, "#ifdef __cplusplus\n");
Printf(f_wrappers, "}\n");
Printf(f_wrappers, "#endif\n");
@ -167,7 +197,7 @@ public:
}
// write all to the file
Dump(f_header, f_runtime);
Wrapper_pretty_print(f_header, f_runtime);
Dump(f_wrappers, f_runtime);
Wrapper_pretty_print(f_init, f_runtime);
@ -710,7 +740,7 @@ ready:
// declare type for specific class in the proxy header
if (proxy_flag)
Printv(f_proxy_header, "typedef SwigObj ", name, ";\n\n", NIL);
Printv(f_proxy_header, "\ntypedef SwigObj ", name, ";\n\n", NIL);
Delete(sobj);
Delete(name);
@ -993,23 +1023,27 @@ ready:
}
/* ---------------------------------------------------------------------
* emit_runtime_make_object()
* add_to_create_object()
* --------------------------------------------------------------------- */
void emit_runtime_make_object(Node *n, String *classname, String *code) {
void add_to_create_object(Node *n, String *classname, String *newclassname) {
String *s = create_object;
Printv(s, "if (strcmp(classname, \"", classname, "\") == 0) {\n", NIL);
// store the name of each class in the hierarchy
List *baselist = Getattr(parentNode(n), "bases");
Printf(code, "result->typenames = (const char **) malloc(%d*sizeof(const char*));\n", Len(baselist) + 2);
Printv(code, "result->typenames[0] = Swig_typename_", classname, ";\n", NIL);
Printf(s, "result->typenames = (const char **) malloc(%d*sizeof(const char*));\n", Len(baselist) + 2);
Printv(s, "result->typenames[0] = Swig_typename_", newclassname, ";\n", NIL);
int i = 1;
if (baselist) {
Iterator it;
for (it = First(baselist); it.item; it = Next(it)) {
Printf(code, "result->typenames[%d] = Swig_typename_%s;\n", i++, Getattr(it.item, "name"));
Printf(s, "result->typenames[%d] = Swig_typename_%s;\n", i++, Getattr(it.item, "sym:name"));
}
}
Printf(code, "result->typenames[%d] = 0;\n", i);
Printf(code, "SWIG_Runtime_init();\n");
Printf(s, "result->typenames[%d] = 0;\n", i);
Printf(s, "}\n");
}
/* ---------------------------------------------------------------------
@ -1049,10 +1083,9 @@ ready:
Setattr(n, "sym:name", constr_name);
// generate action code
Printv(code, "result = (", sobj_name, "*) malloc(sizeof(", sobj_name, "));\n", NIL);
add_to_create_object(n, classname, newclassname);
Printv(code, "result = SWIG_create_object(\"", classname, "\");\n", NIL);
Printv(code, "result->obj = (void*) new ", classname, arg_lnames, ";\n", NIL);
if (runtime_flag)
emit_runtime_make_object(n, newclassname, code);
Setattr(n, "wrap:action", code);
@ -1099,10 +1132,9 @@ ready:
Setattr(n, "sym:name", constr_name);
// generate action code
Printv(code, "result = (", sobj_name, "*) malloc(sizeof(", sobj_name, "));\n", NIL);
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 (runtime_flag)
emit_runtime_make_object(n, newclassname, code);
Setattr(n, "wrap:action", code);