Infinispan HotRod C++ Client  8.3.1.Final
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JBossMarshaller.h
Go to the documentation of this file.
1 /*
2  * JBossMarshaller.h
3  *
4  * Created on: Dec 4, 2017
5  * Author: rigazilla
6  */
7 
8 #ifndef INCLUDE_INFINISPAN_HOTROD_JBOSSMARSHALLER_H_
9 #define INCLUDE_INFINISPAN_HOTROD_JBOSSMARSHALLER_H_
10 
12 
13 namespace infinispan {
14 namespace hotrod {
15 
17 public:
18  template<typename R> static R unmarshall(const std::vector<char>& b);
19  template<typename A> static std::vector<char> marshall(A a);
20  virtual ~JBossMarshaller() = 0;
21 
22  static void marshallSmall(const std::string& s, std::vector<char>& b) {
23  b.resize(s.size() + 3);
24  char* buf = b.data();
25  // JBoss preamble
26  buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
27  buf[1] = JBasicMarshallerHelper::SMALL_STRING;
28  buf[2] = (char) s.size();
29  memcpy(buf + 3, s.data(), s.size());
30  }
31  static void marshallMedium(const std::string& s, std::vector<char>& b) {
32  b.resize(s.size() + 4);
33  char* buf = b.data();
34  // JBoss preamble
35  buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
36  buf[1] = JBasicMarshallerHelper::MEDIUM_STRING;
37  buf[2] = (char) (s.size() >> 8);
38  buf[3] = s.size() & 0xff;
39 
40  memcpy(buf + 4, s.data(), s.size());
41  }
42 
43 };
44 
45 template<> inline int* JBossMarshaller::unmarshall(const std::vector<char>& b) {
46  int result = 0;
47  for (int i = 0; i < 4; i++) {
48  result <<= 8;
49  result ^= (int) *(b.data() + i + 2) & 0xFF;
50  }
51  int* s = new int(result);
52  return s;
53 }
54 
55 template<> inline std::string* JBossMarshaller::unmarshall(const std::vector<char>& b) {
56  // TODO: this works only for SMALL_STRING
57  std::string* s = new std::string(b.begin() + 3, b.end());
58  return s;
59 }
60 
61 template<> inline std::vector<char> JBossMarshaller::marshall(std::string s) {
62  std::vector<char> b;
63  if (s.size() <= 0x100) {
65  } else {
67  }
68  return b;
69 }
70 
71 template<> inline std::vector<char> JBossMarshaller::marshall(int a) {
72  char buf[6];
73  std::vector<char> res;
74  // JBoss preamble
75  buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
76  buf[1] = JBasicMarshallerHelper::INTEGER;
77  for (int i = 0; i < 4; i++) {
78  buf[5 - i] = (char) ((a) >> (8 * i));
79  }
80  res.assign(buf, buf + 6);
81  return res;
82 }
83 
84 }
85 }
86 
87 #endif /* INCLUDE_INFINISPAN_HOTROD_JBOSSMARSHALLER_H_ */
static std::vector< char > marshall(A a)
Definition: JBossMarshaller.h:16
static void marshallMedium(const std::string &s, std::vector< char > &b)
Definition: JBossMarshaller.h:31
static void marshallSmall(const std::string &s, std::vector< char > &b)
Definition: JBossMarshaller.h:22
static R unmarshall(const std::vector< char > &b)