1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* GridTools
*
* Copyright (c) 2014-2019, ETH Zurich
* All rights reserved.
*
* Please, refer to the LICENSE file in the root directory.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <cpp_bindgen/export.hpp>
#include <functional>
#include <sstream>
#include <stack>
#include <gtest/gtest.h>
#include <cpp_bindgen/handle.h>
namespace {
using stack_t = std::stack<double>;
// Various flavours to create exported functions.
stack_t my_create_impl() { return stack_t{}; }
BINDGEN_EXPORT_BINDING_0(my_create, my_create_impl);
template <class T>
void push_impl(std::stack<T> *obj, T val) {
obj->push(val);
}
BINDGEN_EXPORT_GENERIC_BINDING(2, my_push, push_impl, (float)(int)(double));
BINDGEN_EXPORT_BINDING_WITH_SIGNATURE_1(my_pop, void(stack_t &), [](stack_t &obj) { obj.pop(); });
struct top_impl {
template <class T>
T operator()(std::stack<T> const &container) const {
return container.top();
}
};
BINDGEN_EXPORT_BINDING_WITH_SIGNATURE_1(my_top, double(stack_t const &), top_impl{});
BINDGEN_EXPORT_BINDING_WITH_SIGNATURE_1(my_empty, bool(stack_t const &), std::mem_fn(&stack_t::empty));
template <class T, size_t size>
void assign_impl(T (&obj)[size][size], T val) {
for (size_t i = 0; i < size; ++i) {
for (size_t j = 0; j < size; ++j) {
obj[i][j] = val;
}
}
}
BINDGEN_EXPORT_GENERIC_BINDING_WRAPPED(2, my_assign, assign_impl, (int, 2)(double, 2));
struct cpp_bindgen_compatible_type {
cpp_bindgen_compatible_type(const bindgen_fortran_array_descriptor &) {}
};
struct wrapper_compatible_type {
wrapper_compatible_type(const bindgen_fortran_array_descriptor &) {}
};
bindgen_fortran_array_descriptor get_fortran_view_meta(wrapper_compatible_type *) {
bindgen_fortran_array_descriptor d;
d.rank = 2;
d.type = bindgen_fk_Int;
d.is_acc_present = false;
return d;
}
void test_cpp_bindgen_and_wrapper_compatible_type_impl(cpp_bindgen_compatible_type, wrapper_compatible_type) {}
BINDGEN_EXPORT_BINDING_2(
test_cpp_bindgen_and_wrapper_compatible_type_a, test_cpp_bindgen_and_wrapper_compatible_type_impl);
BINDGEN_EXPORT_BINDING_WRAPPED_2(
test_cpp_bindgen_and_wrapper_compatible_type_b, test_cpp_bindgen_and_wrapper_compatible_type_impl);
TEST(export, smoke) {
bindgen_handle *obj = my_create();
EXPECT_TRUE(my_empty(obj));
my_push2(obj, 42);
EXPECT_FALSE(my_empty(obj));
EXPECT_EQ(42, my_top(obj));
my_pop(obj);
EXPECT_TRUE(my_empty(obj));
bindgen_release(obj);
}
const char expected_c_interface[] = R"?(// This file is generated!
#pragma once
#include <cpp_bindgen/array_descriptor.h>
#include <cpp_bindgen/handle.h>
#ifdef __cplusplus
extern "C" {
#endif
void my_assign0(bindgen_fortran_array_descriptor*, int);
void my_assign1(bindgen_fortran_array_descriptor*, double);
bindgen_handle* my_create();
bool my_empty(bindgen_handle*);
void my_pop(bindgen_handle*);
void my_push0(bindgen_handle*, float);
void my_push1(bindgen_handle*, int);
void my_push2(bindgen_handle*, double);
double my_top(bindgen_handle*);
void test_cpp_bindgen_and_wrapper_compatible_type_a(bindgen_fortran_array_descriptor*, bindgen_fortran_array_descriptor*);
void test_cpp_bindgen_and_wrapper_compatible_type_b(bindgen_fortran_array_descriptor*, bindgen_fortran_array_descriptor*);
#ifdef __cplusplus
}
#endif
)?";
TEST(export, c_interface) {
std::ostringstream strm;
cpp_bindgen::generate_c_interface(strm);
EXPECT_EQ(strm.str(), expected_c_interface);
}
const char expected_fortran_interface[] = R"?(! This file is generated!
module my_module
use iso_c_binding
implicit none
interface
subroutine my_assign0_impl(arg0, arg1) bind(c, name="my_assign0")
use iso_c_binding
use bindgen_array_descriptor
type(bindgen_fortran_array_descriptor) :: arg0
integer(c_int), value :: arg1
end subroutine
subroutine my_assign1_impl(arg0, arg1) bind(c, name="my_assign1")
use iso_c_binding
use bindgen_array_descriptor
type(bindgen_fortran_array_descriptor) :: arg0
real(c_double), value :: arg1
end subroutine
type(c_ptr) function my_create() bind(c)
use iso_c_binding
end function
logical(c_bool) function my_empty(arg0) bind(c)
use iso_c_binding
type(c_ptr), value :: arg0
end function
subroutine my_pop(arg0) bind(c)
use iso_c_binding
type(c_ptr), value :: arg0
end subroutine
subroutine my_push0(arg0, arg1) bind(c)
use iso_c_binding
type(c_ptr), value :: arg0
real(c_float), value :: arg1
end subroutine
subroutine my_push1(arg0, arg1) bind(c)
use iso_c_binding
type(c_ptr), value :: arg0
integer(c_int), value :: arg1
end subroutine
subroutine my_push2(arg0, arg1) bind(c)
use iso_c_binding
type(c_ptr), value :: arg0
real(c_double), value :: arg1
end subroutine
real(c_double) function my_top(arg0) bind(c)
use iso_c_binding
type(c_ptr), value :: arg0
end function
subroutine test_cpp_bindgen_and_wrapper_compatible_type_a(arg0, arg1) bind(c)
use iso_c_binding
use bindgen_array_descriptor
type(bindgen_fortran_array_descriptor) :: arg0
type(bindgen_fortran_array_descriptor) :: arg1
end subroutine
subroutine test_cpp_bindgen_and_wrapper_compatible_type_b_impl(arg0, arg1) bind(c, &
name="test_cpp_bindgen_and_wrapper_compatible_type_b")
use iso_c_binding
use bindgen_array_descriptor
type(bindgen_fortran_array_descriptor) :: arg0
type(bindgen_fortran_array_descriptor) :: arg1
end subroutine
end interface
interface my_assign
procedure my_assign0, my_assign1
end interface
interface my_push
procedure my_push0, my_push1, my_push2
end interface
contains
subroutine my_assign0(arg0, arg1)
use iso_c_binding
use bindgen_array_descriptor
integer(c_int), dimension(:,:), target :: arg0
integer(c_int), value, target :: arg1
type(bindgen_fortran_array_descriptor) :: descriptor0
descriptor0%rank = 2
descriptor0%type = 1
descriptor0%dims = reshape(shape(arg0), &
shape(descriptor0%dims), (/0/))
descriptor0%data = c_loc(arg0(lbound(arg0, 1),lbound(arg0, 2)))
call my_assign0_impl(descriptor0, arg1)
end subroutine
subroutine my_assign1(arg0, arg1)
use iso_c_binding
use bindgen_array_descriptor
real(c_double), dimension(:,:), target :: arg0
real(c_double), value, target :: arg1
type(bindgen_fortran_array_descriptor) :: descriptor0
descriptor0%rank = 2
descriptor0%type = 6
descriptor0%dims = reshape(shape(arg0), &
shape(descriptor0%dims), (/0/))
descriptor0%data = c_loc(arg0(lbound(arg0, 1),lbound(arg0, 2)))
call my_assign1_impl(descriptor0, arg1)
end subroutine
subroutine test_cpp_bindgen_and_wrapper_compatible_type_b(arg0, arg1)
use iso_c_binding
use bindgen_array_descriptor
type(bindgen_fortran_array_descriptor), target :: arg0
integer(c_int), dimension(:,:), target :: arg1
type(bindgen_fortran_array_descriptor) :: descriptor1
descriptor1%rank = 2
descriptor1%type = 1
descriptor1%dims = reshape(shape(arg1), &
shape(descriptor1%dims), (/0/))
descriptor1%data = c_loc(arg1(lbound(arg1, 1),lbound(arg1, 2)))
call test_cpp_bindgen_and_wrapper_compatible_type_b_impl(arg0, descriptor1)
end subroutine
end
)?";
TEST(export, fortran_interface) {
std::ostringstream strm;
cpp_bindgen::generate_fortran_interface(strm, "my_module");
EXPECT_EQ(strm.str(), expected_fortran_interface);
}
} // namespace
|