LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
Views/NumericArray.hpp
Go to the documentation of this file.
1 
6 #ifndef LLU_CONTAINERS_VIEWS_NUMERICARRAY_HPP
7 #define LLU_CONTAINERS_VIEWS_NUMERICARRAY_HPP
8 
12 
13 namespace LLU {
14 
22  public:
23  NumericArrayView() = default;
24 
29  NumericArrayView(const GenericNumericArray& gNA) : na {gNA.getContainer()} {} // NOLINT: implicit conversion is useful and harmless
30 
35  NumericArrayView(MNumericArray mna) : na {mna} {} // NOLINT:
36 
38  mint getRank() const override {
39  return LibraryData::NumericArrayAPI()->MNumericArray_getRank(na);
40  }
41 
43  mint const* getDimensions() const override {
44  return LibraryData::NumericArrayAPI()->MNumericArray_getDimensions(na);
45  }
46 
48  mint getFlattenedLength() const override {
49  return LibraryData::NumericArrayAPI()->MNumericArray_getFlattenedLength(na);
50  }
51 
53  numericarray_data_t type() const final {
54  return LibraryData::NumericArrayAPI()->MNumericArray_getType(na);
55  }
56 
58  void* rawData() const noexcept override {
59  return LibraryData::NumericArrayAPI()->MNumericArray_getData(na);
60  }
61 
62  private:
63  MNumericArray na = nullptr;
64  };
65 
72  template<typename T>
74  public:
75  NumericArrayTypedView() = default;
76 
82  NumericArrayTypedView(const GenericNumericArray& gNA) : NumericArrayView(gNA) { // NOLINT: implicit conversion is useful and harmless
83  if (NumericArrayType<T> != type()) {
85  }
86  }
87 
93  NumericArrayTypedView(NumericArrayView nav) : NumericArrayView(std::move(nav)) { // NOLINT
94  if (NumericArrayType<T> != type()) {
96  }
97  }
98 
104  NumericArrayTypedView(MNumericArray mna) : NumericArrayView(mna) { // NOLINT
105  if (NumericArrayType<T> != type()) {
107  }
108  }
109 
110  private:
111  T* getData() const noexcept override {
112  return static_cast<T*>(rawData());
113  }
114 
115  mint getSize() const noexcept override {
116  return getFlattenedLength();
117  }
118  };
119 
128  template<typename NumericArrayT, typename F>
129  auto asTypedNumericArray(NumericArrayT&& na, F&& callable) {
130  switch (na.type()) {
131  case MNumericArray_Type_Bit8: return std::forward<F>(callable)(NumericArrayTypedView<std::int8_t> {std::forward<NumericArrayT>(na)});
132  case MNumericArray_Type_UBit8: return std::forward<F>(callable)(NumericArrayTypedView<std::uint8_t> {std::forward<NumericArrayT>(na)});
133  case MNumericArray_Type_Bit16: return std::forward<F>(callable)(NumericArrayTypedView<std::int16_t> {std::forward<NumericArrayT>(na)});
134  case MNumericArray_Type_UBit16: return std::forward<F>(callable)(NumericArrayTypedView<std::uint16_t> {std::forward<NumericArrayT>(na)});
135  case MNumericArray_Type_Bit32: return std::forward<F>(callable)(NumericArrayTypedView<std::int32_t> {std::forward<NumericArrayT>(na)});
136  case MNumericArray_Type_UBit32: return std::forward<F>(callable)(NumericArrayTypedView<std::uint32_t> {std::forward<NumericArrayT>(na)});
137  case MNumericArray_Type_Bit64: return std::forward<F>(callable)(NumericArrayTypedView<std::int64_t> {std::forward<NumericArrayT>(na)});
138  case MNumericArray_Type_UBit64: return std::forward<F>(callable)(NumericArrayTypedView<std::uint64_t> {std::forward<NumericArrayT>(na)});
139  case MNumericArray_Type_Real32: return std::forward<F>(callable)(NumericArrayTypedView<float> {std::forward<NumericArrayT>(na)});
140  case MNumericArray_Type_Real64: return std::forward<F>(callable)(NumericArrayTypedView<double> {std::forward<NumericArrayT>(na)});
141  case MNumericArray_Type_Complex_Real32:
142  return std::forward<F>(callable)(NumericArrayTypedView<std::complex<float>> {std::forward<NumericArrayT>(na)});
143  case MNumericArray_Type_Complex_Real64:
144  return std::forward<F>(callable)(NumericArrayTypedView<std::complex<double>> {std::forward<NumericArrayT>(na)});
146  }
147  }
148 
150  // Specialization of asTypedNumericArray for MNumericArray
151  template<typename F>
152  auto asTypedNumericArray(MNumericArray na, F&& callable) {
153  return asTypedNumericArray(NumericArrayView {na}, std::forward<F>(callable));
154  }
156 } // namespace LLU
157 
158 #endif // LLU_CONTAINERS_VIEWS_NUMERICARRAY_HPP
LLU::NumericArrayInterface
Abstract class that defines a basic set of operations on a numeric array.
Definition: Interfaces.h:106
LLU::NumericArrayTypedView::NumericArrayTypedView
NumericArrayTypedView(MNumericArray mna)
Create a NumericArrayTypedView from a raw MNumericArray.
Definition: Views/NumericArray.hpp:104
LLU::NumericArrayTypedView
Simple, light-weight, non-owning wrappper over MNumericArray.
Definition: Views/NumericArray.hpp:73
LLU::IterableContainer
Abstract class that provides iterators (c/r/begin and c/r/end methods) and subscript operator for any...
Definition: IterableContainer.hpp:20
LLU::MContainer< MArgumentType::NumericArray >
MContainer specialization for MNumericArray.
Definition: Generic/NumericArray.hpp:23
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::NumericArrayView::getRank
mint getRank() const override
Get rank.
Definition: Views/NumericArray.hpp:38
LLU::NumericArrayTypedView::NumericArrayTypedView
NumericArrayTypedView(const GenericNumericArray &gNA)
Create a NumericArrayTypedView from a GenericNumericArray.
Definition: Views/NumericArray.hpp:82
LLU::NumericArrayView::rawData
void * rawData() const noexcept override
Get access to the raw data.
Definition: Views/NumericArray.hpp:58
LLU::NumericArrayView::NumericArrayView
NumericArrayView(MNumericArray mna)
Create a NumericArrayView from a raw MNumericArray.
Definition: Views/NumericArray.hpp:35
LLU::NumericArrayView::type
numericarray_data_t type() const final
Get the data type of this array.
Definition: Views/NumericArray.hpp:53
LLU::ErrorName::NumericArrayTypeError
const std::string NumericArrayTypeError
NumericArray type mismatch.
LLU::NumericArrayView
Simple, light-weight, non-owning, data-type-agnostic wrappper over MNumericArray.
Definition: Views/NumericArray.hpp:21
LLU::NumericArrayView::getDimensions
mint const * getDimensions() const override
Get dimensions.
Definition: Views/NumericArray.hpp:43
LLU::MContainerBase::getContainer
Container getContainer() const noexcept
Get internal container.
Definition: Base.hpp:97
LLU::NumericArrayView::getFlattenedLength
mint getFlattenedLength() const override
Get length.
Definition: Views/NumericArray.hpp:48
LLU::ErrorManager::throwException
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
LLU::asTypedNumericArray
auto asTypedNumericArray(NumericArrayT &&na, F &&callable)
Take a NumericArray-like object na and a function callable and call the function with a NumericArrayT...
Definition: Views/NumericArray.hpp:129
LLU::NumericArrayTypedView::NumericArrayTypedView
NumericArrayTypedView(NumericArrayView nav)
Create a NumericArrayTypedView from a NumericArrayView.
Definition: Views/NumericArray.hpp:93
LLU::LibraryData::NumericArrayAPI
static const st_WolframNumericArrayLibrary_Functions * NumericArrayAPI()
Get a pointer to structure with function pointers to MNumericArray API.
Definition: LibraryData.cpp:33
LLU::NumericArrayView::NumericArrayView
NumericArrayView(const GenericNumericArray &gNA)
Create a NumericArrayView from a GenericNumericArray.
Definition: Views/NumericArray.hpp:29
Interfaces.h
IterableContainer.hpp
Implementation of the IterableContainer class.
NumericArray.hpp
GenericNumericArray definition and implementation.