Infinispan HotRod C++ Client 9.2.0.Final
Loading...
Searching...
No Matches
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 <cstring>
7#include <iostream>
10
11namespace infinispan {
12namespace hotrod {
13
18template <class T> class JBasicMarshaller : public infinispan::hotrod::Marshaller<T> {
19};
20
21class JBasicMarshallerHelper {
22public:
23 // Type managed: SMALL_STRING, INTEGER
24 enum {MARSHALL_VERSION = 0x03, SMALL_STRING = 0x3e, MEDIUM_STRING = 0x3f, INTEGER=0x4b};
25 static void noRelease(std::vector<char>*) { /* nothing allocated, nothing to release */ }
26 static void release(std::vector<char> *buf) {
27 delete buf->data();
28 }
29 template <class T> static T unmarshall(char *);
30};
31
32 template <> inline std::string JBasicMarshallerHelper::unmarshall(char *b) {
33 if (b[0]!=JBasicMarshallerHelper::MARSHALL_VERSION)
34 throw Exception("JBasicMarshallerHelper: bad version");
35 if ( (b[1]!=JBasicMarshallerHelper::SMALL_STRING) && (b[1]!=JBasicMarshallerHelper::MEDIUM_STRING) )
36 throw Exception("JBasicMarshallerHelper: not a string");
37 return std::string(b+3,b[2]);
38 }
39
40 template <> inline int JBasicMarshallerHelper::unmarshall(char *b) {
41 if (b[0]!=JBasicMarshallerHelper::MARSHALL_VERSION)
42 throw Exception("JBasicMarshallerHelper: bad version");
43 if (b[1]!=JBasicMarshallerHelper::INTEGER)
44 throw Exception("JBasicMarshallerHelper: not a integer");
45 int result = 0;
46 for (int i = 0; i < 4 ; i++) {
47 result <<= 8;
48 result ^= (int) *(b+i+2) & 0xFF;
49 }
50 return result;
51 }
52
53
54
55// Specialization for std::string:
56
57template <>
58class JBasicMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> {
59 public:
60 void marshall(const std::string& s, std::vector<char>& b) {
61 if (s.size() <= 0x100) {
62 marshallSmall(s, b);
63 }
64 else
65 marshallMedium(s, b);
66 }
67
68 std::string* unmarshall(const std::vector<char>& b) {
69 std::string* s = new std::string(b.data()+3, b.size()-3);
70 return s;
71 }
72
73static std::string addPreamble(std::string &s) {
74 std::string res;
75 if (s.size()<0x100)
76 {
77 res = addPreambleSmall(s);
78 }
79 else
80 {
81 res = addPreambleMedium(s);
82 }
83 return res;
84 }
85
86private:
87 static std::string addPreambleSmall(std::string& s) {
88 std::string res("\x03\x3e");
89 res.append(1, (char)s.size());
90 res.append(s);
91 return res;
92 }
93 static std::string addPreambleMedium(std::string& s) {
94 std::string res("\x03\x3f");
95 res.append(1, (char)(s.size()>>8));
96 res.append(1, (char)(s.size()&& 0xff));
97 res.append(s);
98 return res;
99 }
100
101 void marshallSmall(const std::string& s, std::vector<char>& b) {
102 b.resize(s.size() + 3);
103 char* buf = b.data();
104 // JBoss preamble
105 buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
106 buf[1] = JBasicMarshallerHelper::SMALL_STRING;
107 buf[2] = (char)s.size();
108 memcpy(buf + 3, s.data(), s.size());
109 }
110
111 void marshallMedium(const std::string& s, std::vector<char>& b) {
112 b.resize(s.size() + 4);
113 char* buf = b.data();
114 // JBoss preamble
115 buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
116 buf[1] = JBasicMarshallerHelper::MEDIUM_STRING;
117 buf[2] = (char)(s.size() >> 8);
118 buf[3] = s.size() & 0xff;
119
120 memcpy(buf + 4, s.data(), s.size());
121 }
122};
123
124template <>
125class JBasicMarshaller<int> : public infinispan::hotrod::Marshaller<int> {
126 public:
127 void marshall(const int& s, std::vector<char>& b) {
128 char buf[6];
129 // JBoss preamble
130 buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
131 buf[1] = JBasicMarshallerHelper::INTEGER;
132 for (int i = 0 ; i < 4 ; i++) {
133 buf[5-i] = (char) ((s) >> (8*i));
134 }
135 b.assign(buf, buf+6);
136 }
137 int* unmarshall(const std::vector<char>& b) {
138 int result = 0;
139 for (int i = 0; i < 4 ; i++) {
140 result <<= 8;
141 result ^= (int) *(b.data()+i+2) & 0xFF;
142 }
143 int* s = new int(result);
144 return s;
145 }
146};
147}} // namespace
148
149#endif /* ISPN_HOTROD_JBasicMarshaller_H */
Definition: JBasicMarshaller.h:18
Definition: Marshaller.h:13
virtual void marshall(const T &obj, std::vector< char > &buff)=0
virtual T * unmarshall(const std::vector< char > &buff)=0
Definition: AuthenticationConfiguration.h:10