RInside Version 0.2.12
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rinsideserver.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Christian Authmann
3  */
4 
5 #pragma once
6 
7 #include "common/typeid.h"
8 #include "common/binarystream.h"
9 #include "common/constants.h"
10 #include "rinside_callbacks.h"
11 
12 #include <stdexcept>
13 #include <map>
14 #include <functional>
15 
16 
18 
20  public:
23 
24  void run();
25 
26  private:
27  SEXP sexp_from_stream();
28  void sexp_to_stream(SEXP, int32_t type, bool include_reply = false);
29 
33 
35  void sendReply(char reply) { if (!can_send_reply) throw std::runtime_error("Cannot send a reply at this time, exiting"); can_send_reply = false; stream.write(reply); }
36  void allowSendReply() { if (can_send_reply) throw std::runtime_error("allowSendReply() called twice, exiting"); can_send_reply = true; }
37 
38  static std::map<int32_t, std::function<SEXP(BinaryStream &)> > registry_sexp_from_stream;
39  static std::map<int32_t, std::function<void(RInsideServer &, SEXP, bool)> > registry_sexp_to_stream;
40 
41  public:
42  static void registerDefaultTypes();
43  template <typename T>
44  static void registerType() {
45  int32_t type = TYPEID<T>();
46 
47  if (registry_sexp_from_stream.count(type) > 0 || registry_sexp_to_stream.count(type) > 0)
48  throw std::runtime_error("registerType(): type already registered");
49 
50  registry_sexp_from_stream[type] = [] (BinaryStream &stream) -> SEXP {
51  T value = stream.read<T>();
52  return Rcpp::wrap<T>(value);
53  };
54 
55  registry_sexp_to_stream[type] = [type] (RInsideServer &server, SEXP sexp, bool include_reply) -> void {
56  T value = Rcpp::as<T>(sexp);
57  /*
58  * The reply should be sent after type conversion. If type conversion throws an exception,
59  * the server cannot reply with REPLY_ERROR after another reply has been sent.
60  */
61  if (include_reply)
62  server.sendReply(RIS_REPLY_VALUE);
63  server.stream.write(type);
64  server.stream.write(value);
65  };
66  }
67 
69 };
BinaryStream stream
Definition: rinsideserver.h:30
RInsideCallbacks & Rcallbacks
Definition: rinsideserver.h:32
RInsideServer(BinaryStream &stream, RInside &R, RInsideCallbacks &Rcallbacks)
SEXP sexp_from_stream()
void allowSendReply()
Definition: rinsideserver.h:36
void write(const char *buffer, size_t len)
static void registerDefaultTypes()
void sexp_to_stream(SEXP, int32_t type, bool include_reply=false)
static std::map< int32_t, std::function< SEXP(BinaryStream &)> > registry_sexp_from_stream
Definition: rinsideserver.h:38
const char RIS_REPLY_VALUE
Definition: constants.h:23
static void registerType()
Definition: rinsideserver.h:44
static std::map< int32_t, std::function< void(RInsideServer &, SEXP, bool)> > registry_sexp_to_stream
Definition: rinsideserver.h:39
void sendReply(char reply)
Definition: rinsideserver.h:35
size_t read(char *buffer, size_t len)
RInside & R
Definition: rinsideserver.h:31