From ffca8a6d89d5e38cb4f28e4a5423c21145fed53f Mon Sep 17 00:00:00 2001
From: Pradipta Samanta <samanta@dkrz.de>
Date: Tue, 31 Dec 2024 15:07:21 +0100
Subject: [PATCH] enabled the use of cpp bindings for mo_lib_loopindices

---
 CMakeLists.txt                     |  1 +
 src/support/CMakeLists.txt         |  4 ++++
 src/support/mo_lib_loopindices.F90 | 34 +++++++++++++++++++++++++++++-
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c40cd40..2f32fcf 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,7 @@ option(BUILD_ICONMATH_HORIZONTAL "Build horizontal library" ON)
 
 option(IM_ENABLE_MIXED_PRECISION "Enable mixed precision" OFF)
 option(IM_ENABLE_LOOP_EXCHANGE "Enable loop exchange" OFF)
+option(IM_USE_CPP_BINDINGS "Use C++ bindings" OFF)
 option(IM_ENABLE_DIM_SWAP "Enable dimension swap" OFF)
 option(IM_ENABLE_OPENACC "Enable OpenACC support" OFF)
 option(IM_ENABLE_OPENMP "Enable OpenMP support" OFF)
diff --git a/src/support/CMakeLists.txt b/src/support/CMakeLists.txt
index 6d2fd78..c0fc287 100644
--- a/src/support/CMakeLists.txt
+++ b/src/support/CMakeLists.txt
@@ -41,6 +41,10 @@ if(IM_ENABLE_DIM_SWAP)
   target_compile_definitions(iconmath-support PRIVATE __SWAPDIM)
 endif()
 
+if(IM_USE_CPP_BINDINGS)
+  target_compile_definitions(iconmath-support PRIVATE __USE_CPP_BINDINGS)
+endif()
+
 if(IM_ENABLE_OPENACC)
   # If _OPENACC is defined, assume that the required compiler flags are already
   # provided, e.g. in CMAKE_Fortran_FLAGS:
diff --git a/src/support/mo_lib_loopindices.F90 b/src/support/mo_lib_loopindices.F90
index fe6c9b9..3ac80dd 100644
--- a/src/support/mo_lib_loopindices.F90
+++ b/src/support/mo_lib_loopindices.F90
@@ -16,12 +16,18 @@
 
 MODULE mo_lib_loopindices
 
+#ifdef __USE_CPP_BINDINGS
+  USE, INTRINSIC :: ISO_C_BINDING
+#endif
+
   IMPLICIT NONE
 
   PRIVATE
 
   PUBLIC :: get_indices_c_lib, get_indices_e_lib, get_indices_v_lib
 
+#ifndef __USE_CPP_BINDINGS
+
 CONTAINS
 
 !-------------------------------------------------------------------------
@@ -121,5 +127,31 @@ CONTAINS
 
   END SUBROUTINE get_indices_v_lib
 
-END MODULE mo_lib_loopindices
+#else
+
+  INTERFACE
+    SUBROUTINE get_indices_c_lib(i_startidx_in, i_endidx_in, nproma, i_blk, i_startblk, i_endblk, &
+                                 i_startidx_out, i_endidx_out) BIND(C, NAME="get_indices_c_lib")
+      IMPORT :: C_INT
+      INTEGER(C_INT), VALUE :: i_startidx_in, i_endidx_in, nproma, i_blk, i_startblk, i_endblk
+      INTEGER(C_INT) :: i_startidx_out, i_endidx_out
+    END SUBROUTINE get_indices_c_lib
+
+    SUBROUTINE get_indices_e_lib(i_startidx_in, i_endidx_in, nproma, i_blk, i_startblk, i_endblk, &
+                                 i_startidx_out, i_endidx_out) BIND(C, NAME="get_indices_e_lib")
+      IMPORT :: C_INT
+      INTEGER(C_INT), VALUE :: i_startidx_in, i_endidx_in, nproma, i_blk, i_startblk, i_endblk
+      INTEGER(C_INT) :: i_startidx_out, i_endidx_out
+    END SUBROUTINE get_indices_e_lib
+
+    SUBROUTINE get_indices_v_lib(i_startidx_in, i_endidx_in, nproma, i_blk, i_startblk, i_endblk, &
+                                 i_startidx_out, i_endidx_out) BIND(C, NAME="get_indices_v_lib")
+      IMPORT :: C_INT
+      INTEGER(C_INT), VALUE :: i_startidx_in, i_endidx_in, nproma, i_blk, i_startblk, i_endblk
+      INTEGER(C_INT) :: i_startidx_out, i_endidx_out
+    END SUBROUTINE get_indices_v_lib
+  END INTERFACE
+
+#endif
 
+END MODULE mo_lib_loopindices
-- 
GitLab