Red Hat Data Grid HotRod C++ Client  7.2.0
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 
18 template <class T> class BasicMarshaller : public infinispan::hotrod::Marshaller<T>
19 {
20  public:
21  void marshall(const T& s, std::vector<char>& b) {
22 #if __GNUG__ && __GNUC__ < 5
23  static_assert(std::is_fundamental<T>::value, "Type is not fundamental. A marshaller specialization is needed");
24 #else
25  static_assert(std::is_trivially_copyable<T>::value, "Type is not trivially_copyable. A marshaller specialization is needed");
26 #endif
27  b.resize(sizeof(s));
28  std::memcpy(b.data(), &s, sizeof(s));
29  }
30  T* unmarshall(const std::vector<char>& b) {
31 #if __GNUG__ && __GNUC__ < 5
32  static_assert(std::is_fundamental<T>::value, "Type is not trivially_copyable. A marshaller specialization is needed");
33 #else
34  static_assert(std::is_trivially_copyable<T>::value, "Type is not trivially_copyable. A marshaller specialization is needed");
35 #endif
36  T* s = new T();
37  std::memcpy(s, b.data(), sizeof(*s));
38  return s;
39  }
40 };
41 
42 // Specialization for std::string:
43 
44 template <>
45 class BasicMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> {
46  public:
47  void marshall(const std::string& s, std::vector<char>& b) {
48  b.assign(s.data(), s.data()+s.size());
49  }
50  std::string* unmarshall(const std::vector<char>& b) {
51  std::string* s = new std::string(b.data(), b.size());
52  return s;
53  }
54 };
55 
56 }} // namespace
57 
58 #endif /* ISPN_HOTROD_BASICMARSHALLER_H */
void marshall(const T &s, std::vector< char > &b)
Definition: BasicMarshaller.h:21
Definition: AuthenticationConfiguration.h:10
Definition: BasicMarshaller.h:18
T * unmarshall(const std::vector< char > &b)
Definition: BasicMarshaller.h:30
Definition: Marshaller.h:12