00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef GRID_CLIENT_SIMPLE_TYPE_HASHEABLE_HPP_INCLUDED
00011 #define GRID_CLIENT_SIMPLE_TYPE_HASHEABLE_HPP_INCLUDED
00012
00013 #include <memory>
00014
00015 #include "gridgain/gridhasheableobject.hpp"
00016 #include "gridgain/impl/utils/gridclientbyteutils.hpp"
00017
00018 typedef std::shared_ptr<GridClientHasheableObject> TGridHasheableObjectPtr;
00019
00023 template<class T> class GridSimpleTypeHasheable: public GridClientHasheableObject {
00024 protected:
00030 GridSimpleTypeHasheable(const T& pVal) :
00031 val(pVal) {}
00032
00033 public:
00039 virtual void convertToBytes(std::vector < int8_t >& bytes) const {
00040 bytes.resize(sizeof(val));
00041 memset(&bytes[0], 0, sizeof(val));
00042
00043 GridClientByteUtils::valueToBytes(val, &bytes[0], sizeof(val), GridClientByteUtils::LITTLE_ENDIAN_ORDER);
00044 }
00045
00053 static T get(const int8_t* buffer, size_t nBytes) {
00054 T res;
00055
00056 GridClientByteUtils::bytesToValue(buffer, nBytes, res, GridClientByteUtils::LITTLE_ENDIAN_ORDER);
00057
00058 return res;
00059 }
00060
00066 virtual bool operator<(const GridSimpleTypeHasheable<T>& right) const {
00067 return val < const_cast<GridSimpleTypeHasheable<T>&>(right).val;
00068 }
00069
00076 friend std::ostream& operator<< (std::ostream &out, const GridSimpleTypeHasheable<T> &o) {
00077 return out << o.val;
00078 }
00079 protected:
00080 T val;
00081 };
00082
00086 class GridInt64Hasheable: public GridSimpleTypeHasheable<int64_t> {
00087 public:
00091 GridInt64Hasheable(int64_t val): GridSimpleTypeHasheable<int64_t>(val) {}
00092
00098 virtual int32_t hashCode() const {
00099 return (int32_t)(val ^ (val >> 32));
00100 }
00101 };
00102
00108 inline TGridHasheableObjectPtr createHasheable(int64_t val) {
00109 return TGridHasheableObjectPtr(new GridInt64Hasheable(val));
00110 }
00111
00115 class GridInt32Hasheable: public GridSimpleTypeHasheable<int32_t> {
00116 public:
00120 GridInt32Hasheable(int32_t val): GridSimpleTypeHasheable<int32_t>(val) {}
00121
00127 virtual int32_t hashCode() const {
00128 return val;
00129 }
00130 };
00131
00137 inline TGridHasheableObjectPtr createHasheable(int32_t val) {
00138 return TGridHasheableObjectPtr(new GridInt32Hasheable(val));
00139 }
00140
00141
00142 #endif