GridGain C++
binary_enum.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_ENUM
24 #define _IGNITE_BINARY_BINARY_ENUM
25 
26 #include <stdint.h>
27 
28 #include <ignite/common/common.h>
29 
31 
32 namespace ignite
33 {
34  namespace binary
35  {
52  template<typename T>
53  struct BinaryEnum { };
54 
58  template<typename T>
60  {
66  static int32_t GetTypeId()
67  {
68  std::string typeName;
70 
71  return GetBinaryStringHashCode(typeName.c_str());
72  }
73 
79  static int32_t GetOrdinal(T value)
80  {
81  return static_cast<int32_t>(value);
82  }
83 
89  static T FromOrdinal(int32_t ordinal)
90  {
91  return static_cast<T>(ordinal);
92  }
93  };
94 
98  template<typename T>
100  {
106  static bool IsNull(const T& val)
107  {
108  return val == BinaryEnum<T>::FromOrdinal(INT32_MIN);
109  }
110 
116  static void GetNull(T& dst)
117  {
118  dst = BinaryEnum<T>::FromOrdinal(INT32_MIN);
119  }
120  };
121 
125  template<typename T>
128  BinaryEnumIntMinNull<T> { };
129 
133  template <typename T>
134  struct BinaryEnum<T*>
135  {
138 
144  static int32_t GetTypeId()
145  {
146  return BinaryEnumDereferenced::GetTypeId();
147  }
148 
154  static void GetTypeName(std::string& dst)
155  {
156  BinaryEnumDereferenced::GetTypeName(dst);
157  }
158 
164  static int32_t GetOrdinal(T* value)
165  {
166  return BinaryEnumDereferenced::GetOrdinal(*value);
167  }
168 
174  static T* FromOrdinal(int32_t ordinal)
175  {
176  return new T(BinaryEnumDereferenced::FromOrdinal(ordinal));
177  }
178 
185  static bool IsNull(T* const& obj)
186  {
187  return !obj || BinaryEnumDereferenced::IsNull(*obj);
188  }
189 
195  static void GetNull(T*& dst)
196  {
197  dst = 0;
198  }
199  };
200  }
201 }
202 
203 #endif //_IGNITE_BINARY_BINARY_ENUM
ignite
Ignite API.
Definition: cache.h:47
ignite::binary::BinaryEnum
Binary enum structure.
Definition: binary_enum.h:53
ignite::binary::BinaryEnumIntMinNull::IsNull
static bool IsNull(const T &val)
Check whether passed binary object should be interpreted as NULL.
Definition: binary_enum.h:106
ignite::binary::BinaryEnum< T * >::GetOrdinal
static int32_t GetOrdinal(T *value)
Get enum type ordinal.
Definition: binary_enum.h:164
ignite::binary::BinaryEnumIntMinNull::GetNull
static void GetNull(T &dst)
Get NULL value for the given binary type.
Definition: binary_enum.h:116
ignite::binary::BinaryEnumDefault
Default implementations of BinaryEnum.
Definition: binary_enum.h:59
ignite::binary::BinaryEnumDefault::GetTypeId
static int32_t GetTypeId()
Get type ID for the enum type.
Definition: binary_enum.h:66
ignite::binary::BinaryEnumDefault::GetOrdinal
static int32_t GetOrdinal(T value)
Get enum type ordinal.
Definition: binary_enum.h:79
ignite::binary::BinaryEnumIntMinNull
Implementations of BinaryEnum nullability when INT32_MIN ordinal value used as a NULL indicator.
Definition: binary_enum.h:99
ignite::binary::BinaryEnum< T * >::IsNull
static bool IsNull(T *const &obj)
Check whether passed enum should be interpreted as NULL.
Definition: binary_enum.h:185
ignite::binary::BinaryEnum< T * >::BinaryEnumDereferenced
BinaryEnum< T > BinaryEnumDereferenced
Actual type.
Definition: binary_enum.h:137
ignite::binary::BinaryEnum< T * >::GetTypeId
static int32_t GetTypeId()
Get binary object type ID.
Definition: binary_enum.h:144
ignite::binary::GetBinaryStringHashCode
IGNITE_IMPORT_EXPORT int32_t GetBinaryStringHashCode(const char *val)
Get binary string hash code.
Definition: binary_type.cpp:24
ignite::binary::BinaryEnumDefault::FromOrdinal
static T FromOrdinal(int32_t ordinal)
Get enum value for the given ordinal value.
Definition: binary_enum.h:89
binary_type.h
ignite::binary::BinaryEnum< T * >::GetNull
static void GetNull(T *&dst)
Get NULL value for the enum type.
Definition: binary_enum.h:195
ignite::binary::BinaryEnum< T * >::FromOrdinal
static T * FromOrdinal(int32_t ordinal)
Get enum value for the given ordinal value.
Definition: binary_enum.h:174
ignite::binary::BinaryEnumDefaultAll
Default implementations of BinaryType hashing functions and non-null type behaviour.
Definition: binary_enum.h:126
ignite::binary::BinaryEnum< T * >::GetTypeName
static void GetTypeName(std::string &dst)
Get binary object type name.
Definition: binary_enum.h:154