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 class BasicMarshallerHelper { 00021 public: 00022 static void noRelease(ScopedBuffer*) { /* nothing allocated, nothing to release */ } 00023 static void release(ScopedBuffer *buf) { 00024 delete buf->getBytes(); 00025 } 00026 }; 00027 00028 00029 // Specialization for std::string: 00030 00031 template <> 00032 class BasicMarshaller<std::string> : public infinispan::hotrod::Marshaller<std::string> { 00033 public: 00034 void marshall(const std::string& s, ScopedBuffer& b) { 00035 b.set(const_cast<char *> (s.data()), s.size(), &BasicMarshallerHelper::noRelease); 00036 } 00037 std::string* unmarshall(const ScopedBuffer& b) { 00038 std::string* s = new std::string(b.getBytes(), b.getLength()); 00039 return s; 00040 } 00041 }; 00042 00043 template <> 00044 class BasicMarshaller<int> : public infinispan::hotrod::Marshaller<int> { 00045 public: 00046 void marshall(const int& s, ScopedBuffer& b) { 00047 char *buf = new char[4]; 00048 for (int i = 0 ; i < 4 ; i++) { 00049 buf[3-i] = (char) ((s) >> (8*i)); 00050 } 00051 b.set(buf, 4, &BasicMarshallerHelper::release); 00052 } 00053 int* unmarshall(const ScopedBuffer& b) { 00054 int result = 0; 00055 for (int i = 0; i < 4 ; i++) { 00056 result <<= 4; 00057 result ^= (int) *(b.getBytes()+i) & 0xFF; 00058 } 00059 int* s = new int(result); 00060 return s; 00061 } 00062 }; 00063 00064 }} // namespace 00065 00066 #endif /* ISPN_HOTROD_BASICMARSHALLER_H */