LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
Views/Tensor.hpp
Go to the documentation of this file.
1 
6 #ifndef LLU_CONTAINERS_VIEWS_TENSOR_HPP
7 #define LLU_CONTAINERS_VIEWS_TENSOR_HPP
8 
12 
13 namespace LLU {
14 
21  class TensorView : public TensorInterface {
22  public:
23  TensorView() = default;
24 
29  TensorView(const GenericTensor& gTen) : t {gTen.getContainer()} {} // NOLINT: implicit conversion to a view is useful and harmless
30 
35  TensorView(MTensor mt) : t {mt} {} // NOLINT
36 
38  mint getRank() const override {
39  return LibraryData::API()->MTensor_getRank(t);
40  }
41 
43  mint const* getDimensions() const override {
44  return LibraryData::API()->MTensor_getDimensions(t);
45  }
46 
48  mint getFlattenedLength() const override {
49  return LibraryData::API()->MTensor_getFlattenedLength(t);
50  }
51 
53  mint type() const final {
54  return LibraryData::API()->MTensor_getType(t);
55  }
56 
58  void* rawData() const override {
59  switch (type()) {
60  case MType_Integer: return LibraryData::API()->MTensor_getIntegerData(t);
61  case MType_Real: return LibraryData::API()->MTensor_getRealData(t);
62  case MType_Complex: return LibraryData::API()->MTensor_getComplexData(t);
63  default: return nullptr;
64  }
65  }
66 
67  private:
68  MTensor t = nullptr;
69  };
70 
71  template<typename T>
72  class TensorTypedView : public TensorView, public IterableContainer<T> {
73  public:
74  TensorTypedView() = default;
75 
81  TensorTypedView(const GenericTensor& gTen) : TensorView(gTen) { // NOLINT: implicit conversion to a view is useful and harmless
82  if (TensorType<T> != type()) {
84  }
85  }
86 
92  TensorTypedView(TensorView tv) : TensorView(std::move(tv)) { // NOLINT
93  if (TensorType<T> != type()) {
95  }
96  }
97 
103  TensorTypedView(MTensor mt) : TensorView(mt) { // NOLINT
104  if (TensorType<T> != type()) {
106  }
107  }
108 
109  private:
110  T* getData() const noexcept override {
111  return static_cast<T*>(rawData());
112  }
113 
114  mint getSize() const noexcept override {
115  return getFlattenedLength();
116  }
117  };
118 
127  template<typename TensorT, typename F>
128  auto asTypedTensor(TensorT&& t, F&& callable) {
129  switch (t.type()) {
130  case MType_Integer: return std::forward<F>(callable)(TensorTypedView<mint> {std::forward<TensorT>(t)});
131  case MType_Real: return std::forward<F>(callable)(TensorTypedView<double> {std::forward<TensorT>(t)});
132  case MType_Complex: return std::forward<F>(callable)(TensorTypedView<std::complex<double>> {std::forward<TensorT>(t)});
134  }
135  }
136 
138  // Specialization of asTypedTensor for MTensor
139  template<typename F>
140  auto asTypedTensor(MTensor t, F&& callable) {
141  return asTypedTensor(TensorView {t}, std::forward<F>(callable));
142  }
144 } // namespace LLU
145 
146 #endif // LLU_CONTAINERS_VIEWS_TENSOR_HPP
LLU::TensorView::getFlattenedLength
mint getFlattenedLength() const override
Get total length.
Definition: Views/Tensor.hpp:48
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::TensorView::getDimensions
mint const * getDimensions() const override
Get dimensions.
Definition: Views/Tensor.hpp:43
LLU::LibraryData::API
static WolframLibraryData API()
Get currently owned WolframLibraryData, if any.
Definition: LibraryData.cpp:22
LLU::TensorView::TensorView
TensorView(const GenericTensor &gTen)
Create a NumericArrayView from a GenericNumericArray.
Definition: Views/Tensor.hpp:29
LLU::TensorView::getRank
mint getRank() const override
Get rank.
Definition: Views/Tensor.hpp:38
LLU::TensorView::TensorView
TensorView(MTensor mt)
Create a NumericArrayView from a raw MNumericArray.
Definition: Views/Tensor.hpp:35
LLU::GenericTensor
MContainer< MArgumentType::Tensor > GenericTensor
MContainer specialization for MTensor is called GenericTensor.
Definition: Generic/Tensor.hpp:18
Tensor.hpp
GenericTensor definition and implementation.
LLU::TensorInterface
Abstract class that defines a basic set of operations on a tensor.
Definition: Interfaces.h:157
LLU::MContainerBase::getContainer
Container getContainer() const noexcept
Get internal container.
Definition: Base.hpp:97
LLU::ErrorName::TensorTypeError
const std::string TensorTypeError
Tensor type mismatch.
LLU::asTypedTensor
auto asTypedTensor(TensorT &&t, F &&callable)
Take a Tensor-like object t and a function callable and call the function with a TensorTypedView crea...
Definition: Views/Tensor.hpp:128
LLU::ErrorManager::throwException
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
LLU::TensorView::rawData
void * rawData() const override
Get raw pointer to the data of this tensor.
Definition: Views/Tensor.hpp:58
LLU::MContainer< MArgumentType::Tensor >
MContainer specialization for MTensor.
Definition: Generic/Tensor.hpp:24
LLU::TensorView
Simple, light-weight, non-owning wrappper over MTensor.
Definition: Views/Tensor.hpp:21
Interfaces.h
LLU::TensorView::type
mint type() const final
Get the data type of this tensor.
Definition: Views/Tensor.hpp:53
IterableContainer.hpp
Implementation of the IterableContainer class.