From 27f964987ac68c72453ca189831287511f07a5be Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Fri, 21 Apr 2023 09:38:15 +1200 Subject: [OCaml] Fix reference typemaps for std::string Fix warnings in the extend_template_method, li_std_string, and template_methods tests. std::string was missing a typecheck typemap. Add extend_template_method_runme.ml, li_std_string_runme.ml, and template_methods_runme.ml. Add INPUT, OUTPUT and INOUT typemaps for string & Use the INOUT typemap in the strings_test example. In the strings_test example, takes_and_gives_std_string() was relying on the silly fact that an argout typemap for string & was enabled by default. Remove the in, out, and typecheck typemaps for string &. Closes: #1439 --- Lib/ocaml/std_string.i | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'Lib') diff --git a/Lib/ocaml/std_string.i b/Lib/ocaml/std_string.i index 2564cfb38..08e304911 100644 --- a/Lib/ocaml/std_string.i +++ b/Lib/ocaml/std_string.i @@ -4,18 +4,11 @@ * SWIG typemaps for std::string * ----------------------------------------------------------------------------- */ -// ------------------------------------------------------------------------ -// std::string is typemapped by value -// This can prevent exporting methods which return a string -// in order for the user to modify it. -// However, I think I'll wait until someone asks for it... -// ------------------------------------------------------------------------ - %{ #include #include %} - + %include %include @@ -23,10 +16,10 @@ namespace std { %naturalvar string; %naturalvar wstring; - + class string; class wstring; - + /* Overloading check */ %typemap(in) string { if (caml_ptr_check($input)) @@ -44,15 +37,6 @@ class wstring; } } -%typemap(in) string & ($*1_ltype temp) { - if (caml_ptr_check($input)) { - temp.assign((char *)caml_ptr_val($input,0), caml_string_len($input)); - $1 = &temp; - } else { - SWIG_exception(SWIG_TypeError, "string expected"); - } -} - %typemap(in) string * ($*1_ltype *temp) { if (caml_ptr_check($input)) { temp = new $*1_ltype((char *)caml_ptr_val($input,0), caml_string_len($input)); @@ -66,8 +50,8 @@ class wstring; delete temp; } -%typemap(argout) string & { - swig_result = caml_list_append(swig_result,caml_val_string_len((*$1).c_str(), (*$1).size())); +%typemap(out) const string & { + $result = caml_val_string_len((*$1).c_str(), (*$1).size()); } %typemap(directorin) string { @@ -99,6 +83,20 @@ class wstring; %typemap(throws) string, const string & "SWIG_OCamlThrowException(SWIG_OCamlRuntimeException, $1.c_str());" +%typemap(in) string &INPUT = const string &; +%typemap(in, numinputs=0) string &OUTPUT ($*1_ltype temp) +%{ $1 = &temp; %} +%typemap(argout) string &OUTPUT { + swig_result = caml_list_append(swig_result, caml_val_string_len((*$1).c_str(), (*$1).size())); +} +%typemap(in) string &INOUT = const string &; +%typemap(argout) string &INOUT = string &OUTPUT; + +%typemap(typecheck) string, const string & = char *; + +%typemap(throws) string, const string & { + SWIG_OCamlThrowException(SWIG_OCamlRuntimeException, $1.c_str()); +} } #ifdef ENABLE_CHARPTR_ARRAY -- cgit v1.2.1