| 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_BINARY_TYPE
25 #define _IGNITE_BINARY_BINARY_TYPE
26
27 #include <stdint.h>
28
29 #include <ignite/common/common.h>
30
31 #include <ignite/impl/binary/binary_type_impl.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 static int32_t GetTypeId() \
55 { \
56 return id; \
57 }
58
63 #define IGNITE_BINARY_GET_TYPE_ID_AS_HASH(typeName) \
64 static int32_t GetTypeId() \
65 { \
66 return GetBinaryStringHashCode(#typeName); \
67 }
68
73 #define IGNITE_BINARY_GET_TYPE_NAME_AS_IS(typeName) \
74 static void GetTypeName(std::string& dst) \
75 { \
76 dst = #typeName; \
77 }
78
83 #define IGNITE_BINARY_GET_FIELD_ID_AS_HASH \
84 static int32_t GetFieldId(const char* name) \
85 { \
86 return GetBinaryStringHashCode(name); \
87 }
88
93 #define IGNITE_BINARY_IS_NULL_FALSE(T) \
94 static bool IsNull(const T&) \
95 { \
96 return false; \
97 }
98
103 #define IGNITE_BINARY_IS_NULL_IF_NULLPTR(T) \
104 static bool IsNull(const T& obj) \
105 { \
106 return obj; \
107 }
108
113 #define IGNITE_BINARY_GET_NULL_DEFAULT_CTOR(T) \
114 static void GetNull(T& dst) \
115 { \
116 dst = T(); \
117 }
118
123 #define IGNITE_BINARY_GET_NULL_NULLPTR(T) \
124 static void GetNull(T& dst) \
125 { \
126 dst = 0; \
127 }
128
129
130 namespace ignite
131 {
132 namespace binary
133 {
134 class BinaryWriter;
135 class BinaryReader;
136
143 IGNITE_IMPORT_EXPORT int32_t GetBinaryStringHashCode(const char* val);
144
148 template<typename T>
149 struct IGNITE_IMPORT_EXPORT BinaryType { };
150
154 template<typename T>
155 struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultHashing
156 {
162 static int32_t GetTypeId()
163 {
164 std::string typeName;
165 BinaryType<T>::GetTypeName(typeName);
166
167 return GetBinaryStringHashCode(typeName.c_str());
168 }
169
176 static int32_t GetFieldId(const char* name)
177 {
178 return GetBinaryStringHashCode(name);
179 }
180 };
181
185 template<typename T>
186 struct IGNITE_IMPORT_EXPORT BinaryTypeNonNullableType
187 {
193 static bool IsNull(const T&)
194 {
195 return false;
196 }
197
203 static void GetNull(T& dst)
204 {
205 dst = T();
206 }
207 };
208
212 template<typename T>
213 struct IGNITE_IMPORT_EXPORT BinaryTypeDefaultAll :
214 BinaryTypeDefaultHashing<T>,
215 BinaryTypeNonNullableType<T> { };
216
220 template <typename T>
221 struct IGNITE_IMPORT_EXPORT BinaryType<T*>
222 {
224 typedef BinaryType<T> BinaryTypeDereferenced;
225
231 static int32_t GetTypeId()
232 {
233 return BinaryTypeDereferenced::GetTypeId();
234 }
235
241 static void GetTypeName(std::string& dst)
242 {
243 BinaryTypeDereferenced::GetTypeName(dst);
244 }
245
252 static int32_t GetFieldId(const char* name)
253 {
254 return BinaryTypeDereferenced::GetFieldId(name);
255 }
256
263 static void Write(BinaryWriter& writer, T* const& obj)
264 {
265 BinaryTypeDereferenced::Write(writer, *obj);
266 }
267
274 static void Read(BinaryReader& reader, T*& dst)
275 {
276 dst = new T();
277
278 BinaryTypeDereferenced::Read(reader, *dst);
279 }
280
287 static bool IsNull(T* const& obj)
288 {
289 return !obj || BinaryTypeDereferenced::IsNull(*obj);
290 }
291
297 static void GetNull(T*& dst)
298 {
299 dst = 0;
300 }
301 };
302 }
303 }
304
305 #endif //_IGNITE_BINARY_BINARY_TYPE
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< T *>::GetTypeId
static int32_t GetTypeId()
Get binary object type ID.
Definition: binary_type.h:231
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:287
ignite::binary::BinaryTypeDefaultHashing
Default implementations of BinaryType hashing functions.
Definition: binary_type.h:155
ignite::binary::BinaryTypeDefaultHashing::GetFieldId
static int32_t GetFieldId(const char *name)
Get binary object field ID.
Definition: binary_type.h:176
ignite::binary::BinaryTypeNonNullableType::GetNull
static void GetNull(T &dst)
Get NULL value for the given binary type.
Definition: binary_type.h:203
ignite::binary::BinaryWriter
Binary writer.
Definition: binary_writer.h:51
ignite::binary::BinaryType< T *>::Read
static void Read(BinaryReader &reader, T *&dst)
Read binary object.
Definition: binary_type.h:274
ignite::binary::BinaryTypeDefaultHashing::GetTypeId
static int32_t GetTypeId()
Get binary object type ID.
Definition: binary_type.h:162
ignite::binary::BinaryType< T *>::GetTypeName
static void GetTypeName(std::string &dst)
Get binary object type name.
Definition: binary_type.h:241
ignite::binary::BinaryType< T *>::Write
static void Write(BinaryWriter &writer, T *const &obj)
Write binary object.
Definition: binary_type.h:263
ignite::binary::BinaryType
Binary type structure.
Definition: binary_type.h:149
ignite::binary::BinaryType< T *>::BinaryTypeDereferenced
BinaryType< T > BinaryTypeDereferenced
Actual type.
Definition: binary_type.h:224
ignite::binary::BinaryTypeNonNullableType::IsNull
static bool IsNull(const T &)
Check whether passed binary object should be interpreted as NULL.
Definition: binary_type.h:193
ignite::binary::BinaryType< T *>::GetFieldId
static int32_t GetFieldId(const char *name)
Get binary object field ID.
Definition: binary_type.h:252
ignite::binary::BinaryTypeDefaultAll
Default implementations of BinaryType hashing functions and non-null type behaviour.
Definition: binary_type.h:213
ignite::binary::BinaryType< T *>::GetNull
static void GetNull(T *&dst)
Get NULL value for the given binary type.
Definition: binary_type.h:297
ignite
Apache Ignite API.
Definition: cache.h:48
ignite::binary::BinaryTypeNonNullableType
Default implementations of BinaryType methods for non-null type.
Definition: binary_type.h:186
ignite::binary::BinaryReader
Binary reader.
Definition: binary_reader.h:54