RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
callback_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Christian Authmann
3  */
4 
5 
6 namespace callback_helper {
7  // A recursive template sending the TYPEIDs of all template parameters over the stream
8  template <typename... Params>
9  struct send_pack;
10 
11  template <typename First, typename... Remaining>
12  struct send_pack<First, Remaining...> {
13  static void send(BinaryStream &stream) {
14  auto type = TYPEID<First>();
15  stream.write(type);
17  }
18  };
19 
20  template <>
21  struct send_pack<> {
22  static void send(BinaryStream &stream) {
23  }
24  };
25 
26  // An exception when the wrong type is sent over the stream
27  class type_mismatch_exception : std::exception {
28  };
29 
30  // An exception when the server failed to transform a parameter, but can still continue
31  class parameter_error_exception : public std::runtime_error {
32  public:
33  explicit parameter_error_exception(const std::string &error) : std::runtime_error(error) {};
34  };
35 
36  // read a typeid from the stream, compare it to the expected type, then read the value
37  template<typename T>
39  auto result = stream.read<char>();
40  if (result == RIS_REPLY_ERROR) {
41  auto error = stream.read<std::string>();
42  throw parameter_error_exception(error);
43  }
44  else if (result != RIS_REPLY_VALUE) {
45  throw std::runtime_error("Invalid reply from server");
46  }
47  auto type = stream.read<int32_t>();
48  if (type != TYPEID<T>()) {
49  printf("trying to read type %d, got type %d\n", (int) TYPEID<T>(), (int) type);
51  }
52  return stream.read<T>();
53  }
54 
55  // auto-generated functions for calling callbacks
56  template<typename RESULT_TYPE>
57  void call(const std::function<RESULT_TYPE()> &fun, BinaryStream &stream) {
58  RESULT_TYPE result = fun();
59  int32_t result_type = TYPEID<RESULT_TYPE>();
60  stream.write(result_type);
61  stream.write(result);
62  }
63  template<typename RESULT_TYPE, typename U0>
64  void call(const std::function<RESULT_TYPE(U0)> &fun, BinaryStream &stream) {
65  auto x0 = read_from_stream_with_typeid<typename std::decay<U0>::type>(stream);
66 
67  RESULT_TYPE result = fun(x0);
68  int32_t result_type = TYPEID<RESULT_TYPE>();
69  stream.write(result_type);
70  stream.write(result);
71  }
72  template<typename RESULT_TYPE, typename U0, typename U1>
73  void call(const std::function<RESULT_TYPE(U0, U1)> &fun, BinaryStream &stream) {
74  auto x0 = read_from_stream_with_typeid<typename std::decay<U0>::type>(stream);
75  auto x1 = read_from_stream_with_typeid<typename std::decay<U1>::type>(stream);
76 
77  RESULT_TYPE result = fun(x0, x1);
78  int32_t result_type = TYPEID<RESULT_TYPE>();
79  stream.write(result_type);
80  stream.write(result);
81  }
82  template<typename RESULT_TYPE, typename U0, typename U1, typename U2>
83  void call(const std::function<RESULT_TYPE(U0, U1, U2)> &fun, BinaryStream &stream) {
84  auto x0 = read_from_stream_with_typeid<typename std::decay<U0>::type>(stream);
85  auto x1 = read_from_stream_with_typeid<typename std::decay<U1>::type>(stream);
86  auto x2 = read_from_stream_with_typeid<typename std::decay<U2>::type>(stream);
87 
88  RESULT_TYPE result = fun(x0, x1, x2);
89  int32_t result_type = TYPEID<RESULT_TYPE>();
90  stream.write(result_type);
91  stream.write(result);
92  }
93  // TODO: more parameters
94 }
void call(const std::function< RESULT_TYPE()> &fun, BinaryStream &stream)
const char RIS_REPLY_ERROR
Definition: constants.h:24
void write(const char *buffer, size_t len)
static void send(BinaryStream &stream)
const char RIS_REPLY_VALUE
Definition: constants.h:23
static void send(BinaryStream &stream)
size_t read(char *buffer, size_t len)
T read_from_stream_with_typeid(BinaryStream &stream)
parameter_error_exception(const std::string &error)