From 9d4ad324c7ba732d70b6f08c50030c83252a8e09 Mon Sep 17 00:00:00 2001
From: Romain Boman <romain.boman@gmail.com>
Date: Wed, 4 Sep 2024 14:58:28 +0200
Subject: [PATCH] fix std::vector<double> problem with swig 4.2.x

---
 fwk/_src/fwkw.i      |  5 +++++
 tbox/_src/tboxw.i    |  1 -
 tbox/tests/stdvec.py | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 tbox/tests/stdvec.py

diff --git a/fwk/_src/fwkw.i b/fwk/_src/fwkw.i
index 3f3b930..70b5033 100644
--- a/fwk/_src/fwkw.i
+++ b/fwk/_src/fwkw.i
@@ -43,6 +43,11 @@ threads="1"
 
 %include "fwkw.swg"
 
+// Instantiate some std templates
+namespace std {
+   %template(std_vector_double) std::vector<double>;
+}
+
 %include "fwk.h"
 
 //%#define SHARED_PTR_DISOWN $disown
diff --git a/tbox/_src/tboxw.i b/tbox/_src/tboxw.i
index 135b6a9..4cd315d 100644
--- a/tbox/_src/tboxw.i
+++ b/tbox/_src/tboxw.i
@@ -59,7 +59,6 @@ threads="1"
 // Instantiate some std templates
 namespace std {
    %template(std_vector_int) std::vector<int>;
-   %template(std_vector_double) std::vector<double>;
    %template(std_vector_vector_double) std::vector<std::vector<double>>;
    %template(std_vector_string) std::vector<std::string>;
 }
diff --git a/tbox/tests/stdvec.py b/tbox/tests/stdvec.py
new file mode 100644
index 0000000..d763f39
--- /dev/null
+++ b/tbox/tests/stdvec.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+# Copyright 2020 University of Liège
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# basic test of std::vector<double> binding
+
+# note:
+#  std_vector_double was defined in tbox.i, but now it is now defined in fwk.i.
+#  This is because std::vector<double> is a type seen by swig in fwk.
+#  In version <4.2.0, swig was able to understand that std::vector<double> in fwk
+#  was the same as std::vector<double> in tbox where the bindings were
+#  instanciated by the %template command. From 4.2.0, swig fails to understand
+#  that link and the __setitem__ generates cast failure.
+
+
+import fwk
+
+v = fwk.std_vector_double()
+v.push_back(1.0)
+v.push_back(2.0)
+# next command fails with swig >4.2.0 if std_vector_double is 
+# instanciated (%template) in tbox.i:
+print('v[0] =', v[0])
+v[1] = 3.0
+print('vector length =', v.size())
-- 
GitLab