LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
MArgument.h
Go to the documentation of this file.
1 
8 #ifndef LLU_MARGUMENT_H
9 #define LLU_MARGUMENT_H
10 
11 #include <string>
12 #include <variant>
13 
14 #include "LLU/LibraryData.h"
15 #include "LLU/Utilities.hpp"
16 
17 namespace LLU {
18 
22  enum class MArgumentType {
23  MArgument = MType_Undef,
24  Boolean = MType_Boolean,
25  Integer = MType_Integer,
26  Real = MType_Real,
27  Complex = MType_Complex,
28  Tensor = MType_Tensor,
29  SparseArray = MType_SparseArray,
30  NumericArray = MType_NumericArray,
31  Image = MType_Image,
32  UTF8String = MType_UTF8String,
33  DataStore = MType_DataStore
34  };
35 
36  namespace Argument {
38  using PrimitiveAny = std::variant<std::monostate, mbool, mint, mreal, mcomplex, MTensor, MSparseArray, MNumericArray, MImage, char*, DataStore>;
39 
41  template<typename T>
42  inline constexpr MArgumentType PrimitiveIndex = static_cast<MArgumentType>(variant_index<PrimitiveAny, T>());
43 
45  template<typename T>
46  inline constexpr bool PrimitiveQ = (variant_index<PrimitiveAny, T>() < std::variant_size_v<PrimitiveAny>);
47 
52  template<MArgumentType T>
53  using CType = std::conditional_t<T == MArgumentType::MArgument, MArgument, std::variant_alternative_t<static_cast<size_t>(T), PrimitiveAny>>;
54 
58  template<MArgumentType T>
59  inline constexpr bool ContainerTypeQ = (T == MArgumentType::Tensor || T == MArgumentType::Image || T == MArgumentType::NumericArray ||
60  T == MArgumentType::DataStore || T == MArgumentType::SparseArray);
61  } // namespace Argument
62 
66  template<MArgumentType T>
67  inline constexpr bool alwaysFalse = false;
68 
69 
75  template<MArgumentType T>
77  public:
80 
81  public:
86  explicit PrimitiveWrapper(MArgument& a) : arg(a) {}
87 
93 
98  const value_type& get() const;
99 
105 
110  void set(value_type newValue);
111 
119  void addToDataStore(DataStore ds, const std::string& name, MArgumentType actualType = T) const;
120 
128  static void addDataStoreNode(DataStore ds, std::string_view name, value_type val);
129 
136  static void addDataStoreNode(DataStore ds, value_type val);
137 
138  private:
139  MArgument& arg;
140  };
141 
142  template<MArgumentType T>
143  void PrimitiveWrapper<T>::addToDataStore(DataStore ds, const std::string& name, [[maybe_unused]] MArgumentType actualType) const {
144  addDataStoreNode(ds, name, get());
145  }
146 
147  /* Explicit specialization for member functions of PrimitiveWrapper class */
149 #define LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(ArgType) \
150  template<> \
151  auto PrimitiveWrapper<MArgumentType::ArgType>::get()->typename PrimitiveWrapper::value_type&; \
152  template<> \
153  auto PrimitiveWrapper<MArgumentType::ArgType>::get() const->const typename PrimitiveWrapper::value_type&; \
154  template<> \
155  void PrimitiveWrapper<MArgumentType::ArgType>::addDataStoreNode(DataStore ds, std::string_view name, value_type val); \
156  template<> \
157  void PrimitiveWrapper<MArgumentType::ArgType>::addDataStoreNode(DataStore ds, value_type val); \
158  template<> \
159  auto PrimitiveWrapper<MArgumentType::ArgType>::getAddress() const->typename PrimitiveWrapper::value_type*; \
160  template<> \
161  void PrimitiveWrapper<MArgumentType::ArgType>::set(typename PrimitiveWrapper::value_type newValue);
162 
163  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(Boolean)
164  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(Integer)
165  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(Real)
166  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(Complex)
167  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(Tensor)
168  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(DataStore)
169  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(SparseArray)
170  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(NumericArray)
171  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(Image)
172  LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS(UTF8String)
173 
174  template<>
176  template<>
177  auto PrimitiveWrapper<MArgumentType::MArgument>::get() const -> const typename PrimitiveWrapper::value_type&;
178  template<>
179  void PrimitiveWrapper<MArgumentType::MArgument>::addDataStoreNode(DataStore ds, std::string_view name, value_type val) = delete;
180  template<>
181  void PrimitiveWrapper<MArgumentType::MArgument>::addDataStoreNode(DataStore ds, value_type val) = delete;
182  template<>
183  auto PrimitiveWrapper<MArgumentType::MArgument>::getAddress() const -> typename PrimitiveWrapper::value_type*;
184  template<>
185  void PrimitiveWrapper<MArgumentType::MArgument>::set(typename PrimitiveWrapper::value_type newValue);
186  template<>
187  void PrimitiveWrapper<MArgumentType::MArgument>::addToDataStore(DataStore ds, const std::string& name, MArgumentType actualType) const;
188 
189 #undef LLU_ARGUMENT_DEFINE_SPECIALIZATIONS_OF_MEMBER_FUNCTIONS
190 
192 } // namespace LLU
193 
194 #endif // LLU_MARGUMENT_H
LLU::PrimitiveWrapper::set
void set(value_type newValue)
Set new value of type T in MArgument.
LLU::Argument::ContainerTypeQ
constexpr bool ContainerTypeQ
Helper template variable that says if an MArgumentType is a LibraryLink container type.
Definition: MArgument.h:59
LLU::PrimitiveWrapper::addToDataStore
void addToDataStore(DataStore ds, const std::string &name, MArgumentType actualType=T) const
Add arg to the DataStore ds inside a node named name The optional parameter should only be used by ex...
Definition: MArgument.h:143
LLU::Argument::PrimitiveQ
constexpr bool PrimitiveQ
Type trait for checking if T is a primitive LibraryLink type (belongs to the MArgument union)
Definition: MArgument.h:46
LLU::Argument::CType
std::conditional_t< T==MArgumentType::MArgument, MArgument, std::variant_alternative_t< static_cast< size_t >(T), PrimitiveAny > > CType
Type alias that binds given MArgumentType (enumerated value) to the corresponding type of MArgument.
Definition: MArgument.h:53
LLU::NumericArray
This is a class template, where template parameter T is the type of data elements....
Definition: NumericArray.h:54
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::Image
This is a class template, where template parameter T is the type of data elements....
Definition: Image.h:127
LLU::PrimitiveWrapper::getAddress
value_type * getAddress() const
Get address of the value stored in MArgument.
LibraryData.h
LLU::Argument::PrimitiveAny
std::variant< std::monostate, mbool, mint, mreal, mcomplex, MTensor, MSparseArray, MNumericArray, MImage, char *, DataStore > PrimitiveAny
A variant holding all possible MArgument types.
Definition: MArgument.h:38
LLU::Tensor
This is a class template, where template parameter T is the type of data elements....
Definition: Tensor.h:53
LLU::PrimitiveWrapper::addDataStoreNode
static void addDataStoreNode(DataStore ds, std::string_view name, value_type val)
Add val to the DataStore ds inside a node named name This is a static method because there is no MArg...
LLU::PrimitiveWrapper::get
const value_type & get() const
Get the read-only value stored in MArgument.
LLU::PrimitiveWrapper
Small class that wraps a reference to MArgument and provides proper API to work with this MArgument.
Definition: MArgument.h:76
LLU::PrimitiveWrapper::PrimitiveWrapper
PrimitiveWrapper(MArgument &a)
Construct PrimitiveWrapper from a reference to MArgument.
Definition: MArgument.h:86
LLU::PrimitiveWrapper::get
value_type & get()
Get the value stored in MArgument.
LLU::MArgumentType
MArgumentType
Strongly type enum with possible types of data stored in MArgument.
Definition: MArgument.h:22
LLU::SparseArray
Strongly typed wrapper for MSparseArray.
Definition: SparseArray.h:20
LLU::alwaysFalse
constexpr bool alwaysFalse
Helper template variable that is always false.
Definition: MArgument.h:67
LLU::PrimitiveWrapper::value_type
Argument::CType< T > value_type
This is the actual type of data stored in arg.
Definition: MArgument.h:79
LLU::PrimitiveWrapper::addDataStoreNode
static void addDataStoreNode(DataStore ds, value_type val)
Add val to the DataStore ds inside an unnamed node This is a static method because there is no MArgum...
LLU::Argument::PrimitiveIndex
constexpr MArgumentType PrimitiveIndex
PrimitiveIndex<T> is the index of type T in the PrimitiveAny variant converted to MArgumentType enum.
Definition: MArgument.h:42
Utilities.hpp
Short but generally useful functions.