GridGain C++ Client Library
portable_type.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) GridGain Systems. All Rights Reserved.
3  * _________ _____ __________________ _____
4  * __ ____/___________(_)______ /__ ____/______ ____(_)_______
5  * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \
6  * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / /
7  * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/
8  */
9 
16 #ifndef _GRIDGAIN_PORTABLE_TYPE
17 #define _GRIDGAIN_PORTABLE_TYPE
18 
19 #include <stdint.h>
20 
21 #include <ignite/common/common.h>
22 
23 #include "gridgain/grid_error.h"
24 
29 #define GG_PORTABLE_TYPE_START(T) \
30 template<> \
31 struct PortableType<T> \
32 {
33 
38 #define GG_PORTABLE_TYPE_END \
39 };
40 
45 #define GG_PORTABLE_GET_TYPE_ID_AS_CONST(id) \
46 int32_t GetTypeId() \
47 { \
48  return id; \
49 }
50 
55 #define GG_PORTABLE_GET_TYPE_ID_AS_HASH(typeName) \
56 int32_t GetTypeId() \
57 { \
58  return GetPortableStringHashCode(#typeName); \
59 }
60 
65 #define GG_PORTABLE_GET_TYPE_NAME_AS_IS(typeName) \
66 std::string GetTypeName() \
67 { \
68  return #typeName; \
69 }
70 
75 #define GG_PORTABLE_GET_FIELD_ID_AS_HASH \
76 int32_t GetFieldId(const char* name) \
77 { \
78  return GetPortableStringHashCode(name); \
79 }
80 
85 #define GG_PORTABLE_GET_HASH_CODE_ZERO(T) \
86 int32_t GetHashCode(const T& obj) \
87 { \
88  return 0; \
89 }
90 
95 #define GG_PORTABLE_IS_NULL_FALSE(T) \
96 bool IsNull(const T& obj) \
97 { \
98  return false; \
99 }
100 
105 #define GG_PORTABLE_IS_NULL_IF_NULLPTR(T) \
106 bool IsNull(const T& obj) \
107 { \
108  return obj; \
109 }
110 
115 #define GG_PORTABLE_GET_NULL_DEFAULT_CTOR(T) \
116 T GetNull() \
117 { \
118  return T(); \
119 }
120 
125 #define GG_PORTABLE_GET_NULL_NULLPTR(T) \
126 T GetNull() \
127 { \
128  return NULL; \
129 }
130 
131 namespace gridgain
132 {
133  namespace portable
134  {
135  class PortableWriter;
136  class PortableReader;
137 
144  IGNITE_IMPORT_EXPORT int32_t GetPortableStringHashCode(const char* val);
145 
149  template<typename T>
150  struct IGNITE_IMPORT_EXPORT PortableType
151  {
157  int32_t GetTypeId()
158  {
159  GG_ERROR_1(GridError::GG_ERR_PORTABLE, "GetTypeId function is not defined for portable type.");
160  }
161 
167  std::string GetTypeName()
168  {
169  GG_ERROR_1(GridError::GG_ERR_PORTABLE, "GetTypeName function is not defined for portable type.");
170  }
171 
178  int32_t GetFieldId(const char* name)
179  {
180  return GetPortableStringHashCode(name);
181  }
182 
189  int32_t GetHashCode(const T& obj)
190  {
191  return 0;
192  }
193 
200  void Write(PortableWriter& writer, const T& obj)
201  {
202  GG_ERROR_1(GridError::GG_ERR_PORTABLE, "Write function is not defined for portable type.");
203  }
204 
211  T Read(PortableReader& reader)
212  {
213  GG_ERROR_1(GridError::GG_ERR_PORTABLE, "Read function is not defined for portable type.");
214  }
215 
222  bool IsNull(const T& obj)
223  {
224  return false;
225  }
226 
233  {
234  GG_ERROR_1(GridError::GG_ERR_PORTABLE, "GetNull function is not defined for portable type.");
235  }
236  };
237 
241  template <typename T>
242  struct IGNITE_IMPORT_EXPORT PortableType<T*>
243  {
246 
251  {
252  typ = PortableType<T>();
253  }
254 
255  int32_t GetTypeId()
256  {
257  return typ.GetTypeId();
258  }
259 
260  std::string GetTypeName()
261  {
262  return typ.GetTypeName();
263  }
264 
265  int32_t GetFieldId(const char* name)
266  {
267  return typ.GetFieldId(name);
268  }
269 
270  int32_t GetHashCode(T* const& obj)
271  {
272  return typ.GetHashCode(*obj);
273  }
274 
275  void Write(PortableWriter& writer, T* const& obj)
276  {
277  typ.Write(writer, *obj);
278  }
279 
280  T* Read(PortableReader& reader)
281  {
282  T* res = new T();
283 
284  *res = typ.Read(reader);
285 
286  return res;
287  }
288 
289  bool IsNull(T* const& obj)
290  {
291  return !obj || typ.IsNull(*obj);
292  }
293 
294  T* GetNull()
295  {
296  return NULL;
297  }
298  };
299  }
300 }
301 
302 #endif
int32_t GetHashCode(const T &obj)
Get portable object hash code.
Definition: portable_type.h:189
int32_t GetTypeId()
Get portable object type ID.
Definition: portable_type.h:157
Portable writer.
Definition: portable_writer.h:32
static const int GG_ERR_PORTABLE
Portable error.
Definition: grid_error.h:101
int32_t GetFieldId(const char *name)
Get portable object field ID.
Definition: portable_type.h:178
PortableType()
Constructor.
Definition: portable_type.h:250
Declares gridgain::GridError class.
IGNITE_IMPORT_EXPORT int32_t GetPortableStringHashCode(const char *val)
Get portable string hash code.
Definition: portable_type.cpp:16
T GetNull()
Get NULL value for the given portable type.
Definition: portable_type.h:232
void Write(PortableWriter &writer, const T &obj)
Write portable object.
Definition: portable_type.h:200
T Read(PortableReader &reader)
Read portable object.
Definition: portable_type.h:211
PortableType< T > typ
Actual type.
Definition: portable_type.h:245
bool IsNull(const T &obj)
Check whether passed portable object should be interpreted as NULL.
Definition: portable_type.h:222
std::string GetTypeName()
Get portable object type name.
Definition: portable_type.h:167
Portable type structure.
Definition: portable_type.h:150
GridGain API.
Definition: cache.h:35
Portable reader.
Definition: portable_reader.h:33