include/infinispan/hotrod/BasicMarshaller.h

Go to the documentation of this file.
00001 #ifndef ISPN_HOTROD_BASICMARSHALLER_H
00002 #define ISPN_HOTROD_BASICMARSHALLER_H
00003 
00004 
00005 #include <string>
00006 #include <iostream>
00007 #include "infinispan/hotrod/Marshaller.h"
00008 
00009 namespace infinispan {
00010 namespace hotrod {
00011 
00012 /*
00013  * A Marshaller for a few simple types.
00014  */
00015 
00016 
00017 template <class T> class BasicMarshaller : public infinispan::hotrod::Marshaller<T>
00018 {
00019 };
00020 
00021 class BasicMarshallerHelper {
00022   public:
00023     static void noRelease(ScopedBuffer*) { /* nothing allocated, nothing to release */ }
00024 };
00025 
00026 
00027 // Specialization for std::string:
00028 
00029 template <>
00030 class BasicMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> {
00031   public:
00032     void marshall(const std::string& s, ScopedBuffer& b) {
00033         b.set(const_cast<char *> (s.data()), s.size(), &BasicMarshallerHelper::noRelease);
00034     }
00035     std::string* unmarshall(const ScopedBuffer& b) {
00036         std::string* s = new std::string(b.getBytes(), b.getLength());
00037         return s;
00038     }
00039 };
00040 
00041 template <>
00042 class BasicMarshaller<int> : public infinispan::hotrod::Marshaller<int> {
00043   public:
00044     void marshall(const int& s, ScopedBuffer& b) {
00045           char *buf = new char[4];
00046           for (int i = 0 ; i < 4 ; i++) {
00047             buf[3-i] = (char) ((s) >> (8*i));
00048           }
00049         b.set(buf, 4);
00050     }
00051     int* unmarshall(const ScopedBuffer& b) {
00052         int result = 0;
00053           for (int i = 0; i < 4 ; i++) {
00054             result <<= 4;
00055             result ^= (int) *(b.getBytes()+i) & 0xFF;
00056           }
00057         int* s = new int(result);
00058         return s;
00059     }
00060 };
00061 
00062 }} // namespace
00063 
00064 #endif  /* ISPN_HOTROD_BASICMARSHALLER_H */

Generated on 26 Mar 2014 for InfinispanHotRodC++Client by  doxygen 1.4.7