LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
Get.h
Go to the documentation of this file.
1 
7 #ifndef LLU_WSTP_GET_H_
8 #define LLU_WSTP_GET_H_
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 
14 #include "wstp.h"
15 
16 #include "LLU/ErrorLog/Errors.h"
17 #include "LLU/Utilities.hpp"
18 #include "LLU/WSTP/Release.h"
19 #include "LLU/WSTP/Utilities.h"
20 
21 namespace LLU::WS {
22 
26  template<typename T>
27  using ListData = std::unique_ptr<T[], ReleaseList<T>>;
28 
32  template<typename T>
33  using ArrayData = std::unique_ptr<T[], ReleaseArray<T>>;
34 
35  template<typename T>
36  struct GetArray {
37  using Func = std::function<int(WSLINK, T**, int**, char***, int*)>;
38 
39  static ArrayData<T> get(WSLINK m) {
40  T* rawResult {};
41  int* dims {};
42  char** heads {};
43  int rank {};
44  Detail::checkError(m, ArrayF(m, &rawResult, &dims, &heads, &rank), ErrorName::WSGetArrayError, ArrayFName);
45  return {rawResult, ReleaseArray<T> {m, dims, heads, rank}};
46  }
47 
48  private:
49  static const std::string ArrayFName;
50  static Func ArrayF;
51  };
52 
53  template<typename T>
54  struct GetList {
55  using Func = std::function<int(WSLINK, T**, int*)>;
56 
57  static ListData<T> get(WSLINK m) {
58  T* rawResult {};
59  int len {};
60  Detail::checkError(m, ListF(m, &rawResult, &len), ErrorName::WSGetListError, ListFName);
61  return {rawResult, ReleaseList<T> {m, len}};
62  }
63 
64  private:
65  static const std::string ListFName;
66  static Func ListF;
67  };
68 
69  template<typename T>
70  struct GetScalar {
71  using Func = std::function<int(WSLINK, T*)>;
72 
73  static T get(WSLINK m) {
74  T rawResult;
75  Detail::checkError(m, ScalarF(m, &rawResult), ErrorName::WSGetScalarError, ScalarFName);
76  return rawResult;
77  }
78 
79  private:
80  static const std::string ScalarFName;
81  static Func ScalarF;
82  };
83 
84  template<typename T>
85  typename GetArray<T>::Func GetArray<T>::ArrayF = [](WSLINK /*link*/, T** /*rawResult*/, int** /*dims*/, char*** /*heads*/, int* /*rank*/) {
86  static_assert(dependent_false_v<T>, "Trying to use WS::GetArray<T> for unsupported type T");
87  return 0;
88  };
89 
90  template<typename T>
91  typename GetList<T>::Func GetList<T>::ListF = [](WSLINK /*link*/) {
92  static_assert(dependent_false_v<T>, "Trying to use WS::GetList<T> for unsupported type T");
93  return 0;
94  };
95 
96  template<typename T>
97  typename GetScalar<T>::Func GetScalar<T>::ScalarF = [](WSLINK /*link*/, T* /*result*/) {
98  static_assert(dependent_false_v<T>, "Trying to use WS::GetScalar<T> for unsupported type T");
99  return 0;
100  };
101 
103 #ifndef _WIN32
104 
105 #define WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(T) \
106  template<> \
107  GetArray<T>::Func GetArray<T>::ArrayF; \
108  template<> \
109  const std::string GetArray<T>::ArrayFName; \
110  template<> \
111  GetList<T>::Func GetList<T>::ListF; \
112  template<> \
113  const std::string GetList<T>::ListFName; \
114  template<> \
115  GetScalar<T>::Func GetScalar<T>::ScalarF; \
116  template<> \
117  const std::string GetScalar<T>::ScalarFName;
118 
119  WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(unsigned char)
120  WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(short)
121  WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(int)
122  WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(wsint64)
123  WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(float)
124  WS_GET_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(double)
125 
126 #else
127 
128  /* ***************************************************************** */
129  /* ********* Template specializations for unsigned char ********** */
130  /* ***************************************************************** */
131 
132  /* GetArray */
133 
134  template<>
135  GetArray<unsigned char>::Func GetArray<unsigned char>::ArrayF = WSGetInteger8Array;
136 
137  template<>
138  const std::string GetArray<unsigned char>::ArrayFName = "WSGetInteger8Array";
139 
140  /* GetList */
141 
142  template<>
143  GetList<unsigned char>::Func GetList<unsigned char>::ListF = WSGetInteger8List;
144 
145  template<>
146  const std::string GetList<unsigned char>::ListFName = "WSGetInteger8List";
147 
148  /* GetScalar */
149 
150  template<>
151  GetScalar<unsigned char>::Func GetScalar<unsigned char>::ScalarF = WSGetInteger8;
152 
153  template<>
154  const std::string GetScalar<unsigned char>::ScalarFName = "WSGetInteger8";
155 
156  /* ***************************************************************** */
157  /* ******* Template specializations for (unsigned) short ********* */
158  /* ***************************************************************** */
159 
160  /* GetArray */
161 
162  template<>
163  GetArray<short>::Func GetArray<short>::ArrayF = WSGetInteger16Array;
164 
165  template<>
166  const std::string GetArray<short>::ArrayFName = "WSGetInteger16Array";
167 
168  /* GetList */
169 
170  template<>
171  GetList<short>::Func GetList<short>::ListF = WSGetInteger16List;
172 
173  template<>
174  const std::string GetList<short>::ListFName = "WSGetInteger16List";
175 
176  /* GetScalar */
177 
178  template<>
179  GetScalar<short>::Func GetScalar<short>::ScalarF = WSGetInteger16;
180 
181  template<>
182  const std::string GetScalar<short>::ScalarFName = "WSGetInteger16";
183 
184  /* ***************************************************************** */
185  /* ******** Template specializations for (unsigned) int ********** */
186  /* ***************************************************************** */
187 
188  /* GetArray */
189 
190  template<>
191  GetArray<int>::Func GetArray<int>::ArrayF = WSGetInteger32Array;
192 
193  template<>
194  const std::string GetArray<int>::ArrayFName = "WSGetInteger32Array";
195 
196  /* GetList */
197 
198  template<>
199  GetList<int>::Func GetList<int>::ListF = WSGetInteger32List;
200 
201  template<>
202  const std::string GetList<int>::ListFName = "WSGetInteger32List";
203 
204  /* GetScalar */
205 
206  template<>
207  GetScalar<int>::Func GetScalar<int>::ScalarF = WSGetInteger32;
208 
209  template<>
210  const std::string GetScalar<int>::ScalarFName = "WSGetInteger32";
211 
212  /* ***************************************************************** */
213  /* *********** Template specializations for wsint64 ************** */
214  /* ***************************************************************** */
215 
216  /* GetArray */
217 
218  template<>
219  GetArray<wsint64>::Func GetArray<wsint64>::ArrayF = WSGetInteger64Array;
220 
221  template<>
222  const std::string GetArray<wsint64>::ArrayFName = "WSGetInteger64Array";
223 
224  /* GetList */
225 
226  template<>
227  GetList<wsint64>::Func GetList<wsint64>::ListF = WSGetInteger64List;
228 
229  template<>
230  const std::string GetList<wsint64>::ListFName = "WSGetInteger64List";
231 
232  /* GetScalar */
233 
234  template<>
235  GetScalar<wsint64>::Func GetScalar<wsint64>::ScalarF = WSGetInteger64;
236 
237  template<>
238  const std::string GetScalar<wsint64>::ScalarFName = "WSGetInteger64";
239 
240  /* ***************************************************************** */
241  /* ************ Template specializations for float *************** */
242  /* ***************************************************************** */
243 
244  /* GetArray */
245 
246  template<>
247  GetArray<float>::Func GetArray<float>::ArrayF = WSGetReal32Array;
248 
249  template<>
250  const std::string GetArray<float>::ArrayFName = "WSGetReal32Array";
251 
252  /* GetList */
253 
254  template<>
255  GetList<float>::Func GetList<float>::ListF = WSGetReal32List;
256 
257  template<>
258  const std::string GetList<float>::ListFName = "WSGetReal32List";
259 
260  /* GetScalar */
261 
262  template<>
263  GetScalar<float>::Func GetScalar<float>::ScalarF = WSGetReal32;
264 
265  template<>
266  const std::string GetScalar<float>::ScalarFName = "WSGetReal32";
267 
268  /* ***************************************************************** */
269  /* *********** Template specializations for double *************** */
270  /* ***************************************************************** */
271 
272  /* GetArray */
273 
274  template<>
275  GetArray<double>::Func GetArray<double>::ArrayF = WSGetReal64Array;
276 
277  template<>
278  const std::string GetArray<double>::ArrayFName = "WSGetReal64Array";
279 
280  /* GetList */
281 
282  template<>
283  GetList<double>::Func GetList<double>::ListF = WSGetReal64List;
284 
285  template<>
286  const std::string GetList<double>::ListFName = "WSGetReal64List";
287 
288  /* GetScalar */
289 
290  template<>
291  GetScalar<double>::Func GetScalar<double>::ScalarF = WSGetReal64;
292 
293  template<>
294  const std::string GetScalar<double>::ScalarFName = "WSGetReal64";
295 #endif
296 } /* namespace LLU::WS */
298 
299 #endif /* LLU_WSTP_GET_H_ */
LLU::WS::ArrayData
std::unique_ptr< T[], ReleaseArray< T > > ArrayData
ArrayData with of type T is a unique_ptr to an array of Ts with custom destructor.
Definition: Get.h:33
LLU::ErrorName::WSGetListError
const std::string WSGetListError
Could not get list from WSTP.
Release.h
Header file with classes responsible for releasing memory allocated by WSTP when receiving data.
LLU::ErrorName::WSGetScalarError
const std::string WSGetScalarError
Could not get scalar from WSTP.
Errors.h
Definitions of error names and error codes used across LLU.
LLU::ErrorName::WSGetArrayError
const std::string WSGetArrayError
Could not get array from WSTP.
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::WS::ListData
std::unique_ptr< T[], ReleaseList< T > > ListData
ListData with of type T is a unique_ptr to an array of Ts with custom destructor.
Definition: Get.h:27
Utilities.hpp
Short but generally useful functions.