Zserio C++ runtime library  1.1.0
Built for Zserio 2.15.0
Enums.h
Go to the documentation of this file.
1 #ifndef ZSERIO_ENUMS_H_INC
2 #define ZSERIO_ENUMS_H_INC
3 
4 #include <algorithm>
5 #include <cstddef>
6 #include <type_traits>
7 
9 #include "zserio/StringView.h"
10 #include "zserio/Types.h"
11 
12 // This should be implemented in runtime library header.
13 namespace zserio
14 {
15 
16 // forward declarations
17 class BitStreamReader;
18 class BitStreamWriter;
19 
23 template <typename T>
24 struct EnumTraits
25 {};
26 
34 template <typename T>
35 size_t enumToOrdinal(T value);
36 
46 template <typename T>
47 T valueToEnum(typename std::underlying_type<T>::type rawValue);
48 
56 template <typename T>
57 constexpr typename std::underlying_type<T>::type enumToValue(T value)
58 {
59  return static_cast<typename std::underlying_type<T>::type>(value);
60 }
61 
69 template <typename T>
70 uint32_t enumHashCode(T value);
71 
81 template <typename T>
83 {
84  const auto foundIt = std::find_if(EnumTraits<T>::names.begin(), EnumTraits<T>::names.end(),
85  [itemName](const char* enumItemName) { return itemName.compare(enumItemName) == 0; });
86  if (foundIt == EnumTraits<T>::names.end())
87  {
88  throw CppRuntimeException("Enum item '")
89  << itemName << "' doesn't exist in enum '" << EnumTraits<T>::enumName << "'!";
90  }
91 
92  const size_t ordinal = static_cast<size_t>(std::distance(EnumTraits<T>::names.begin(), foundIt));
93  return EnumTraits<T>::values[ordinal];
94 }
95 
103 template <typename T>
104 const char* enumToString(T value)
105 {
106  return EnumTraits<T>::names[enumToOrdinal(value)];
107 }
108 
115 template <typename PACKING_CONTEXT, typename T>
116 void initPackingContext(PACKING_CONTEXT& context, T value);
117 
127 template <typename T>
128 size_t bitSizeOf(T value);
129 
140 template <typename PACKING_CONTEXT, typename T>
141 size_t bitSizeOf(PACKING_CONTEXT& context, T value);
142 
154 template <typename T>
155 size_t initializeOffsets(size_t bitPosition, T value);
156 
169 template <typename PACKING_CONTEXT, typename T>
170 size_t initializeOffsets(PACKING_CONTEXT& context, size_t bitPosition, T value);
171 
179 template <typename T>
181 
190 template <typename T, typename PACKING_CONTEXT>
191 T read(PACKING_CONTEXT& context, BitStreamReader& in);
192 
199 template <typename T>
200 void write(BitStreamWriter& out, T value);
201 
209 template <typename PACKING_CONTEXT, typename T>
210 void write(PACKING_CONTEXT& context, BitStreamWriter& out, T value);
211 
220 template <typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
222 {
223  exception << enumToString(value);
224  return exception;
225 }
226 
227 } // namespace zserio
228 
229 #endif // ZSERIO_ENUMS_H_INC
int compare(BasicStringView other) const noexcept
Definition: StringView.h:356
T read(BitStreamReader &in)
constexpr std::underlying_type< T >::type enumToValue(T value)
Definition: Enums.h:57
void write(BitStreamWriter &out, T value)
T valueToEnum(typename std::underlying_type< T >::type rawValue)
T stringToEnum(StringView itemName)
Definition: Enums.h:82
CppRuntimeException & operator<<(CppRuntimeException &exception, const BasicBitBuffer< ALLOC > &bitBuffer)
Definition: BitBuffer.h:455
size_t enumToOrdinal(T value)
size_t initializeOffsets(size_t bitPosition, T value)
const char * enumToString(T value)
Definition: Enums.h:104
void initPackingContext(PACKING_CONTEXT &context, T value)
uint32_t enumHashCode(T value)
size_t bitSizeOf(T value)