|
18 | 18 | #include "fortran_array_view.hpp" |
19 | 19 | #include "handle_impl.hpp" |
20 | 20 |
|
21 | | -namespace gridtools { |
22 | | - namespace c_bindings { |
23 | | - namespace _impl { |
24 | | - |
25 | | - template <class T, class = void> |
26 | | - struct result_converted_to_c; |
27 | | - |
28 | | - template <class T> |
29 | | - struct result_converted_to_c<T, |
30 | | - typename std::enable_if<std::is_void<T>::value || std::is_arithmetic<T>::value>::type> { |
31 | | - using type = T; |
32 | | - }; |
33 | | - |
34 | | - template <class T> |
35 | | - struct result_converted_to_c<T, |
36 | | - typename std::enable_if<std::is_class<typename std::remove_reference<T>::type>::value>::type> { |
37 | | - using type = gt_handle *; |
38 | | - }; |
39 | | - |
40 | | - template <class T, class = void> |
41 | | - struct param_converted_to_c; |
42 | | - |
43 | | - template <class T> |
44 | | - struct param_converted_to_c<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> { |
45 | | - using type = T; |
46 | | - }; |
47 | | - |
48 | | - template <class T> |
49 | | - struct param_converted_to_c<T *, typename std::enable_if<std::is_arithmetic<T>::value>::type> { |
50 | | - using type = T *; |
51 | | - }; |
52 | | - |
53 | | - template <class T> |
54 | | - struct param_converted_to_c<T &, typename std::enable_if<std::is_arithmetic<T>::value>::type> { |
55 | | - using type = T *; |
56 | | - }; |
57 | | - |
58 | | - template <class T> |
59 | | - struct param_converted_to_c<T *, |
60 | | - typename std::enable_if<std::is_class<T>::value && !is_fortran_array_bindable<T *>::value>::type> { |
61 | | - using type = gt_handle *; |
62 | | - }; |
63 | | - template <class T> |
64 | | - struct param_converted_to_c<T, |
65 | | - typename std::enable_if<std::is_class<remove_reference_t<T>>::value && |
66 | | - !is_fortran_array_bindable<T>::value>::type> { |
67 | | - using type = gt_handle *; |
68 | | - }; |
69 | | - |
70 | | - template <class T> |
71 | | - struct param_converted_to_c<T, typename std::enable_if<is_fortran_array_bindable<T>::value>::type> { |
72 | | - using type = gt_fortran_array_descriptor *; |
73 | | - }; |
74 | | - |
75 | | - template <class T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0> |
76 | | - T convert_to_c(T obj) { |
77 | | - return obj; |
78 | | - } |
| 21 | +namespace cpp_bindgen { |
| 22 | + namespace _impl { |
79 | 23 |
|
80 | | - template <class T, |
81 | | - typename std::enable_if<std::is_class<typename std::remove_reference<T>::type>::value, int>::type = 0> |
82 | | - gt_handle *convert_to_c(T &&obj) { |
83 | | - return new gt_handle{std::forward<T>(obj)}; |
84 | | - } |
| 24 | + template <class T, class = void> |
| 25 | + struct result_converted_to_c; |
85 | 26 |
|
86 | | - template <class T> |
87 | | - using result_converted_to_c_t = typename result_converted_to_c<T>::type; |
88 | | - template <class T> |
89 | | - using param_converted_to_c_t = typename param_converted_to_c<T>::type; |
90 | | - |
91 | | - template <class T, |
92 | | - typename std::enable_if<std::is_arithmetic<typename std::remove_pointer<T>::type>::value, int>::type = |
93 | | - 0> |
94 | | - T convert_from_c(T obj) { |
95 | | - return obj; |
96 | | - }; |
97 | | - |
98 | | - template <class T, |
99 | | - typename std::enable_if<std::is_reference<T>::value && |
100 | | - std::is_arithmetic<typename std::remove_reference<T>::type>::value, |
101 | | - int>::type = 0> |
102 | | - T convert_from_c(typename std::remove_reference<T>::type *obj) { |
103 | | - return *obj; |
104 | | - }; |
105 | | - |
106 | | - template <class T, typename std::enable_if<std::is_pointer<T>::value, int>::type = 0> |
107 | | - T convert_from_c(gt_handle *obj) { |
108 | | - return &any_cast<remove_pointer_t<T> &>(obj->m_value); |
109 | | - } |
110 | | - template <class T, typename std::enable_if<!std::is_pointer<T>::value, int>::type = 0> |
111 | | - T convert_from_c(gt_handle *obj) { |
112 | | - return any_cast<T>(obj->m_value); |
113 | | - } |
114 | | - template <class T> |
115 | | - T convert_from_c(gt_fortran_array_descriptor *obj) { |
116 | | - return make_fortran_array_view<T>(obj); |
117 | | - } |
| 27 | + template <class T> |
| 28 | + struct result_converted_to_c<T, |
| 29 | + typename std::enable_if<std::is_void<T>::value || std::is_arithmetic<T>::value>::type> { |
| 30 | + using type = T; |
| 31 | + }; |
118 | 32 |
|
119 | | - template <class T, class Impl> |
120 | | - struct wrapped_f; |
121 | | - |
122 | | - template <class R, class... Params, class Impl> |
123 | | - struct wrapped_f<R(Params...), Impl> { |
124 | | - Impl m_fun; |
125 | | - result_converted_to_c_t<R> operator()(param_converted_to_c_t<Params>... args) const { |
126 | | - return convert_to_c(m_fun(convert_from_c<Params>(args)...)); |
127 | | - } |
128 | | - }; |
129 | | - |
130 | | - template <class... Params, class Impl> |
131 | | - struct wrapped_f<void(Params...), Impl> { |
132 | | - Impl m_fun; |
133 | | - void operator()(param_converted_to_c_t<Params>... args) const { |
134 | | - m_fun(convert_from_c<Params>(args)...); |
135 | | - } |
136 | | - }; |
137 | | - |
138 | | - template <class T> |
139 | | - struct wrapped; |
140 | | - |
141 | | - template <class T> |
142 | | - struct wrapped<T *> { |
143 | | - using type = typename wrapped<T>::type; |
144 | | - }; |
145 | | - |
146 | | - template <class T> |
147 | | - struct wrapped<T &> { |
148 | | - using type = typename wrapped<T>::type; |
149 | | - }; |
150 | | - |
151 | | - template <class R, class... Params> |
152 | | - struct wrapped<R(Params...)> { |
153 | | - using type = result_converted_to_c_t<R>(typename param_converted_to_c<Params>::type...); |
154 | | - }; |
155 | | - } // namespace _impl |
156 | | - |
157 | | - /// Transform a function type to to the function type that is callable from C |
158 | 33 | template <class T> |
159 | | - using wrapped_t = typename _impl::wrapped<T>::type; |
| 34 | + struct result_converted_to_c<T, |
| 35 | + typename std::enable_if<std::is_class<typename std::remove_reference<T>::type>::value>::type> { |
| 36 | + using type = gt_handle *; |
| 37 | + }; |
160 | 38 |
|
161 | | - /// Wrap the functor of type `Impl` to another functor that can be invoked with the 'wrapped_t<T>' signature. |
162 | | - template <class T, class Impl> |
163 | | - constexpr _impl::wrapped_f<T, typename std::decay<Impl>::type> wrap(Impl &&obj) { |
164 | | - return {std::forward<Impl>(obj)}; |
| 39 | + template <class T, class = void> |
| 40 | + struct param_converted_to_c; |
| 41 | + |
| 42 | + template <class T> |
| 43 | + struct param_converted_to_c<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> { |
| 44 | + using type = T; |
| 45 | + }; |
| 46 | + |
| 47 | + template <class T> |
| 48 | + struct param_converted_to_c<T *, typename std::enable_if<std::is_arithmetic<T>::value>::type> { |
| 49 | + using type = T *; |
| 50 | + }; |
| 51 | + |
| 52 | + template <class T> |
| 53 | + struct param_converted_to_c<T &, typename std::enable_if<std::is_arithmetic<T>::value>::type> { |
| 54 | + using type = T *; |
| 55 | + }; |
| 56 | + |
| 57 | + template <class T> |
| 58 | + struct param_converted_to_c<T *, |
| 59 | + typename std::enable_if<std::is_class<T>::value && !is_fortran_array_bindable<T *>::value>::type> { |
| 60 | + using type = gt_handle *; |
| 61 | + }; |
| 62 | + template <class T> |
| 63 | + struct param_converted_to_c<T, |
| 64 | + typename std::enable_if<std::is_class<remove_reference_t<T>>::value && |
| 65 | + !is_fortran_array_bindable<T>::value>::type> { |
| 66 | + using type = gt_handle *; |
| 67 | + }; |
| 68 | + |
| 69 | + template <class T> |
| 70 | + struct param_converted_to_c<T, typename std::enable_if<is_fortran_array_bindable<T>::value>::type> { |
| 71 | + using type = gt_fortran_array_descriptor *; |
| 72 | + }; |
| 73 | + |
| 74 | + template <class T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0> |
| 75 | + T convert_to_c(T obj) { |
| 76 | + return obj; |
| 77 | + } |
| 78 | + |
| 79 | + template <class T, |
| 80 | + typename std::enable_if<std::is_class<typename std::remove_reference<T>::type>::value, int>::type = 0> |
| 81 | + gt_handle *convert_to_c(T &&obj) { |
| 82 | + return new gt_handle{std::forward<T>(obj)}; |
165 | 83 | } |
166 | 84 |
|
167 | | - /// Specialization for function pointers. |
168 | 85 | template <class T> |
169 | | - constexpr _impl::wrapped_f<T, T *> wrap(T *obj) { |
170 | | - return {obj}; |
| 86 | + using result_converted_to_c_t = typename result_converted_to_c<T>::type; |
| 87 | + template <class T> |
| 88 | + using param_converted_to_c_t = typename param_converted_to_c<T>::type; |
| 89 | + |
| 90 | + template <class T, |
| 91 | + typename std::enable_if<std::is_arithmetic<typename std::remove_pointer<T>::type>::value, int>::type = 0> |
| 92 | + T convert_from_c(T obj) { |
| 93 | + return obj; |
| 94 | + }; |
| 95 | + |
| 96 | + template <class T, |
| 97 | + typename std::enable_if<std::is_reference<T>::value && |
| 98 | + std::is_arithmetic<typename std::remove_reference<T>::type>::value, |
| 99 | + int>::type = 0> |
| 100 | + T convert_from_c(typename std::remove_reference<T>::type *obj) { |
| 101 | + return *obj; |
| 102 | + }; |
| 103 | + |
| 104 | + template <class T, typename std::enable_if<std::is_pointer<T>::value, int>::type = 0> |
| 105 | + T convert_from_c(gt_handle *obj) { |
| 106 | + return &any_cast<remove_pointer_t<T> &>(obj->m_value); |
| 107 | + } |
| 108 | + template <class T, typename std::enable_if<!std::is_pointer<T>::value, int>::type = 0> |
| 109 | + T convert_from_c(gt_handle *obj) { |
| 110 | + return any_cast<T>(obj->m_value); |
171 | 111 | } |
172 | | - } // namespace c_bindings |
173 | | -} // namespace gridtools |
| 112 | + template <class T> |
| 113 | + T convert_from_c(gt_fortran_array_descriptor *obj) { |
| 114 | + return make_fortran_array_view<T>(obj); |
| 115 | + } |
| 116 | + |
| 117 | + template <class T, class Impl> |
| 118 | + struct wrapped_f; |
| 119 | + |
| 120 | + template <class R, class... Params, class Impl> |
| 121 | + struct wrapped_f<R(Params...), Impl> { |
| 122 | + Impl m_fun; |
| 123 | + result_converted_to_c_t<R> operator()(param_converted_to_c_t<Params>... args) const { |
| 124 | + return convert_to_c(m_fun(convert_from_c<Params>(args)...)); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + template <class... Params, class Impl> |
| 129 | + struct wrapped_f<void(Params...), Impl> { |
| 130 | + Impl m_fun; |
| 131 | + void operator()(param_converted_to_c_t<Params>... args) const { m_fun(convert_from_c<Params>(args)...); } |
| 132 | + }; |
| 133 | + |
| 134 | + template <class T> |
| 135 | + struct wrapped; |
| 136 | + |
| 137 | + template <class T> |
| 138 | + struct wrapped<T *> { |
| 139 | + using type = typename wrapped<T>::type; |
| 140 | + }; |
| 141 | + |
| 142 | + template <class T> |
| 143 | + struct wrapped<T &> { |
| 144 | + using type = typename wrapped<T>::type; |
| 145 | + }; |
| 146 | + |
| 147 | + template <class R, class... Params> |
| 148 | + struct wrapped<R(Params...)> { |
| 149 | + using type = result_converted_to_c_t<R>(typename param_converted_to_c<Params>::type...); |
| 150 | + }; |
| 151 | + } // namespace _impl |
| 152 | + |
| 153 | + /// Transform a function type to to the function type that is callable from C |
| 154 | + template <class T> |
| 155 | + using wrapped_t = typename _impl::wrapped<T>::type; |
| 156 | + |
| 157 | + /// Wrap the functor of type `Impl` to another functor that can be invoked with the 'wrapped_t<T>' signature. |
| 158 | + template <class T, class Impl> |
| 159 | + constexpr _impl::wrapped_f<T, typename std::decay<Impl>::type> wrap(Impl &&obj) { |
| 160 | + return {std::forward<Impl>(obj)}; |
| 161 | + } |
| 162 | + |
| 163 | + /// Specialization for function pointers. |
| 164 | + template <class T> |
| 165 | + constexpr _impl::wrapped_f<T, T *> wrap(T *obj) { |
| 166 | + return {obj}; |
| 167 | + } |
| 168 | +} // namespace cpp_bindgen |
0 commit comments