GridGain C++
binary_type.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 GridGain Systems, Inc. and Contributors.
3  *
4  * Licensed under the GridGain Community Edition License (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
23 #ifndef _IGNITE_BINARY_BINARY_TYPE
24 #define _IGNITE_BINARY_BINARY_TYPE
25 
26 #include <stdint.h>
27 
28 #include <ignite/common/common.h>
29 
30 #include <ignite/impl/binary/binary_type_impl.h>
31 
36 #define IGNITE_BINARY_TYPE_START(T) \
37 template<> \
38 struct BinaryType<T> \
39 {
40 
45 #define IGNITE_BINARY_TYPE_END \
46 };
47 
52 #define IGNITE_BINARY_GET_TYPE_ID_AS_CONST(id) \
53 static int32_t GetTypeId() \
54 { \
55  return id; \
56 }
57 
62 #define IGNITE_BINARY_GET_TYPE_ID_AS_HASH(typeName) \
63 static int32_t GetTypeId() \
64 { \
65  return GetBinaryStringHashCode(#typeName); \
66 }
67 
72 #define IGNITE_BINARY_GET_TYPE_NAME_AS_IS(typeName) \
73 static void GetTypeName(std::string& dst) \
74 { \
75  dst = #typeName; \
76 }
77 
82 #define IGNITE_BINARY_GET_FIELD_ID_AS_HASH \
83 static int32_t GetFieldId(const char* name) \
84 { \
85  return GetBinaryStringHashCode(name); \
86 }
87 
92 #define IGNITE_BINARY_IS_NULL_FALSE(T) \
93 static bool IsNull(const T&) \
94 { \
95  return false; \
96 }
97 
102 #define IGNITE_BINARY_IS_NULL_IF_NULLPTR(T) \
103 static bool IsNull(const T& obj) \
104 { \
105  return obj; \
106 }
107 
112 #define IGNITE_BINARY_GET_NULL_DEFAULT_CTOR(T) \
113 static void GetNull(T& dst) \
114 { \
115  dst = T(); \
116 }
117 
122 #define IGNITE_BINARY_GET_NULL_NULLPTR(T) \
123 static void GetNull(T& dst) \
124 { \
125  dst = 0; \
126 }
127 
128 
129 namespace ignite
130 {
131  namespace binary
132  {
133  class BinaryWriter;
134  class BinaryReader;
135 
142  IGNITE_IMPORT_EXPORT int32_t GetBinaryStringHashCode(const char* val);
143 
147  template<typename T>
148  struct IGNITE_IMPORT_EXPORT BinaryType { };
149 
153  template<typename T>
154  struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultHashing
155  {
161  static int32_t GetTypeId()
162  {
163  std::string typeName;
164  BinaryType<T>::GetTypeName(typeName);
165 
166  return GetBinaryStringHashCode(typeName.c_str());
167  }
168 
175  static int32_t GetFieldId(const char* name)
176  {
177  return GetBinaryStringHashCode(name);
178  }
179  };
180 
184  template<typename T>
185  struct IGNITE_IMPORT_EXPORT BinaryTypeNonNullableType
186  {
192  static bool IsNull(const T&)
193  {
194  return false;
195  }
196 
202  static void GetNull(T& dst)
203  {
204  dst = T();
205  }
206  };
207 
211  template<typename T>
212  struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultAll :
215 
219  template <typename T>
220  struct IGNITE_IMPORT_EXPORT BinaryType<T*>
221  {
224 
230  static int32_t GetTypeId()
231  {
232  return BinaryTypeDereferenced::GetTypeId();
233  }
234 
240  static void GetTypeName(std::string& dst)
241  {
242  BinaryTypeDereferenced::GetTypeName(dst);
243  }
244 
251  static int32_t GetFieldId(const char* name)
252  {
253  return BinaryTypeDereferenced::GetFieldId(name);
254  }
255 
262  static void Write(BinaryWriter& writer, T* const& obj)
263  {
264  BinaryTypeDereferenced::Write(writer, *obj);
265  }
266 
273  static void Read(BinaryReader& reader, T*& dst)
274  {
275  dst = new T();
276 
277  BinaryTypeDereferenced::Read(reader, *dst);
278  }
279 
286  static bool IsNull(T* const& obj)
287  {
288  return !obj || BinaryTypeDereferenced::IsNull(*obj);
289  }
290 
296  static void GetNull(T*& dst)
297  {
298  dst = 0;
299  }
300  };
301  }
302 }
303 
304 #endif //_IGNITE_BINARY_BINARY_TYPE
ignite::binary::BinaryWriter
Binary writer.
Definition: binary_writer.h:50
ignite
Ignite API.
Definition: cache.h:47
ignite::binary::BinaryType< T * >::GetNull
static void GetNull(T *&dst)
Get NULL value for the given binary type.
Definition: binary_type.h:296
ignite::binary::BinaryTypeNonNullableType::GetNull
static void GetNull(T &dst)
Get NULL value for the given binary type.
Definition: binary_type.h:202
ignite::binary::BinaryType< T * >::GetFieldId
static int32_t GetFieldId(const char *name)
Get binary object field ID.
Definition: binary_type.h:251
ignite::binary::BinaryTypeDefaultAll
Default implementations of BinaryType hashing functions and non-null type behaviour.
Definition: binary_type.h:212
ignite::binary::BinaryType< T * >::BinaryTypeDereferenced
BinaryType< T > BinaryTypeDereferenced
Actual type.
Definition: binary_type.h:223
ignite::binary::BinaryTypeDefaultHashing
Default implementations of BinaryType hashing functions.
Definition: binary_type.h:154
ignite::binary::BinaryType< T * >::GetTypeId
static int32_t GetTypeId()
Get binary object type ID.
Definition: binary_type.h:230
ignite::binary::BinaryType< T * >::GetTypeName
static void GetTypeName(std::string &dst)
Get binary object type name.
Definition: binary_type.h:240
ignite::binary::BinaryReader
Binary reader.
Definition: binary_reader.h:53
ignite::binary::BinaryType< T * >::IsNull
static bool IsNull(T *const &obj)
Check whether passed binary object should be interpreted as NULL.
Definition: binary_type.h:286
ignite::binary::BinaryTypeNonNullableType
Default implementations of BinaryType methods for non-null type.
Definition: binary_type.h:185
ignite::binary::BinaryTypeDefaultHashing::GetTypeId
static int32_t GetTypeId()
Get binary object type ID.
Definition: binary_type.h:161
ignite::binary::GetBinaryStringHashCode
IGNITE_IMPORT_EXPORT int32_t GetBinaryStringHashCode(const char *val)
Get binary string hash code.
Definition: binary_type.cpp:24
ignite::binary::BinaryType
Binary type structure.
Definition: binary_type.h:148
ignite::binary::BinaryType< T * >::Read
static void Read(BinaryReader &reader, T *&dst)
Read binary object.
Definition: binary_type.h:273
ignite::binary::BinaryType< T * >::Write
static void Write(BinaryWriter &writer, T *const &obj)
Write binary object.
Definition: binary_type.h:262
ignite::binary::BinaryTypeNonNullableType::IsNull
static bool IsNull(const T &)
Check whether passed binary object should be interpreted as NULL.
Definition: binary_type.h:192
ignite::binary::BinaryTypeDefaultHashing::GetFieldId
static int32_t GetFieldId(const char *name)
Get binary object field ID.
Definition: binary_type.h:175