Basic constructor and destructor wrapper.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10532 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a5b8db8296
commit
e06b48e3f6
7 changed files with 238 additions and 21 deletions
|
|
@ -1107,6 +1107,6 @@ c: $(SRCS)
|
||||||
c_cpp: $(SRCS)
|
c_cpp: $(SRCS)
|
||||||
$(SWIG) -c++ -c $(SWIGOPT) $(INTERFACE)
|
$(SWIG) -c++ -c $(SWIGOPT) $(INTERFACE)
|
||||||
$(CC) -c $(CCSHARED) $(CXXFLAGS) $(ICXXSRCS) $(SRCS) $(CXXSRCS) $(INCLUDES)
|
$(CC) -c $(CCSHARED) $(CXXFLAGS) $(ICXXSRCS) $(SRCS) $(CXXSRCS) $(INCLUDES)
|
||||||
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBSi) $(CPP_DLLIBS)
|
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) $(CPP_DLLIBS) -o $(LIBPREFIX)_$(TARGET)$(SO)
|
||||||
$(CC) $(MAIN) $(TARGET)_proxy.c -L. -l$(LIBPREFIX)_$(TARGET)
|
$(CC) $(MAIN) $(TARGET)_proxy.c -L. -l$(LIBPREFIX)_$(TARGET)
|
||||||
|
|
||||||
|
|
|
||||||
14
Examples/c/class/Makefile
Normal file
14
Examples/c/class/Makefile
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
TOP = ../..
|
||||||
|
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
|
||||||
|
CXXSRCS = example.cxx
|
||||||
|
TARGET = example
|
||||||
|
INTERFACE = example.i
|
||||||
|
MAIN = main.c
|
||||||
|
|
||||||
|
all::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c_cpp
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm *.o
|
||||||
|
|
||||||
28
Examples/c/class/example.cxx
Normal file
28
Examples/c/class/example.cxx
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/* File : example.cxx */
|
||||||
|
|
||||||
|
#include "example.h"
|
||||||
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
||||||
|
/* Move the shape to a new location */
|
||||||
|
void Shape::move(double dx, double dy) {
|
||||||
|
x += dx;
|
||||||
|
y += dy;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Shape::nshapes = 0;
|
||||||
|
|
||||||
|
double Circle::area(void) {
|
||||||
|
return M_PI*radius*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Circle::perimeter(void) {
|
||||||
|
return 2*M_PI*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square::area(void) {
|
||||||
|
return width*width;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square::perimeter(void) {
|
||||||
|
return 4*width;
|
||||||
|
}
|
||||||
39
Examples/c/class/example.h
Normal file
39
Examples/c/class/example.h
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
/* File : example.h */
|
||||||
|
|
||||||
|
class Shape {
|
||||||
|
public:
|
||||||
|
Shape() {
|
||||||
|
nshapes++;
|
||||||
|
}
|
||||||
|
virtual ~Shape() {
|
||||||
|
nshapes--;
|
||||||
|
};
|
||||||
|
double x, y;
|
||||||
|
void move(double dx, double dy);
|
||||||
|
virtual double area(void) = 0;
|
||||||
|
virtual double perimeter(void) = 0;
|
||||||
|
static int nshapes;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Circle : public Shape {
|
||||||
|
private:
|
||||||
|
double radius;
|
||||||
|
public:
|
||||||
|
Circle(double r) : radius(r) { };
|
||||||
|
virtual double area(void);
|
||||||
|
virtual double perimeter(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Square : public Shape {
|
||||||
|
private:
|
||||||
|
double width;
|
||||||
|
public:
|
||||||
|
Square(double w) : width(w) { };
|
||||||
|
virtual double area(void);
|
||||||
|
virtual double perimeter(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
10
Examples/c/class/example.i
Normal file
10
Examples/c/class/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
/* File : example.i */
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
12
Examples/c/class/main.c
Normal file
12
Examples/c/class/main.c
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "example_proxy.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
struct CircleObj* pc = new_Circle(10.0);
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
delete_Circle(pc);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -25,6 +25,8 @@ class C:public Language {
|
||||||
String *f_shadow_code_body;
|
String *f_shadow_code_body;
|
||||||
String *f_shadow_header;
|
String *f_shadow_header;
|
||||||
|
|
||||||
|
String *empty_string;
|
||||||
|
|
||||||
bool shadow_flag;
|
bool shadow_flag;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -33,7 +35,9 @@ public:
|
||||||
* C()
|
* C()
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
C() : shadow_flag(true) {
|
C() :
|
||||||
|
empty_string(NewString("")),
|
||||||
|
shadow_flag(true) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------
|
/* ------------------------------------------------------------
|
||||||
|
|
@ -65,14 +69,6 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void emitBanner(File *f) {
|
|
||||||
Printf(f, "/* This file was automatically generated by SWIG (http://www.swig.org).\n");
|
|
||||||
Printf(f, " * Version %s\n", Swig_package_version());
|
|
||||||
Printf(f, " * \n");
|
|
||||||
Printf(f, " * Don't modify this file, modify the SWIG interface instead.\n");
|
|
||||||
Printf(f, " */\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void emitSwigExport(File *f) {
|
void emitSwigExport(File *f) {
|
||||||
Printf(f, "#ifndef SWIGEXPORT\n");
|
Printf(f, "#ifndef SWIGEXPORT\n");
|
||||||
Printf(f, "# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n");
|
Printf(f, "# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n");
|
||||||
|
|
@ -124,6 +120,11 @@ public:
|
||||||
f_header = NewString("");
|
f_header = NewString("");
|
||||||
f_wrappers = NewString("");
|
f_wrappers = NewString("");
|
||||||
|
|
||||||
|
Swig_banner(f_runtime);
|
||||||
|
|
||||||
|
// FIXME
|
||||||
|
Printf(f_header, "#include \"malloc.h\"\n\n");
|
||||||
|
|
||||||
/* generate shadow files if enabled */
|
/* generate shadow files if enabled */
|
||||||
if (shadow_flag) {
|
if (shadow_flag) {
|
||||||
f_shadow_code_init = NewString("");
|
f_shadow_code_init = NewString("");
|
||||||
|
|
@ -147,8 +148,8 @@ public:
|
||||||
Swig_register_filebyname("shadow_code_body", f_shadow_code_body);
|
Swig_register_filebyname("shadow_code_body", f_shadow_code_body);
|
||||||
Swig_register_filebyname("shadow_header", f_shadow_header);
|
Swig_register_filebyname("shadow_header", f_shadow_header);
|
||||||
|
|
||||||
emitBanner(f_shadow_code_init);
|
Swig_banner(f_shadow_code_init);
|
||||||
emitBanner(f_shadow_header);
|
Swig_banner(f_shadow_header);
|
||||||
emitSwigImport(f_shadow_header);
|
emitSwigImport(f_shadow_header);
|
||||||
Printf(f_shadow_code_init, "#include \"%s\"\n\n", shadow_header_filename);
|
Printf(f_shadow_code_init, "#include \"%s\"\n\n", shadow_header_filename);
|
||||||
}
|
}
|
||||||
|
|
@ -217,6 +218,7 @@ public:
|
||||||
String *name = Getattr(n, "sym:name");
|
String *name = Getattr(n, "sym:name");
|
||||||
SwigType *return_type = Getattr(n, "type");
|
SwigType *return_type = Getattr(n, "type");
|
||||||
String *return_type_str = SwigType_str(return_type, 0);
|
String *return_type_str = SwigType_str(return_type, 0);
|
||||||
|
String *arg_names = NewString("");
|
||||||
ParmList *parms = Getattr(n, "parms");
|
ParmList *parms = Getattr(n, "parms");
|
||||||
|
|
||||||
/* create new function wrapper object */
|
/* create new function wrapper object */
|
||||||
|
|
@ -238,6 +240,7 @@ public:
|
||||||
np = nextSibling(p);
|
np = nextSibling(p);
|
||||||
SwigType *type = Getattr(p, "type");
|
SwigType *type = Getattr(p, "type");
|
||||||
Printv(wrapper->def, SwigType_str(type, 0), " ", Getattr(p, "lname"), np ? ", " : "", NIL);
|
Printv(wrapper->def, SwigType_str(type, 0), " ", Getattr(p, "lname"), np ? ", " : "", NIL);
|
||||||
|
Printv(arg_names, Getattr(p, "name"), np ? ", " : "", NIL);
|
||||||
p = np;
|
p = np;
|
||||||
}
|
}
|
||||||
Printv(wrapper->def, ") {", NIL);
|
Printv(wrapper->def, ") {", NIL);
|
||||||
|
|
@ -248,21 +251,15 @@ public:
|
||||||
/* emit action code */
|
/* emit action code */
|
||||||
String *action = emit_action(n);
|
String *action = emit_action(n);
|
||||||
Append(wrapper->code, action);
|
Append(wrapper->code, action);
|
||||||
Append(wrapper->code, "return result;\n}\n");
|
if (return_type && Strcmp(return_type, "void") != 0)
|
||||||
|
Append(wrapper->code, "return result;\n");
|
||||||
|
|
||||||
|
Append(wrapper->code, "}\n");
|
||||||
Wrapper_print(wrapper, f_wrappers);
|
Wrapper_print(wrapper, f_wrappers);
|
||||||
|
|
||||||
/* take care of shadow function */
|
/* take care of shadow function */
|
||||||
if (shadow_flag) {
|
if (shadow_flag) {
|
||||||
String *proto = ParmList_str(parms);
|
String *proto = ParmList_str(parms);
|
||||||
String *arg_names = NewString("");
|
|
||||||
|
|
||||||
Parm *p, *np;
|
|
||||||
for (p = parms; p; ) {
|
|
||||||
np = nextSibling(p);
|
|
||||||
Printv(arg_names, Getattr(p, "name"), np ? ", " : "", NIL);
|
|
||||||
p = np;
|
|
||||||
}
|
|
||||||
|
|
||||||
Printv(f_shadow_code_init, "extern ", return_type_str, " _wrap_", name, "(", proto, ");\n", NIL);
|
Printv(f_shadow_code_init, "extern ", return_type_str, " _wrap_", name, "(", proto, ");\n", NIL);
|
||||||
Printv(f_shadow_code_body, return_type_str, " ", name, "(", proto, ") {\n", NIL);
|
Printv(f_shadow_code_body, return_type_str, " ", name, "(", proto, ") {\n", NIL);
|
||||||
|
|
@ -298,12 +295,129 @@ public:
|
||||||
Printv(f_shadow_header, return_type_str, " ", name, "(", proto, ");\n");
|
Printv(f_shadow_header, return_type_str, " ", name, "(", proto, ");\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Delete(arg_names);
|
||||||
Delete(wname);
|
Delete(wname);
|
||||||
DelWrapper(wrapper);
|
DelWrapper(wrapper);
|
||||||
|
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
* classDeclaration()
|
||||||
|
* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
virtual int classHandler(Node* n) {
|
||||||
|
String* name = Getattr(n, "name");
|
||||||
|
String* sobj_name = NewString("");
|
||||||
|
Printv(sobj_name, "struct ", name, "Obj", NIL);
|
||||||
|
Printv(f_header, sobj_name, "{\n void* obj;\n};\n\n", NIL);
|
||||||
|
|
||||||
|
if (shadow_flag) {
|
||||||
|
Printv(f_shadow_header, sobj_name, ";\n\n", NIL);
|
||||||
|
}
|
||||||
|
|
||||||
|
Delete(sobj_name);
|
||||||
|
return Language::classHandler(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
* memberfunctionHandler()
|
||||||
|
* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
virtual int memberfunctionHandler(Node* n) {
|
||||||
|
return SWIG_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
* variableHandler()
|
||||||
|
* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
virtual int variableHandler(Node* n) {
|
||||||
|
if (Cmp(Getattr(n, "ismember"), "1") != 0)
|
||||||
|
return globalvariableHandler(n);
|
||||||
|
return SWIG_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
* constructorHandler()
|
||||||
|
* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
virtual int constructorHandler(Node* n) {
|
||||||
|
String* sobj_name = NewString("");
|
||||||
|
String* ctype = NewString("");
|
||||||
|
String* code = NewString("");
|
||||||
|
String* new_name = NewString("");
|
||||||
|
String* arg_lnames = NewString("");
|
||||||
|
ParmList* parms;
|
||||||
|
Parm* p, *np;
|
||||||
|
|
||||||
|
parms = Getattr(n, "parms");
|
||||||
|
int i = 1;
|
||||||
|
for (p = parms; p; ) {
|
||||||
|
np = nextSibling(p);
|
||||||
|
String* name = NewString("");
|
||||||
|
Printf(name, "arg%d", i++);
|
||||||
|
Setattr(p, "lname", name);
|
||||||
|
Delete(name);
|
||||||
|
Printv(arg_lnames, name, np ? ", " : "", NIL);
|
||||||
|
p = np;
|
||||||
|
}
|
||||||
|
|
||||||
|
Printv(sobj_name, "struct ", Getattr(n, "name"), "Obj", NIL);
|
||||||
|
ctype = Copy(sobj_name);
|
||||||
|
SwigType_add_pointer(ctype);
|
||||||
|
Printv(code, "result = (", sobj_name, "*) malloc(sizeof(", sobj_name, "));\n", NIL);
|
||||||
|
Printv(code, "result->obj = (void*) new ", Getattr(n, "name"), "(", arg_lnames, ");\n", NIL);
|
||||||
|
Setattr(n, "wrap:action", code);
|
||||||
|
Setattr(n, "type", ctype);
|
||||||
|
Printv(new_name, "new_", Getattr(n, "sym:name"), NIL);
|
||||||
|
Setattr(n, "sym:name", new_name);
|
||||||
|
|
||||||
|
functionWrapper(n);
|
||||||
|
|
||||||
|
Delete(arg_lnames);
|
||||||
|
Delete(new_name);
|
||||||
|
Delete(code);
|
||||||
|
Delete(ctype);
|
||||||
|
Delete(sobj_name);
|
||||||
|
return SWIG_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------
|
||||||
|
* destructorHandler()
|
||||||
|
* --------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
virtual int destructorHandler(Node* n) {
|
||||||
|
String* sobj_name = NewString("");
|
||||||
|
String* ctype = NewString("");
|
||||||
|
String* code = NewString("");
|
||||||
|
String* new_name = NewString("");
|
||||||
|
Parm* p;
|
||||||
|
|
||||||
|
Printv(sobj_name, "struct ", Getattr(n, "sym:name"), "Obj", NIL);
|
||||||
|
ctype = Copy(sobj_name);
|
||||||
|
SwigType_add_pointer(ctype);
|
||||||
|
|
||||||
|
p = NewParm(ctype, "self");
|
||||||
|
Setattr(p, "lname", "arg1");
|
||||||
|
Setattr(n, "parms", p);
|
||||||
|
|
||||||
|
Setattr(n, "type", "void");
|
||||||
|
Printv(code, "delete ((", sobj_name, "*) arg1->obj);\nfree(arg1);\n", NIL);
|
||||||
|
Setattr(n, "wrap:action", code);
|
||||||
|
Printv(new_name, "delete_", Getattr(n, "sym:name"), NIL);
|
||||||
|
Setattr(n, "sym:name", new_name);
|
||||||
|
|
||||||
|
functionWrapper(n);
|
||||||
|
|
||||||
|
Delete(p);
|
||||||
|
Delete(new_name);
|
||||||
|
Delete(code);
|
||||||
|
Delete(ctype);
|
||||||
|
Delete(sobj_name);
|
||||||
|
return SWIG_OK;
|
||||||
|
}
|
||||||
|
|
||||||
}; /* class C */
|
}; /* class C */
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue