summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2023-04-21 09:38:15 +1200
committerOlly Betts <olly@survex.com>2023-04-21 09:39:59 +1200
commit27f964987ac68c72453ca189831287511f07a5be (patch)
treecdf56fdaf61acc04f3462be2b1fe0a1e8f572f42
parentc4f1975f9e81c47cdc724fc60b76029c668bdd79 (diff)
downloadswig-27f964987ac68c72453ca189831287511f07a5be.tar.gz
[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
-rw-r--r--Examples/ocaml/strings_test/example.i1
-rw-r--r--Examples/test-suite/ocaml/extend_template_method_runme.ml20
-rw-r--r--Examples/test-suite/ocaml/li_std_string_runme.ml44
-rw-r--r--Examples/test-suite/ocaml/template_methods_runme.ml25
-rw-r--r--Lib/ocaml/std_string.i40
5 files changed, 109 insertions, 21 deletions
diff --git a/Examples/ocaml/strings_test/example.i b/Examples/ocaml/strings_test/example.i
index d360715c1..e0697b061 100644
--- a/Examples/ocaml/strings_test/example.i
+++ b/Examples/ocaml/strings_test/example.i
@@ -12,4 +12,5 @@ using std::string;
%}
%include "std_string.i"
+%apply std::string& INOUT { std::string &inout }
%include example.h
diff --git a/Examples/test-suite/ocaml/extend_template_method_runme.ml b/Examples/test-suite/ocaml/extend_template_method_runme.ml
new file mode 100644
index 000000000..5f418c927
--- /dev/null
+++ b/Examples/test-suite/ocaml/extend_template_method_runme.ml
@@ -0,0 +1,20 @@
+open Swig
+open Extend_template_method
+
+let _ =
+ let em = new_ExtendMe '() in
+ assert (em -> do_stuff_double (1, 1.1) as float = 1.1);
+ assert (em -> do_stuff_string (1, "hello there") as string = "hello there");
+ assert (em -> do_overloaded_stuff (1.1) as float = 1.1);
+ assert (em -> do_overloaded_stuff ("hello there") as string = "hello there");
+
+ assert (_ExtendMe_static_method '(123) as int = 123);
+ ignore (new_ExtendMe '(123));
+ let em = new_TemplateExtend '() in
+ assert (em -> do_template_stuff_double (1, 1.1) as float = 1.1);
+ assert (em -> do_template_stuff_string (1, "hello there") as string = "hello there");
+ assert (em -> do_template_overloaded_stuff (1.1) as float = 1.1);
+ assert (em -> do_template_overloaded_stuff ("hello there") as string = "hello there");
+ assert (_TemplateExtend_static_template_method '(123) as int = 123);
+ ignore (new_TemplateExtend '(123))
+;;
diff --git a/Examples/test-suite/ocaml/li_std_string_runme.ml b/Examples/test-suite/ocaml/li_std_string_runme.ml
new file mode 100644
index 000000000..5f8c98a6b
--- /dev/null
+++ b/Examples/test-suite/ocaml/li_std_string_runme.ml
@@ -0,0 +1,44 @@
+open Swig
+open Li_std_string
+
+let _ =
+ assert (_test_value '("Fee") as string = "Fee");
+ try
+ ignore (_test_value '(None)); assert false
+ with Invalid_argument _ -> ()
+
+ assert (_test_const_reference '("Fee") as string = "Fee");
+ try
+ ignore (_test_const_reference '(None)); assert false
+ with Invalid_argument _ -> ()
+
+ let stringPtr = _test_pointer_out '() in
+ ignore (_test_pointer '(stringPtr));
+ let stringPtr = _test_const_pointer_out '() in
+ ignore (_test_const_pointer '(stringPtr));
+ let stringPtr = _test_reference_out '() in
+ ignore (_test_reference '(stringPtr));
+
+ try
+ ignore (_test_throw '()); assert false
+ with Failure s -> assert (s = "test_throw message")
+ try
+ ignore (_test_const_reference_throw '()); assert false
+ with Failure s -> assert (s = "test_const_reference_throw message")
+ assert (_GlobalString2 '() as string = "global string 2");
+ let s = C_string "initial string" in
+ ignore (_GlobalString2 '(s));
+ assert (_GlobalString2 '() = s);
+ assert (_ConstGlobalString '() as string = "const global string");
+
+ let myStructure = new_Structure '() in
+ assert (myStructure -> "[MemberString2]" () as string = "member string 2");
+ assert (myStructure -> "[MemberString2]" (s) = C_void);
+ assert (myStructure -> "[MemberString2]" () = s);
+ assert (myStructure -> "[ConstMemberString]" () as string = "const member string");
+
+ assert (_Structure_StaticMemberString2 '() as string = "static member string 2");
+ ignore (_Structure_StaticMemberString2 '(s));
+ assert (_Structure_StaticMemberString2 '() = s);
+ assert (_Structure_ConstStaticMemberString '() as string = "const static member string")
+;;
diff --git a/Examples/test-suite/ocaml/template_methods_runme.ml b/Examples/test-suite/ocaml/template_methods_runme.ml
new file mode 100644
index 000000000..aaa5a3416
--- /dev/null
+++ b/Examples/test-suite/ocaml/template_methods_runme.ml
@@ -0,0 +1,25 @@
+open Swig
+open Template_methods
+
+let _ =
+ let num = C_float 1. in
+ assert (_convolve1Bool '() as int = 0);
+ assert (_convolve1Bool '(true) = C_void);
+ assert (_convolve2Float '() as int = 0);
+ assert (_convolve3FloatRenamed '(num) = C_void);
+ assert (_convolve4Float '() as int = 0);
+ assert (_convolve4FloatRenamed '(num) = C_void);
+ assert (_convolve5FloatRenamed '() as int = 0);
+ assert (_convolve5FloatRenamed '(num) = C_void);
+
+ let k = new_Klass '() in
+ assert (k -> KlassTMethodBoolRenamed (true) as bool = true);
+ assert (k -> KlassTMethodBool () = C_void);
+ assert (_Klass_KlassStaticTMethodBoolRenamed '(true) as bool = true);
+ assert (_Klass_KlassStaticTMethodBool '() = C_void);
+
+ let cp = new_ComponentProperties '() in
+ assert (cp -> adda ("key1", "val1", "key2", 22.2) = C_void);
+ assert (cp -> adda ("key1", "val1", "key2", "val2", "key3", "val3") = C_void);
+ assert (cp -> adda ("key1", 1, "key2", 2, "key3", 3) = C_void)
+;;
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 <string>
#include <vector>
%}
-
+
%include <exception.i>
%include <std_vector.i>
@@ -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