LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
Put.h
Go to the documentation of this file.
1 
7 #ifndef LLU_WSTP_PUT_H_
8 #define LLU_WSTP_PUT_H_
9 
10 #include <functional>
11 
12 #include "wstp.h"
13 
14 #include "LLU/ErrorLog/Errors.h"
15 #include "LLU/Utilities.hpp"
16 #include "LLU/WSTP/Utilities.h"
17 
18 namespace LLU::WS {
19 
20  template<typename T>
21  struct PutArray {
22  using Func = std::function<int(WSLINK, const T*, const int*, const char**, int)>;
23 
24  static void put(WSLINK m, const T* array, const int* dims, const char** heads, int len) {
25  Detail::checkError(m, ArrayF(m, array, dims, heads, len), ErrorName::WSPutArrayError, ArrayFName);
26  }
27 
28  static void put(WSLINK m, const T* array, const int* dims, char** heads, int len) {
29  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast): ArrayF treats heads as read-only, so this cast is safe
30  Detail::checkError(m, ArrayF(m, array, dims, const_cast<const char**>(heads), len), ErrorName::WSPutArrayError, ArrayFName);
31  }
32 
33  private:
34  static const std::string ArrayFName;
35  static Func ArrayF;
36  };
37 
38  template<typename T>
39  struct PutList {
40  using Func = std::function<int(WSLINK, const T*, int)>;
41 
42  static void put(WSLINK m, const T* list, int len) {
43  Detail::checkError(m, ListF(m, list, len), ErrorName::WSPutListError, ListFName);
44  }
45 
46  private:
47  static const std::string ListFName;
48  static Func ListF;
49  };
50 
51  template<typename T>
52  struct PutScalar {
53  using Func = std::function<int(WSLINK, T)>;
54 
55  static void put(WSLINK m, T scalar) {
56  Detail::checkError(m, ScalarF(m, scalar), ErrorName::WSPutScalarError, ScalarFName);
57  }
58 
59  private:
60  static const std::string ScalarFName;
61  static Func ScalarF;
62  };
63 
64  template<typename T>
65  typename PutArray<T>::Func PutArray<T>::ArrayF = [](WSLINK /*link*/, const T* /*values*/, const int* /*dims*/, const char** /*heads*/, int /*length*/) {
66  static_assert(dependent_false_v<T>, "Trying to use WS::PutArray<T> for unsupported type T");
67  return 0;
68  };
69 
70  template<typename T>
71  typename PutList<T>::Func PutList<T>::ListF = [](WSLINK /*link*/, const T* /*values*/, int /*length*/) {
72  static_assert(dependent_false_v<T>, "Trying to use WS::PutList<T> for unsupported type T");
73  return 0;
74  };
75 
76  template<typename T>
77  typename PutScalar<T>::Func PutScalar<T>::ScalarF = [](WSLINK /*link*/, T /*value*/) {
78  static_assert(dependent_false_v<T>, "Trying to use WS::PutScalar<T> for unsupported type T");
79  return 0;
80  };
81 
83 #ifndef _WIN32
84 
85 #define WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(T) \
86  template<> \
87  PutArray<T>::Func PutArray<T>::ArrayF; \
88  template<> \
89  const std::string PutArray<T>::ArrayFName; \
90  template<> \
91  PutList<T>::Func PutList<T>::ListF; \
92  template<> \
93  const std::string PutList<T>::ListFName; \
94  template<> \
95  PutScalar<T>::Func PutScalar<T>::ScalarF; \
96  template<> \
97  const std::string PutScalar<T>::ScalarFName;
98 
99  WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(unsigned char)
100  WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(short)
101  WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(int)
102  WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(wsint64)
103  WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(float)
104  WS_PUT_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(double)
105 
106 #else
107 
108  /* ***************************************************************** */
109  /* ********* Template specializations for unsigned char ********** */
110  /* ***************************************************************** */
111 
112  /* PutArray */
113 
114  template<>
115  PutArray<unsigned char>::Func PutArray<unsigned char>::ArrayF = WSPutInteger8Array;
116 
117  template<>
118  const std::string PutArray<unsigned char>::ArrayFName = "WSPutInteger8Array";
119 
120  /* PutList */
121 
122  template<>
123  PutList<unsigned char>::Func PutList<unsigned char>::ListF = WSPutInteger8List;
124 
125  template<>
126  const std::string PutList<unsigned char>::ListFName = "WSPutInteger8List";
127 
128  /* PutScalar */
129 
130  template<>
131  PutScalar<unsigned char>::Func PutScalar<unsigned char>::ScalarF = WSPutInteger8;
132 
133  template<>
134  const std::string PutScalar<unsigned char>::ScalarFName = "WSPutInteger8";
135 
136  /* ***************************************************************** */
137  /* ******* Template specializations for (unsigned) short ********* */
138  /* ***************************************************************** */
139 
140  /* PutArray */
141 
142  template<>
143  PutArray<short>::Func PutArray<short>::ArrayF = WSPutInteger16Array;
144 
145  template<>
146  const std::string PutArray<short>::ArrayFName = "WSPutInteger16Array";
147 
148  /* PutList */
149 
150  template<>
151  PutList<short>::Func PutList<short>::ListF = WSPutInteger16List;
152 
153  template<>
154  const std::string PutList<short>::ListFName = "WSPutInteger16List";
155 
156  /* PutScalar */
157 
158  template<>
159  PutScalar<short>::Func PutScalar<short>::ScalarF = WSPutInteger16;
160 
161  template<>
162  const std::string PutScalar<short>::ScalarFName = "WSPutInteger16";
163 
164  /* ***************************************************************** */
165  /* ******** Template specializations for (unsigned) int ********** */
166  /* ***************************************************************** */
167 
168  /* PutArray */
169 
170  template<>
171  PutArray<int>::Func PutArray<int>::ArrayF = WSPutInteger32Array;
172 
173  template<>
174  const std::string PutArray<int>::ArrayFName = "WSPutInteger32Array";
175 
176  /* PutList */
177 
178  template<>
179  PutList<int>::Func PutList<int>::ListF = WSPutInteger32List;
180 
181  template<>
182  const std::string PutList<int>::ListFName = "WSPutInteger32List";
183 
184  /* PutScalar */
185 
186  template<>
187  PutScalar<int>::Func PutScalar<int>::ScalarF = WSPutInteger32;
188 
189  template<>
190  const std::string PutScalar<int>::ScalarFName = "WSPutInteger32";
191 
192  /* ***************************************************************** */
193  /* *********** Template specializations for wsint64 ************** */
194  /* ***************************************************************** */
195 
196  /* PutArray */
197 
198  template<>
199  PutArray<wsint64>::Func PutArray<wsint64>::ArrayF = WSPutInteger64Array;
200 
201  template<>
202  const std::string PutArray<wsint64>::ArrayFName = "WSPutInteger64Array";
203 
204  /* PutList */
205 
206  template<>
207  PutList<wsint64>::Func PutList<wsint64>::ListF = WSPutInteger64List;
208 
209  template<>
210  const std::string PutList<wsint64>::ListFName = "WSPutInteger64List";
211 
212  /* PutScalar */
213 
214  template<>
215  PutScalar<wsint64>::Func PutScalar<wsint64>::ScalarF = WSPutInteger64;
216 
217  template<>
218  const std::string PutScalar<wsint64>::ScalarFName = "WSPutInteger64";
219 
220  /* ***************************************************************** */
221  /* ************ Template specializations for float *************** */
222  /* ***************************************************************** */
223 
224  /* PutArray */
225 
226  template<>
227  PutArray<float>::Func PutArray<float>::ArrayF = WSPutReal32Array;
228 
229  template<>
230  const std::string PutArray<float>::ArrayFName = "WSPutReal32Array";
231 
232  /* PutList */
233 
234  template<>
235  PutList<float>::Func PutList<float>::ListF = WSPutReal32List;
236 
237  template<>
238  const std::string PutList<float>::ListFName = "WSPutReal32List";
239 
240  /* PutScalar */
241 
242  template<>
243  PutScalar<float>::Func PutScalar<float>::ScalarF = WSPutReal32;
244 
245  template<>
246  const std::string PutScalar<float>::ScalarFName = "WSPutReal32";
247 
248  /* ***************************************************************** */
249  /* *********** Template specializations for double *************** */
250  /* ***************************************************************** */
251 
252  /* PutArray */
253 
254  template<>
255  PutArray<double>::Func PutArray<double>::ArrayF = WSPutReal64Array;
256 
257  template<>
258  const std::string PutArray<double>::ArrayFName = "WSPutReal64Array";
259 
260  /* PutList */
261 
262  template<>
263  PutList<double>::Func PutList<double>::ListF = WSPutReal64List;
264 
265  template<>
266  const std::string PutList<double>::ListFName = "WSPutReal64List";
267 
268  /* PutScalar */
269 
270  template<>
271  PutScalar<double>::Func PutScalar<double>::ScalarF = WSPutReal64;
272 
273  template<>
274  const std::string PutScalar<double>::ScalarFName = "WSPutReal64";
275 
276 #endif
277 
279 } /* namespace LLU::WS */
280 
281 #endif /* LLU_WSTP_PUT_H_ */
LLU::ErrorName::WSPutScalarError
const std::string WSPutScalarError
Could not send scalar via WSTP.
Errors.h
Definitions of error names and error codes used across LLU.
LLU::WS
Contains definitions related to WSTP functionality in LLU.
Definition: EncodingTraits.hpp:15
Utilities.h
Header file with miscellaneous utilities used throughout the WSTP-related part of LibraryLinkUtilitie...
LLU::ErrorName::WSPutArrayError
const std::string WSPutArrayError
Could not send array via WSTP.
LLU::ErrorName::WSPutListError
const std::string WSPutListError
Could not send list via WSTP.
Utilities.hpp
Short but generally useful functions.