RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rinsideclient.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Christian Authmann
3  */
4 
5 #pragma once
6 
7 #include "common/binarystream.h"
8 #include "common/constants.h"
9 #include "common/typeid.h"
10 
11 #include <stdexcept>
12 #include <cstdint>
13 #include <functional>
14 #include <map>
15 
16 
17 #include "callback_helper.h"
18 
20  public:
21 
24 
25  void parseEvalQ(const std::string &code) {
26  runScript(code, 0);
27  can_send_command = true;
28  };
29 
30  template <typename T> T parseEval(const std::string &code) {
31  runScript(code, TYPEID<T>());
32  auto result = stream.read<T>();
33  can_send_command = true;
34  return result;
35  };
36 
37  template<typename T>
38  void setValue(const std::string &name, const T &value) {
40  stream.write(name);
41  stream.write(TYPEID<T>());
42  stream.write(value);
43 
44  readReply(true, false);
45  can_send_command = true;
46  };
47 
48  template<typename T>
49  T getValue(const std::string &name) {
51  stream.write(name);
52  stream.write(TYPEID<T>());
53 
54  readReply(false, true);
55 
56  auto type = stream.read<int32_t>();
57  if (type != TYPEID<T>())
58  unrecoverable_error("getValue() returned wrong type");
59  auto result = stream.read<T>();
60  can_send_command = true;
61  return result;
62  };
63 
64  template<typename R, typename... Params>
65  void setCallback(const std::string &name, std::function<R(Params...)> &callback) {
66  uint32_t callback_id = next_callback_id++;
67  int32_t result_type = TYPEID<R>();
68  size_t paramcount = sizeof...(Params);
69 
71  stream.write(name);
72  stream.write(callback_id);
73  stream.write(result_type);
74  stream.write(paramcount);
75  // now write the types of the parameters
77 
78  // remember the callback
79  std::function<void(void)> func = std::bind(callback_helper::call<R, Params...>, std::ref(callback), std::ref(stream));
80  callbacks[callback_id] = func;
81  readReply(true, false);
82  can_send_command = true;
83  }
84 
85  std::string getConsoleOutput();
86 
87  void initPlot(uint32_t width=800, uint32_t height=600);
88  std::string getPlot();
89  private:
90  void runScript(const std::string code, int32_t result_typeid);
91  void writeCommand(char command);
92  char readReply(bool accept_ok = true, bool accept_value = false);
93 
94  void unrecoverable_error(const std::string &error);
95 
97  uint32_t next_callback_id;
98  std::map<uint32_t, std::function<void(void)> > callbacks;
101 };
RInsideClient(BinaryStream &stream)
bool had_unrecoverable_error
Definition: rinsideclient.h:99
void unrecoverable_error(const std::string &error)
void setCallback(const std::string &name, std::function< R(Params...)> &callback)
Definition: rinsideclient.h:65
char readReply(bool accept_ok=true, bool accept_value=false)
uint32_t next_callback_id
Definition: rinsideclient.h:97
void setValue(const std::string &name, const T &value)
Definition: rinsideclient.h:38
void write(const char *buffer, size_t len)
T parseEval(const std::string &code)
Definition: rinsideclient.h:30
std::string getPlot()
const char RIS_CMD_SETVALUE
Definition: constants.h:11
BinaryStream stream
Definition: rinsideclient.h:96
T getValue(const std::string &name)
Definition: rinsideclient.h:49
void initPlot(uint32_t width=800, uint32_t height=600)
std::map< uint32_t, std::function< void(void)> > callbacks
Definition: rinsideclient.h:98
void writeCommand(char command)
const char RIS_CMD_GETVALUE
Definition: constants.h:12
std::string getConsoleOutput()
void runScript(const std::string code, int32_t result_typeid)
size_t read(char *buffer, size_t len)
void parseEvalQ(const std::string &code)
Definition: rinsideclient.h:25
const char RIS_CMD_SETCALLBACK
Definition: constants.h:13