Red Hat Data Grid HotRod C++ Client  7.2.0
BasicTypesProtoStreamMarshaller.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_BASICTYPESPROTOSTREAMMARSHALLER_H
2 #define ISPN_HOTROD_BASICTYPESPROTOSTREAMMARSHALLER_H
3 
4 
5 #include <string>
6 #include <iostream>
9 #if _MSC_VER
10 #pragma warning(push)
11 #pragma warning(disable:4267 4244)
12 #endif
13 #include "infinispan/hotrod/base_types.pb.h"
14 #if _MSC_VER
15 #pragma warning(pop)
16 #endif
17 namespace infinispan {
18 namespace hotrod {
19 
20 /*
21  * A Marshaller for a few simple types that pretends to be compatible with JBoss Marshaller.
22  * See below the Helper class for a list of the managed types.
23  */
24 template <class T> class BasicTypesProtoStreamMarshaller : public infinispan::hotrod::Marshaller<T> {
25 };
26 
27 class BasicTypesProtoStreamMarshallerHelper {
28 public:
29  static void noRelease(std::vector<char>*) { /* nothing allocated, nothing to release */ }
30  static void release(std::vector<char> *buf) {
31  delete buf->data();
32  }
33  template <class T> static T unmarshall(char *, size_t size);
34 };
35 
36  template <> inline std::string BasicTypesProtoStreamMarshallerHelper::unmarshall(char *b, size_t size) {
37  protobuf::base_types bt;
38  bt.ParseFromArray(b,size);
39  return bt.str();
40  }
41 
42  template <> inline int BasicTypesProtoStreamMarshallerHelper::unmarshall(char *b, size_t size) {
43  protobuf::base_types bt;
44  bt.ParseFromArray(b,size);
45  return bt.i32();
46  }
47 
48 
49 
50 // Specialization for std::string:
51 
52 template <>
53 class BasicTypesProtoStreamMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> {
54  public:
55  void marshall(const std::string& s, std::vector<char>& b) {
56  protobuf::base_types bt;
57  bt.set_str(s);
58  b.resize(bt.ByteSize());
59  bt.SerializePartialToArray(b.data(),bt.ByteSize());
60  }
61 
62  std::string* unmarshall(const std::vector<char>& b) {
63  protobuf::base_types bt;
64  bt.ParseFromArray(b.data(),b.size());
65  return new std::string(bt.str());
66  }
67 
68 
69 
70 };
71 
72 template <>
73 class BasicTypesProtoStreamMarshaller<int> : public infinispan::hotrod::Marshaller<int> {
74  public:
75  void marshall(const int& s, std::vector<char>& b) {
76  protobuf::base_types bt;
77  bt.set_i32(s);
78  b.resize(bt.ByteSize());
79  bt.SerializePartialToArray(b.data(),bt.ByteSize());
80  }
81 
82  int* unmarshall(const std::vector<char>& b) {
83  protobuf::base_types bt;
84  bt.ParseFromArray(b.data(),b.size());
85  return new int(bt.i32());
86  }
87 };
88 }} // namespace
89 
90 #endif /* ISPN_HOTROD_BasicTypesProtoStreamMarshaller_H */
Definition: AuthenticationConfiguration.h:10
Definition: Marshaller.h:12