Added class template support (removes text of ocaml class definition from swig

executable).  Now, changes to the implementation of classes can be made without
affecting SWIG itself.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4872 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Art Yerkes 2003-06-08 03:49:09 +00:00
commit dd64ccf0ae
4 changed files with 132 additions and 98 deletions

58
Lib/ocaml/class.swg Normal file
View file

@ -0,0 +1,58 @@
(*Stream:class_ctors*)
let create_$classname_from_ptr raw_ptr =
C_obj
(let rec method_table = [
"nop", (fun args -> C_void) ;
$classbody
"&", (fun args -> raw_ptr) ;
":parents",
(fun args ->
C_list
(List.map
(fun (x,y) ->
C_string (String.sub x 2 ((String.length x) - 2)))
(List.filter
(fun (x,y) ->
((String.length x) > 2)
&& x.[0] == ':' && x.[1] == ':') method_table))) ;
":classof", (fun args -> C_string "$realname") ;
":methods", (fun args -> C_list (List.map (fun (x,y) -> C_string x)
method_table)) ] in
(fun mth arg ->
try
let method_name,application =
List.hd
(List.filter (fun (x,y) -> x = mth) method_table) in
application
(match arg with
C_list l -> (C_list (raw_ptr :: l))
| C_void -> (C_list [ raw_ptr ])
| v -> (C_list [ raw_ptr ; v ]))
with
(Failure "hd") ->
(* Try parent classes *)
begin
let parent_classes = [
$baselist
] in
let rec try_parent plist raw_ptr =
match plist with
p :: tl ->
begin
try
(invoke (p raw_ptr)) mth arg
with (BadMethodName (p,m,s)) ->
try_parent tl raw_ptr
end
| [] ->
raise (BadMethodName (raw_ptr,mth,"$realname"))
in try_parent parent_classes raw_ptr
end))
let _ = Callback.register
"create_$normalized_from_ptr"
create_$classname_from_ptr
(*Stream:mli*)
val create_$classname_from_ptr : c_obj -> c_obj

View file

@ -20,6 +20,8 @@
%insert(runtime) "ocaml.swg"
/*#endif*/
%insert(classtemplate) "class.swg"
/* Definitions */
#define SWIG_malloc(size) swig_malloc(size, FUNC_NAME)
#define SWIG_free(mem) free(mem)

View file

@ -213,9 +213,11 @@ SIMPLE_MAP(unsigned long long,caml_val_ulong,caml_long_val);
%define %char_ptr_in(how)
%typemap(how) char *, signed char *, unsigned char * {
/* %typemap(how) char * ... */
$1 = ($ltype)caml_string_val($input);
}
%typemap(how) char [ANY], signed char [ANY], unsigned char [ANY] {
/* %typemap(how) char [ANY] ... */
char *temp = caml_string_val($input);
strncpy((char *)$1,temp,$1_dim0);
}
@ -244,10 +246,11 @@ SIMPLE_MAP(unsigned long long,caml_val_ulong,caml_long_val);
%define %swigtype_ptr_in(how)
%typemap(how) SWIGTYPE * {
/* %typemap(how) SWIGTYPE * */
$1 = ($ltype)caml_ptr_val($input,$1_descriptor);
}
%typemap(how) SWIGTYPE (CLASS::*) {
/* %typemap(how) SWIGTYPE *, SWIGTYPE (CLASS::*) */
/* %typemap(how) SWIGTYPE (CLASS::*) */
void *v = caml_ptr_val($input,$1_descriptor);
memcpy(& $1, &v, sizeof(v));
}
@ -272,11 +275,9 @@ SIMPLE_MAP(unsigned long long,caml_val_ulong,caml_long_val);
%enddef
%swigtype_ptr_in(in);
%swigtype_ptr_in(memberin);
%swigtype_ptr_in(varin);
%swigtype_ptr_in(outv);
%swigtype_ptr_out(out);
%swigtype_ptr_out(memberout);
%swigtype_ptr_out(varout);
%swigtype_ptr_out(inv);