LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
Release.h
Go to the documentation of this file.
1 
7 #ifndef LLU_WSTP_RELEASE_H_
8 #define LLU_WSTP_RELEASE_H_
9 
10 #include <functional>
11 
12 #include "wstp.h"
13 
14 #include "LLU/Utilities.hpp"
15 
16 namespace LLU::WS {
17 
18  template<typename T>
19  struct ReleaseList {
20  using Func = std::function<void(WSLINK, T*, int)>;
21 
22  ReleaseList() = default;
23  ReleaseList(WSLINK m, int l) : m(m), length(l) {}
24 
25  void operator()(T* data) {
26  Release(m, data, length);
27  }
28 
29  int getLength() const {
30  return length;
31  }
32 
33  private:
34  static Func Release;
35 
36  WSLINK m = nullptr;
37  int length = 0;
38  };
39 
40  template<typename T>
41  struct ReleaseArray {
42  using Func = std::function<void(WSLINK, T*, int*, char**, int)>;
43 
44  ReleaseArray() = default;
45  ReleaseArray(WSLINK m, int* d, char** h, int r) : m(m), dims(d), heads(h), rank(r) {}
46 
47  void operator()(T* data) {
48  Release(m, data, dims, heads, rank);
49  }
50 
51  int* getDims() const {
52  return dims;
53  }
54 
55  char** getHeads() const {
56  return heads;
57  }
58 
59  int getRank() const {
60  return rank;
61  }
62 
63  private:
64  static Func Release;
65 
66  WSLINK m = nullptr;
67  int* dims = nullptr;
68  char** heads = nullptr;
69  int rank = 0;
70  };
71 
72  template<typename T>
73  typename ReleaseArray<T>::Func ReleaseArray<T>::Release = [](WSLINK /*link*/, T* /*array*/, int* /*dims*/, char** /*heads*/, int /*rank*/) {
74  static_assert(dependent_false_v<T>, "Trying to use WS::ReleaseArray<T>::Release for unsupported type T");
75  };
76 
77  template<typename T>
78  typename ReleaseList<T>::Func ReleaseList<T>::Release = [](WSLINK /*link*/, T* /*list*/, int /*length*/) {
79  static_assert(dependent_false_v<T>, "Trying to use WS::ReleaseList<T>::Release for unsupported type T");
80  };
81 
83 #ifndef _WIN32
84 
85 #define WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(T) \
86  template<> \
87  ReleaseArray<T>::Func ReleaseArray<T>::Release; \
88  template<> \
89  ReleaseList<T>::Func ReleaseList<T>::Release;
90 
91  WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(unsigned char)
92  WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(short)
93  WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(int)
94  WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(wsint64)
95  WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(float)
96  WS_RELEASE_DECLARE_SPECIALIZATIONS_OF_STATIC_MEMBERS(double)
97 #else
98 
99  template<>
100  ReleaseArray<unsigned char>::Func ReleaseArray<unsigned char>::Release = WSReleaseInteger8Array;
101 
102  template<>
103  ReleaseList<unsigned char>::Func ReleaseList<unsigned char>::Release = WSReleaseInteger8List;
104 
105  template<>
106  ReleaseArray<short>::Func ReleaseArray<short>::Release = WSReleaseInteger16Array;
107 
108  template<>
109  ReleaseList<short>::Func ReleaseList<short>::Release = WSReleaseInteger16List;
110 
111  template<>
112  ReleaseArray<int>::Func ReleaseArray<int>::Release = WSReleaseInteger32Array;
113 
114  template<>
115  ReleaseList<int>::Func ReleaseList<int>::Release = WSReleaseInteger32List;
116 
117  template<>
118  ReleaseArray<wsint64>::Func ReleaseArray<wsint64>::Release = WSReleaseInteger64Array;
119 
120  template<>
121  ReleaseList<wsint64>::Func ReleaseList<wsint64>::Release = WSReleaseInteger64List;
122 
123  template<>
124  ReleaseArray<float>::Func ReleaseArray<float>::Release = WSReleaseReal32Array;
125 
126  template<>
127  ReleaseList<float>::Func ReleaseList<float>::Release = WSReleaseReal32List;
128 
129  template<>
130  ReleaseArray<double>::Func ReleaseArray<double>::Release = WSReleaseReal64Array;
131 
132  template<>
133  ReleaseList<double>::Func ReleaseList<double>::Release = WSReleaseReal64List;
134 #endif
135 
137 } /* namespace LLU::WS */
138 
139 #endif /* LLU_WSTP_RELEASE_H_ */
LLU::WS
Contains definitions related to WSTP functionality in LLU.
Definition: EncodingTraits.hpp:15
Utilities.hpp
Short but generally useful functions.