InfinispanHotRodC++Client  8.2.0.Alpha1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BasicMarshaller.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_BASICMARSHALLER_H
2 #define ISPN_HOTROD_BASICMARSHALLER_H
3 
4 
5 #include <string>
6 #include <cstring>
7 #include <iostream>
8 #include <type_traits>
10 
11 namespace infinispan {
12 namespace hotrod {
13 
14 /*
15  * A Marshaller for a few simple types.
16  */
17 
18 
19 template <class T> class BasicMarshaller : public infinispan::hotrod::Marshaller<T>
20 {
21  public:
22  void marshall(const T& s, std::vector<char>& b) {
23 #if __GNUG__ && __GNUC__ < 5
24  static_assert(std::is_fundamental<T>::value, "Type is not fundamental. A marshaller specialization is needed");
25 #else
26  static_assert(std::is_trivially_copyable<T>::value, "Type is not trivially_copyable. A marshaller specialization is needed");
27 #endif
28  b.resize(sizeof(s));
29  std::memcpy(b.data(), &s, sizeof(s));
30  }
31  T* unmarshall(const std::vector<char>& b) {
32 #if __GNUG__ && __GNUC__ < 5
33  static_assert(std::is_fundamental<T>::value, "Type is not trivially_copyable. A marshaller specialization is needed");
34 #else
35  static_assert(std::is_trivially_copyable<T>::value, "Type is not trivially_copyable. A marshaller specialization is needed");
36 #endif
37  T* s = new T();
38  std::memcpy(s, b.data(), sizeof(*s));
39  return s;
40  }
41 };
42 
44 public:
45  static void noRelease(std::vector<char>*) { /* nothing allocated, nothing to release */ }
46  static void release(std::vector<char> *buf) {
47  delete buf->data();
48  }
49 };
50 
51 
52 // Specialization for std::string:
53 
54 template <>
55 class BasicMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> {
56  public:
57  void marshall(const std::string& s, std::vector<char>& b) {
58  b.assign(s.data(), s.data()+s.size());
59  }
60  std::string* unmarshall(const std::vector<char>& b) {
61  std::string* s = new std::string(b.data(), b.size());
62  return s;
63  }
64 };
65 
66 }} // namespace
67 
68 #endif /* ISPN_HOTROD_BASICMARSHALLER_H */
static void release(std::vector< char > *buf)
Definition: BasicMarshaller.h:46
std::string * unmarshall(const std::vector< char > &b)
Definition: BasicMarshaller.h:60
void marshall(const T &s, std::vector< char > &b)
Definition: BasicMarshaller.h:22
void marshall(const std::string &s, std::vector< char > &b)
Definition: BasicMarshaller.h:57
Definition: BasicMarshaller.h:19
Definition: BasicMarshaller.h:43
static void noRelease(std::vector< char > *)
Definition: BasicMarshaller.h:45
T * unmarshall(const std::vector< char > &b)
Definition: BasicMarshaller.h:31
Definition: Marshaller.h:9