Added to typemaps: reference type in/out

Strings <=> std::string by value
std::wstring accessible from Ocaml.  The string example converts a multibyte
japanese EUC sequence to a single wchar_t sequence if you have the ja_JP.EUC-JP
locale, or similar.
Better handling of reference in types
Corrected problems with & * mismatch in type verifier.
Type verifier now really functional.  No more type errors in places they
wouldn't be allowed in C++, unless you work at it.
Added argout_ref example for argout_ref.
Init code now effective (called from let _ = f_<module>_init ())


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4412 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Art Yerkes 2003-02-27 07:09:12 +00:00
commit 1c5ab19b2b
10 changed files with 190 additions and 58 deletions

View file

@ -13,6 +13,9 @@
/* Include headers */
%insert(runtime) "ocamldec.swg"
/* Type registration */
%insert(init) "typeregister.swg"
/*#ifndef SWIG_NOINCLUDE*/
%insert(runtime) "ocaml.swg"
/*#endif*/

View file

@ -30,6 +30,11 @@ extern "C" {
SWIG_Cast (void *source, swig_type_info *source_type,
void **ptr, swig_type_info *dest_type)
{
#ifdef TYPE_CAST_VERBOSE
fprintf( stderr, "Trying to cast %s to %s\n",
source_type ? source_type->str : "<none>",
dest_type ? dest_type->str : "<none>" );
#endif
if (dest_type != source_type) {
/* We have a type mismatch. Will have to look through our type
mapping table to figure out whether or not we can accept this
@ -40,6 +45,10 @@ extern "C" {
} else {
swig_type_info *tc =
SWIG_TypeCheck( (char *)source_type->name, dest_type );
#ifdef TYPE_CAST_VERBOSE
fprintf( stderr, "Typecheck -> %s\n",
tc ? tc->str : "<none>" );
#endif
if( tc ) {
*ptr = SWIG_TypeCast( tc, source );
return 0;
@ -56,10 +65,9 @@ extern "C" {
SWIGSTATIC int
SWIG_GetPtr(void *inptr, void **outptr,
swig_type_info *intype, swig_type_info *outtype) {
CAMLparam0();
if (intype) {
return !SWIG_Cast(inptr, intype,
outptr, outtype);
return SWIG_Cast(inptr, intype,
outptr, outtype) == -1;
} else {
*outptr = inptr;
return 0;
@ -295,7 +303,7 @@ extern "C" {
CAMLreturn(vv);
}
SWIGSTATIC value caml_val_string( char *p ) {
SWIGSTATIC value caml_val_string( const char *p ) {
CAMLparam0();
CAMLlocal1(vv);
if( !p ) CAMLreturn(caml_val_ptr( (void *)p, 0 ));
@ -304,7 +312,7 @@ extern "C" {
CAMLreturn(vv);
}
SWIGSTATIC value caml_val_string_len( char *p, int len ) {
SWIGSTATIC value caml_val_string_len( const char *p, int len ) {
CAMLparam0();
CAMLlocal1(vv);
if( !p || len < 0 ) CAMLreturn(caml_val_ptr( (void *)p, 0 ));
@ -387,7 +395,7 @@ extern "C" {
}
SWIGSTATIC int caml_ptr_val_internal( value v, void **out,
swig_type_info *descriptor ) {
swig_type_info *descriptor ) {
CAMLparam1(v);
void *outptr = NULL;
swig_type_info *outdescr = NULL;

View file

@ -33,6 +33,9 @@ extern "C" {
#define __OCAML__SWIG__MAXVALUES 6
static swig_type_info *swig_types[];
static swig_type_info *swig_types_initial[];
SWIGSTATIC int
SWIG_GetPtr(void *source, void **result, swig_type_info *type, swig_type_info *result_type);
@ -65,8 +68,8 @@ extern "C" {
static value caml_val_ptr( void *p, swig_type_info *descriptor );
static value caml_val_string( char *str );
static value caml_val_string_len( char *str, int len );
static value caml_val_string( const char *str );
static value caml_val_string_len( const char *str, int len );
static long caml_long_val( value v );
static double caml_double_val( value v );

View file

@ -70,9 +70,10 @@ namespace std {
%template(string) basic_string<char>;
%template(wstring) basic_string<wchar_t>;
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
/* Overloading check */
#if 0
%typemap(in) string {
if (caml_ptr_check($input))
$1 = std::string((char *)caml_ptr_val($input,0));
@ -89,16 +90,26 @@ namespace std {
}
}
%typemap(in) string & (std::string temp) {
if (caml_ptr_check($input)) {
temp = std::string((char *)caml_ptr_val($input,0));
$1 = &temp;
} else {
SWIG_exception(SWIG_TypeError, "string expected");
}
}
%typemap(argout) string & {
caml_list_append(swig_result,caml_val_string_len($1->c_str(),
$1->size()));
}
%typemap(out) string {
$result = caml_val_ptr((char *)$1.c_str(),0);
$result = caml_val_string_len($1.c_str(),$1.size());
}
%typemap(out) const string & {
$result = caml_val_ptr((char *)$1->c_str(),0);
$result = caml_val_string_len($1.c_str(),$1.size());
}
#endif
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
}

View file

@ -53,7 +53,8 @@ namespace std {
T operator [] ( int f );
vector <T> &operator = ( vector <T> &other );
%extend {
void set( int i, T x ) {
void set( int i, const T &x ) {
self->resize(i+1);
(*self)[i] = x;
}
};

View file

@ -44,28 +44,28 @@
}
%typemap(ocaml,in) SWIGTYPE * {
$1 = ($ltype)caml_ptr_val($input,$descriptor);
$1 = ($ltype)caml_ptr_val($input,$*1_descriptor);
}
%typemap(ocaml,out) SWIGTYPE * {
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *)$1,$descriptor));
$result = callback(*fromval,caml_val_ptr((void *)$1,$*1_descriptor));
} else {
$result = caml_val_ptr ((void *)$1,$descriptor);
$result = caml_val_ptr ((void *)$1,$*1_descriptor);
}
}
%typemap(ocaml,varin) SWIGTYPE * {
$1 = ($ltype)caml_ptr_val($input,$descriptor);
$1 = ($ltype)caml_ptr_val($input,$*1_descriptor);
}
%typemap(ocaml,varout) SWIGTYPE * {
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *)$1,$descriptor));
$result = callback(*fromval,caml_val_ptr((void *)$1,$*1_descriptor));
} else {
$result = caml_val_ptr ((void *)$1,$descriptor);
$result = caml_val_ptr ((void *)$1,$*1_descriptor);
}
}
@ -74,15 +74,29 @@
#ifdef __cplusplus
%typemap(ocaml,in) SWIGTYPE & {
$1 = ($ltype) caml_ptr_val($input,$descriptor);
$1 = ($ltype) caml_ptr_val($input,$*1_descriptor);
}
%typemap(ocaml,out) SWIGTYPE & {
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *) $1,$descriptor));
$result = callback(*fromval,caml_val_ptr((void *) $1,$*1_descriptor));
} else {
$result = caml_val_ptr ((void *) $1,$descriptor);
$result = caml_val_ptr ((void *) $1,$*1_descriptor);
}
}
%typemap(ocaml,argout) SWIGTYPE & {
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
swig_result =
caml_list_append(swig_result,
callback(*fromval,caml_val_ptr((void *) $1,
$*1_descriptor)));
} else {
swig_result =
caml_list_append(swig_result,
caml_val_ptr ((void *) $1,$*1_descriptor));
}
}
@ -118,6 +132,8 @@
}
}
%apply SWIGTYPE { const SWIGTYPE & };
#endif
/* Arrays */
@ -158,7 +174,14 @@
$1 = &temp;
}
%typemap(argout) C_NAME *OUTPUT {
caml_list_append(swig_result,(long)*$1);
swig_result = caml_list_append(swig_result,C_TO_MZ((long)*$1));
}
%typemap(in) C_NAME & (C_NAME temp) {
temp = (C_NAME) MZ_TO_C($input);
$1 = &temp;
}
%typemap(argout) C_NAME & {
swig_result = caml_list_append(swig_result,C_TO_MZ((long)*$1));
}
%enddef