LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
WSTP/Utilities.h
Go to the documentation of this file.
1 
7 #ifndef LLU_WSTP_UTILITIES_H_
8 #define LLU_WSTP_UTILITIES_H_
9 
10 #include <cstring>
11 #include <string>
12 
13 #include "wstp.h"
14 
15 #include "LLU/ErrorLog/Errors.h"
17 
18 namespace LLU {
19 
20  template<WS::Encoding, WS::Encoding>
21  class WSStream;
22 
26  namespace WS {
27 
32  struct Symbol {
37  Symbol() = default;
38 
43  explicit Symbol(std::string h) : head(std::move(h)) {};
44 
49  const std::string& getHead() const;
50 
55  void setHead(std::string h);
56 
57  private:
58  std::string head;
59  };
60 
65  struct Function : Symbol {
66 
73  Function() : Function("", -1) {};
74 
80  explicit Function(const std::string& h) : Function(h, -1) {}
81 
87  Function(const std::string& h, int argCount) : Symbol(h), argc(argCount) {}
88 
93  int getArgc() const;
94 
99  void setArgc(int newArgc);
100 
101  private:
102  int argc;
103  };
104 
110  Association() : Function("Association") {}
111 
116  explicit Association(int argCount) : Function("Association", argCount) {}
117  };
118 
122  struct List : Function {
124  List() : Function("List") {}
125 
130  explicit List(int argCount) : Function("List", argCount) {}
131  };
132 
136  struct Missing : Function {
138  Missing() : Function("Missing") {}
139 
144  explicit Missing(std::string r) : Function("Missing", 1), reason(std::move(r)) {}
145 
151  const std::string& why() const {
152  return reason;
153  }
154 
155  private:
156  std::string reason;
157  };
158 
159  namespace Detail {
167  void checkError(WSLINK m, int statusOk, const std::string& errorName, const std::string& debugInfo = "");
168 
174  [[noreturn]] void throwLLUException(const std::string& errorName, const std::string& debugInfo = "");
175 
181  WSLINK getNewLoopback(WSLINK m);
182 
188  int countExpressionsInLoopbackLink(WSLINK& lpbckLink);
189  } // namespace Detail
190 
192  enum class Direction : bool { Get, Put };
193 
202  template<WS::Encoding EIn, WS::Encoding EOut>
204  Detail::checkError(ms.get(), WSNewPacket(ms.get()), ErrorName::WSPacketHandleError, "Error in WSNewPacket");
205  return ms;
206  }
207 
216  template<WS::Encoding EIn, WS::Encoding EOut>
218  Detail::checkError(ms.get(), WSEndPacket(ms.get()), ErrorName::WSPacketHandleError, "Error in WSEndPacket");
219  return ms;
220  }
221 
230  template<WS::Encoding EIn, WS::Encoding EOut>
232  Detail::checkError(ms.get(), WSFlush(ms.get()), ErrorName::WSFlowControlError, "Error in WSFlush");
233  return ms;
234  }
235 
244  template<WS::Encoding EIn, WS::Encoding EOut>
246  if (dir == Direction::Put) {
247  return ms << Function("Rule", 2);
248  }
249  return ms >> Function("Rule", 2);
250  }
251 
260  template<WS::Encoding EIn, WS::Encoding EOut>
262  if (dir == Direction::Put) {
263  return ms << Symbol("Null");
264  }
265  return ms >> Symbol("Null");
266  }
267 
272  struct BeginExpr : Symbol {
277  explicit BeginExpr(const std::string& head) : Symbol(head) {}
278  };
279 
284  struct DropExpr {};
285 
290  struct EndExpr {};
291 
292  } // namespace WS
293 } // namespace LLU
294 
295 #endif /* LLU_WSTP_UTILITIES_H_ */
LLU::WS::List::List
List(int argCount)
Create a WSStream-compatible structure that corresponds to a List of given length.
Definition: WSTP/Utilities.h:130
LLU::WS::Direction
Direction
Helper enum for tokens that can be sent via WSTP in both directions, e.g. WS::Null.
Definition: WSTP/Utilities.h:192
LLU::WS::Symbol::Symbol
Symbol()=default
Default constructor.
LLU::WS::Symbol::setHead
void setHead(std::string h)
Set Symbol name/head.
Definition: Utilities.cpp:17
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::WS::Flush
WSStream< EIn, EOut > & Flush(WSStream< EIn, EOut > &ms)
Flush is a WSStream token which tells WSTP to flush out any buffers containing data waiting to be sen...
Definition: WSTP/Utilities.h:231
LLU::WSStream
Wrapper class over WSTP with a stream-like interface.
Definition: WSTP/Utilities.h:21
LLU::WS::Function
Structure representing any function in Wolfram Language, i.e. a head plus number of arguments.
Definition: WSTP/Utilities.h:65
LLU::WS::Missing
Special type of a Function which corresponds to the Missing expression when exchanged with the Kernel...
Definition: WSTP/Utilities.h:136
LLU::WS::BeginExpr
Definition: WSTP/Utilities.h:272
LLU::WS::Symbol
Structure representing any symbol in Wolfram Language.
Definition: WSTP/Utilities.h:32
LLU::WS::Missing::Missing
Missing()
Default constructor - sets "Missing" to be the head of expression with no arguments.
Definition: WSTP/Utilities.h:138
LLU::WS::BeginExpr::BeginExpr
BeginExpr(const std::string &head)
Create a token for the WSStream which initiates sending an expression with given head and unknown num...
Definition: WSTP/Utilities.h:277
LLU::WS::Function::Function
Function()
Default constructor.
Definition: WSTP/Utilities.h:73
LLU::WS::Association::Association
Association(int argCount)
Create a WSStream-compatible structure that corresponds to an Association with given number of argume...
Definition: WSTP/Utilities.h:116
LLU::ErrorName::WSFlowControlError
const std::string WSFlowControlError
One of the flow control functions failed.
EncodingTraits.hpp
Defines possible encodings together with their properties and related types.
LLU::WS::Missing::Missing
Missing(std::string r)
Create a WSStream-compatible structure that corresponds to a Missing expression with given "reason".
Definition: WSTP/Utilities.h:144
LLU::WS::Function::getArgc
int getArgc() const
Get argument count.
Definition: Utilities.cpp:21
LLU::WS::EndPacket
WSStream< EIn, EOut > & EndPacket(WSStream< EIn, EOut > &ms)
EndPacket is a WSStream token which tells WSTP that the current expression is complete and is ready t...
Definition: WSTP/Utilities.h:217
LLU::WS::Function::setArgc
void setArgc(int newArgc)
Set argument count.
Definition: Utilities.cpp:25
LLU::WS::Association::Association
Association()
Default constructor - sets "Association" to be the head of expression with no arguments.
Definition: WSTP/Utilities.h:110
LLU::WS::Null
WSStream< EIn, EOut > & Null(WSStream< EIn, EOut > &ms, Direction dir)
Null is a WSStream token corresponding to a Null expression in the WolframLanguage.
Definition: WSTP/Utilities.h:261
Errors.h
Definitions of error names and error codes used across LLU.
LLU::WS::List::List
List()
Default constructor - sets "List" to be the head of expression with no arguments.
Definition: WSTP/Utilities.h:124
LLU::ErrorName::WSPacketHandleError
const std::string WSPacketHandleError
One of the packet handling functions failed.
LLU::WS::Association
Special type of a Function which corresponds to the Association expression when exchanged with the Ke...
Definition: WSTP/Utilities.h:108
LLU::WS::NewPacket
WSStream< EIn, EOut > & NewPacket(WSStream< EIn, EOut > &ms)
NewPacket is a WSStream token which tells WSTP to skip to the end of the current packet.
Definition: WSTP/Utilities.h:203
LLU::WS::DropExpr
Definition: WSTP/Utilities.h:284
LLU::WS::Function::Function
Function(const std::string &h, int argCount)
Construct a Function with given head and number of arguments.
Definition: WSTP/Utilities.h:87
LLU::WS::EndExpr
Definition: WSTP/Utilities.h:290
LLU::WS::Rule
WSStream< EIn, EOut > & Rule(WSStream< EIn, EOut > &ms, Direction dir)
Rule is a WSStream token corresponding to a Rule expression in the WolframLanguage.
Definition: WSTP/Utilities.h:245
LLU::WS::Missing::why
const std::string & why() const
Get the first argument of the expression which is the reason for the data's being missing.
Definition: WSTP/Utilities.h:151
LLU::WS::Function::Function
Function(const std::string &h)
Create a Function with given head but unknown number of arguments.
Definition: WSTP/Utilities.h:80
LLU::WS::Symbol::Symbol
Symbol(std::string h)
Constructs a Symbol given its head.
Definition: WSTP/Utilities.h:43
LLU::WS::List
Special type of a Function which corresponds to the List expression when exchanged with the Kernel vi...
Definition: WSTP/Utilities.h:122
LLU::WSStream::get
WSLINK & get() noexcept
Returns a reference to underlying low-level WSTP handle.
Definition: WSStream.hpp:76
LLU::WS::Symbol::getHead
const std::string & getHead() const
Get Symbol name/head.
Definition: Utilities.cpp:13