Apache Ignite C++
binary_type.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
24 #ifndef _IGNITE_BINARY_TYPE
25 #define _IGNITE_BINARY_TYPE
26 
27 #include <stdint.h>
28 
29 #include <ignite/common/common.h>
30 
31 #include "ignite/ignite_error.h"
32 
37 #define IGNITE_BINARY_TYPE_START(T) \
38 template<> \
39 struct BinaryType<T> \
40 {
41 
46 #define IGNITE_BINARY_TYPE_END \
47 };
48 
53 #define IGNITE_BINARY_GET_TYPE_ID_AS_CONST(id) \
54 int32_t GetTypeId() \
55 { \
56  return id; \
57 }
58 
63 #define IGNITE_BINARY_GET_TYPE_ID_AS_HASH(typeName) \
64 int32_t GetTypeId() \
65 { \
66  return GetBinaryStringHashCode(#typeName); \
67 }
68 
73 #define IGNITE_BINARY_GET_TYPE_NAME_AS_IS(typeName) \
74 std::string GetTypeName() \
75 { \
76  return #typeName; \
77 }
78 
83 #define IGNITE_BINARY_GET_FIELD_ID_AS_HASH \
84 int32_t GetFieldId(const char* name) \
85 { \
86  return GetBinaryStringHashCode(name); \
87 }
88 
93 #define IGNITE_BINARY_GET_HASH_CODE_ZERO(T) \
94 int32_t GetHashCode(const T& obj) \
95 { \
96  return 0; \
97 }
98 
103 #define IGNITE_BINARY_IS_NULL_FALSE(T) \
104 bool IsNull(const T& obj) \
105 { \
106  return false; \
107 }
108 
113 #define IGNITE_BINARY_IS_NULL_IF_NULLPTR(T) \
114 bool IsNull(const T& obj) \
115 { \
116  return obj; \
117 }
118 
123 #define IGNITE_BINARY_GET_NULL_DEFAULT_CTOR(T) \
124 T GetNull() \
125 { \
126  return T(); \
127 }
128 
133 #define IGNITE_BINARY_GET_NULL_NULLPTR(T) \
134 T GetNull() \
135 { \
136  return NULL; \
137 }
138 
139 namespace ignite
140 {
141  namespace binary
142  {
143  class BinaryWriter;
144  class BinaryReader;
145 
152  IGNITE_IMPORT_EXPORT int32_t GetBinaryStringHashCode(const char* val);
153 
157  template<typename T>
158  struct IGNITE_IMPORT_EXPORT BinaryType
159  {
165  int32_t GetTypeId()
166  {
167  IGNITE_ERROR_1(IgniteError::IGNITE_ERR_BINARY, "GetTypeId function is not defined for binary type.");
168  }
169 
175  std::string GetTypeName()
176  {
177  IGNITE_ERROR_1(IgniteError::IGNITE_ERR_BINARY, "GetTypeName function is not defined for binary type.");
178  }
179 
186  int32_t GetFieldId(const char* name)
187  {
188  return GetBinaryStringHashCode(name);
189  }
190 
197  int32_t GetHashCode(const T& obj)
198  {
199  return 0;
200  }
201 
208  void Write(BinaryWriter& writer, const T& obj)
209  {
210  IGNITE_ERROR_1(IgniteError::IGNITE_ERR_BINARY, "Write function is not defined for binary type.");
211  }
212 
219  T Read(BinaryReader& reader)
220  {
221  IGNITE_ERROR_1(IgniteError::IGNITE_ERR_BINARY, "Read function is not defined for binary type.");
222  }
223 
230  bool IsNull(const T& obj)
231  {
232  return false;
233  }
234 
241  {
242  IGNITE_ERROR_1(IgniteError::IGNITE_ERR_BINARY, "GetNull function is not defined for binary type.");
243  }
244  };
245 
249  template <typename T>
250  struct IGNITE_IMPORT_EXPORT BinaryType<T*>
251  {
254 
259  {
260  typ = BinaryType<T>();
261  }
262 
263  int32_t GetTypeId()
264  {
265  return typ.GetTypeId();
266  }
267 
268  std::string GetTypeName()
269  {
270  return typ.GetTypeName();
271  }
272 
273  int32_t GetFieldId(const char* name)
274  {
275  return typ.GetFieldId(name);
276  }
277 
278  int32_t GetHashCode(T* const& obj)
279  {
280  return typ.GetHashCode(*obj);
281  }
282 
283  void Write(BinaryWriter& writer, T* const& obj)
284  {
285  typ.Write(writer, *obj);
286  }
287 
288  T* Read(BinaryReader& reader)
289  {
290  T* res = new T();
291 
292  *res = typ.Read(reader);
293 
294  return res;
295  }
296 
297  bool IsNull(T* const& obj)
298  {
299  return !obj || typ.IsNull(*obj);
300  }
301 
302  T* GetNull()
303  {
304  return NULL;
305  }
306  };
307  }
308 }
309 
310 #endif
IGNITE_IMPORT_EXPORT int32_t GetBinaryStringHashCode(const char *val)
Get binary string hash code.
Definition: binary_type.cpp:24
void Write(BinaryWriter &writer, const T &obj)
Write binary object.
Definition: binary_type.h:208
int32_t GetHashCode(const T &obj)
Get binary object hash code.
Definition: binary_type.h:197
static const int IGNITE_ERR_BINARY
Binary error.
Definition: ignite_error.h:109
BinaryType< T > typ
Actual type.
Definition: binary_type.h:253
Binary writer.
Definition: binary_writer.h:40
T Read(BinaryReader &reader)
Read binary object.
Definition: binary_type.h:219
Binary type structure.
Definition: binary_type.h:158
int32_t GetFieldId(const char *name)
Get binary object field ID.
Definition: binary_type.h:186
int32_t GetTypeId()
Get binary object type ID.
Definition: binary_type.h:165
T GetNull()
Get NULL value for the given binary type.
Definition: binary_type.h:240
bool IsNull(const T &obj)
Check whether passed binary object should be interpreted as NULL.
Definition: binary_type.h:230
BinaryType()
Constructor.
Definition: binary_type.h:258
Apache Ignite API.
Definition: binary_consts.h:28
Declares ignite::IgniteError class.
Binary reader.
Definition: binary_reader.h:41
std::string GetTypeName()
Get binary object type name.
Definition: binary_type.h:175