LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
TypedMArgument.h
Go to the documentation of this file.
1 
7 #ifndef LLU_TYPEDMARGUMENT_H
8 #define LLU_TYPEDMARGUMENT_H
9 
10 #include <complex>
11 #include <string_view>
12 #include <variant>
13 
18 #include "LLU/MArgument.h"
19 #include "LLU/Utilities.hpp"
20 
21 template<>
23 
27 namespace LLU::Argument {
28 
33  namespace Typed {
35  using Boolean = bool;
36 
38  using Integer = mint;
39 
41  using Real = double;
42 
44  using Complex = std::complex<double>;
45 
48 
51 
54 
57 
59  using UTF8String = std::string_view;
60 
63  } // namespace Typed
64 
68  namespace Typed {
70  using Any = TypedArgument;
71  } // namespace Typed
72 
77  template<typename T>
78  inline constexpr MArgumentType WrapperIndex = static_cast<MArgumentType>(variant_index<TypedArgument, T>());
79 
84  template<typename T>
85  inline constexpr bool WrapperQ = std::is_same_v<T, TypedArgument> || (variant_index<TypedArgument, T>() < std::variant_size_v<TypedArgument>);
86 
91  template<MArgumentType T>
92  using WrapperType = std::conditional_t<T == MArgumentType::MArgument, TypedArgument, std::variant_alternative_t<static_cast<size_t>(T), TypedArgument>>;
93 
102  TypedArgument fromMArgument(MArgument m, MArgumentType t);
103 
111  void toMArgument(const TypedArgument& tma, MArgument& res);
112 
119  template<MArgumentType T>
121  if constexpr (T == MArgumentType::Complex) {
122  return {value->ri[0], value->ri[1]};
123  } else if constexpr (T == MArgumentType::UTF8String) {
124  return {value};
125  } else if constexpr (ContainerTypeQ<T>) {
126  return {value, Ownership::LibraryLink};
127  } else {
128  return value;
129  }
130  }
131 
138  template<MArgumentType T>
140  if constexpr (T == MArgumentType::Complex) {
141  return {value.real(), value.imag()};
142  } else if constexpr (T == MArgumentType::UTF8String) {
143  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast): LibraryLink will not modify the string, so const_cast is safe here
144  return const_cast<char*>(value.data());
145  } else if constexpr (ContainerTypeQ<T>) {
146  return value.abandonContainer();
147  } else {
148  return value;
149  }
150  }
151 } // namespace LLU::Argument
152 
153 #endif // LLU_TYPEDMARGUMENT_H
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::MContainer< MArgumentType::DataStore >
MContainer specialization for DataStore, provides basic list interface for the underlying raw DataSto...
Definition: Generic/DataStore.hpp:23
LLU::MContainer< MArgumentType::NumericArray >
MContainer specialization for MNumericArray.
Definition: Generic/NumericArray.hpp:23
LLU::Argument::TypedArgument
std::variant< std::monostate, Typed::Boolean, Typed::Integer, Typed::Real, Typed::Complex, Typed::Tensor, Typed::SparseArray, Typed::NumericArray, Typed::Image, Typed::UTF8String, Typed::DataStore > TypedArgument
C++ wrapper over LibraryLink's MArgument, which is a plain union.
Definition: TypedMArgument.h:67
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::MContainer
MContainer is an abstract class template for generic containers. Only specializations shall be used.
Definition: Base.hpp:245
LLU::Argument::Typed::NumericArray
MContainer< MArgumentType::NumericArray > NumericArray
NumericArray stands for a GenericNumericArray - type agnostic wrapper over MNumericArray.
Definition: TypedMArgument.h:53
LLU::MContainer< MArgumentType::SparseArray >
MContainer specialization for MSparseArray.
Definition: SparseArray.hpp:23
LLU::Argument::toMArgument
void toMArgument(const TypedArgument &tma, MArgument &res)
Assign current value of given TypedArgument to given MArgument.
Definition: TypedMArgument.cpp:38
LLU::Argument::WrapperQ
constexpr bool WrapperQ
Type trait to determine if given type is a "wrapper type" (i.e.
Definition: TypedMArgument.h:85
LLU::Argument::Typed::Any
TypedArgument Any
Any is a union of all supported types. Typed::Any can be used as a template parameter for DataList to...
Definition: TypedMArgument.h:70
LLU::Argument::Typed::SparseArray
MContainer< MArgumentType::SparseArray > SparseArray
SparseArray stands for a GenericSparseArray - type agnostic wrapper over MSparseArray.
Definition: TypedMArgument.h:50
LLU::Argument::toPrimitiveType
CType< T > toPrimitiveType(const WrapperType< T > &value)
Convert value of one of the wrapper types (see LLU::Argument::Typed namespace) to the corresponding L...
Definition: TypedMArgument.h:139
LLU::Argument::toWrapperType
WrapperType< T > toWrapperType(const CType< T > &value)
Convert a value of one of the LibraryLink argument type to its wrapper.
Definition: TypedMArgument.h:120
LLU::MContainer< MArgumentType::Image >
MContainer specialization for MImage.
Definition: Generic/Image.hpp:23
Image.hpp
GenericImage definition and implementation.
Tensor.hpp
GenericTensor definition and implementation.
LLU::Argument::Typed::DataStore
MContainer< MArgumentType::DataStore > DataStore
DataStore stands for a GenericDataList - type agnostic wrapper over DataStore.
Definition: TypedMArgument.h:62
LLU::Argument::WrapperType
std::conditional_t< T==MArgumentType::MArgument, TypedArgument, std::variant_alternative_t< static_cast< size_t >(T), TypedArgument > > WrapperType
Get wrapper type corresponding to primitive LibraryLink argument type T.
Definition: TypedMArgument.h:92
LLU::Argument::Typed::UTF8String
std::string_view UTF8String
String values from LibraryLink (char*) are wrapped in std::string_view.
Definition: TypedMArgument.h:59
SparseArray.hpp
GenericSparseArray definition and implementation.
LLU::Argument::Typed::Image
MContainer< MArgumentType::Image > Image
Image stands for a GenericImage - type agnostic wrapper over MImage.
Definition: TypedMArgument.h:56
LLU::Argument::WrapperIndex
constexpr MArgumentType WrapperIndex
Index of a wrapper type in the TypedArgument variant or std::variant_size_v<TypedArgument> otherwise.
Definition: TypedMArgument.h:78
LLU::Argument::Typed::Integer
mint Integer
Machine integer type.
Definition: TypedMArgument.h:38
LLU::MArgumentType
MArgumentType
Strongly type enum with possible types of data stored in MArgument.
Definition: MArgument.h:22
LLU::MContainer< MArgumentType::Tensor >
MContainer specialization for MTensor.
Definition: Generic/Tensor.hpp:24
LLU::Ownership::LibraryLink
@ LibraryLink
LibraryLink is responsible for managing the container's memory. Corresponds to Automatic and "Constan...
LLU::Argument::Typed::Complex
std::complex< double > Complex
Complex number type, bitwise-compatible with mcomplex defined in WolframLibrary.h.
Definition: TypedMArgument.h:44
MArgument.h
Template class and utilities to work with MArgument in type-safe manner.
LLU::Argument::Typed::Real
double Real
Double precision floating point type.
Definition: TypedMArgument.h:41
LLU::Argument::fromMArgument
TypedArgument fromMArgument(MArgument m, MArgumentType t)
Get a TypeArgument from plain MArgument and an enum describing the active union member.
Definition: TypedMArgument.cpp:17
LLU::Argument::Typed::Tensor
MContainer< MArgumentType::Tensor > Tensor
Tensor stands for a GenericTensor - type agnostic wrapper over MTensor.
Definition: TypedMArgument.h:47
Utilities.hpp
Short but generally useful functions.
LLU::Argument
Namespace for functionality related to arguments passed from LibraryLink to library functions and the...
Definition: MArgument.h:36
LLU::Argument::Typed::Boolean
bool Boolean
Boolean type, corresponds to True or False in the Wolfram Language.
Definition: TypedMArgument.h:35
NumericArray.hpp
GenericNumericArray definition and implementation.