InfinispanHotRodC++Client  8.2.0.Alpha1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JBasicMarshaller.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_JBASICMARSHALLER_H
2 #define ISPN_HOTROD_JBASICMARSHALLER_H
3 
4 
5 #include <string>
6 #include <iostream>
9 
10 namespace infinispan {
11 namespace hotrod {
12 
13 /*
14  * A Marshaller for a few simple types that pretends to be compatible with JBoss Marshaller.
15  * See below the Helper class for a list of the managed types.
16  */
17 template <class T> class JBasicMarshaller : public infinispan::hotrod::Marshaller<T> {
18 };
19 
21 public:
22  // Type managed: SMALL_STRING, INTEGER
23  enum {MARSHALL_VERSION = 0x03, SMALL_STRING = 0x3e, MEDIUM_STRING = 0x3f, INTEGER=0x4b};
24  static void noRelease(std::vector<char>*) { /* nothing allocated, nothing to release */ }
25  static void release(std::vector<char> *buf) {
26  delete buf->data();
27  }
28  template <class T> static T unmarshall(char *);
29 };
30 
31  template <> std::string JBasicMarshallerHelper::unmarshall(char *b) {
33  throw Exception("JBasicMarshallerHelper: bad version");
35  throw Exception("JBasicMarshallerHelper: not a string");
36  return std::string(b+3,b[2]);
37  }
38 
39  template <> int JBasicMarshallerHelper::unmarshall(char *b) {
41  throw Exception("JBasicMarshallerHelper: bad version");
43  throw Exception("JBasicMarshallerHelper: not a integer");
44  int result = 0;
45  for (int i = 0; i < 4 ; i++) {
46  result <<= 8;
47  result ^= (int) *(b+i+2) & 0xFF;
48  }
49  return result;
50  }
51 
52 
53 
54 // Specialization for std::string:
55 
56 template <>
57 class JBasicMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> {
58  public:
59  void marshall(const std::string& s, std::vector<char>& b) {
60  if (s.size() <= 0x100) {
61  marshallSmall(s, b);
62  }
63  else
64  marshallMedium(s, b);
65  }
66 
67  std::string* unmarshall(const std::vector<char>& b) {
68  std::string* s = new std::string(b.data()+3, b.size()-3);
69  return s;
70  }
71 
72 static std::string addPreamble(std::string &s) {
73  std::string res;
74  if (s.size()<0x100)
75  {
76  res = addPreambleSmall(s);
77  }
78  else
79  {
80  res = addPreambleMedium(s);
81  }
82  return res;
83  }
84 
85 private:
86  static std::string addPreambleSmall(std::string& s) {
87  std::string res("\x03\x3e");
88  res.append(1, (char)s.size());
89  res.append(s);
90  return res;
91  }
92  static std::string addPreambleMedium(std::string& s) {
93  std::string res("\x03\x3f");
94  res.append(1, (char)(s.size()>>8));
95  res.append(1, (char)(s.size()&& 0xff));
96  res.append(s);
97  return res;
98  }
99 
100  void marshallSmall(const std::string& s, std::vector<char>& b) {
101  b.resize(s.size() + 3);
102  char* buf = b.data();
103  // JBoss preamble
106  buf[2] = (char)s.size();
107  memcpy(buf + 3, s.data(), s.size());
108  }
109 
110  void marshallMedium(const std::string& s, std::vector<char>& b) {
111  b.resize(s.size() + 4);
112  char* buf = b.data();
113  // JBoss preamble
116  buf[2] = (char)(s.size() >> 8);
117  buf[3] = s.size() & 0xff;
118 
119  memcpy(buf + 4, s.data(), s.size());
120  }
121 };
122 
123 template <>
125  public:
126  void marshall(const int& s, std::vector<char>& b) {
127  char buf[6];
128  // JBoss preamble
131  for (int i = 0 ; i < 4 ; i++) {
132  buf[5-i] = (char) ((s) >> (8*i));
133  }
134  b.assign(buf, buf+6);
135  }
136  int* unmarshall(const std::vector<char>& b) {
137  int result = 0;
138  for (int i = 0; i < 4 ; i++) {
139  result <<= 8;
140  result ^= (int) *(b.data()+i+2) & 0xFF;
141  }
142  int* s = new int(result);
143  return s;
144  }
145 };
146 }} // namespace
147 
148 #endif /* ISPN_HOTROD_JBasicMarshaller_H */
void marshall(const std::string &s, std::vector< char > &b)
Definition: JBasicMarshaller.h:59
Definition: JBasicMarshaller.h:17
void marshall(const int &s, std::vector< char > &b)
Definition: JBasicMarshaller.h:126
static void noRelease(std::vector< char > *)
Definition: JBasicMarshaller.h:24
static std::string addPreamble(std::string &s)
Definition: JBasicMarshaller.h:72
Definition: JBasicMarshaller.h:20
static void release(std::vector< char > *buf)
Definition: JBasicMarshaller.h:25
std::string * unmarshall(const std::vector< char > &b)
Definition: JBasicMarshaller.h:67
Definition: exceptions.h:13
int * unmarshall(const std::vector< char > &b)
Definition: JBasicMarshaller.h:136
Definition: Marshaller.h:9