1 #ifndef ZSERIO_REFLECTABLE_UTIL_H_INC 2 #define ZSERIO_REFLECTABLE_UTIL_H_INC 23 struct gets_value_by_value : std::integral_constant<bool,
24 std::is_arithmetic<T>::value ||
25 std::is_same<StringView, T>::value ||
26 std::is_enum<T>::value ||
48 template <
typename ALLOC = std::allocator<u
int8_t>>
63 template <
typename T,
typename ALLOC = std::allocator<u
int8_t>,
64 typename std::enable_if<detail::gets_value_by_value<T>::value,
int>::type = 0>
67 return reflectable->getAnyValue(allocator).template get<T>();
83 template <
typename T,
typename ALLOC = std::allocator<u
int8_t>,
84 typename std::enable_if<!detail::gets_value_by_value<T>::value,
int>::type = 0>
86 const ALLOC& allocator = ALLOC())
88 return reflectable->getAnyValue(allocator).template get<std::reference_wrapper<const T>>().
get();
104 template <
typename T,
typename ALLOC = std::allocator<u
int8_t>,
105 typename std::enable_if<
106 !detail::gets_value_by_value<T>::value &&
107 !std::is_same<BasicBitBuffer<ALLOC>, T>::value,
int>::type = 0>
110 return reflectable->getAnyValue(allocator).template get<std::reference_wrapper<T>>().
get();
124 template <
typename T,
typename ALLOC = std::allocator<u
int8_t>,
125 typename std::enable_if<std::is_same<BasicBitBuffer<ALLOC>, T>::value,
int>::type = 0>
128 return reflectable->getAnyValue(allocator).template get<std::reference_wrapper<const T>>().
get();
132 template <
typename ALLOC>
136 template <
typename ALLOC>
140 template <
typename ALLOC>
144 static bool doubleValuesAlmostEqual(
double lhs,
double rhs);
147 template <
typename ALLOC>
151 if (lhs ==
nullptr || rhs ==
nullptr)
154 const auto& lhsTypeInfo = lhs->getTypeInfo();
155 const auto& rhsTypeInfo = rhs->getTypeInfo();
157 if (lhsTypeInfo.getSchemaType() != rhsTypeInfo.getSchemaType() ||
158 lhsTypeInfo.getSchemaName() != rhsTypeInfo.getSchemaName())
161 if (lhs->isArray() || rhs->isArray())
163 if (!lhs->isArray() || !rhs->isArray())
165 return arraysEqual<ALLOC>(lhs, rhs);
169 return compoundsEqual<ALLOC>(lhs, rhs);
173 return valuesEqual<ALLOC>(lhs, rhs);
177 template <
typename ALLOC>
181 if (lhsArray->size() != rhsArray->size())
184 for (
size_t i = 0; i < lhsArray->size(); ++i)
186 if (!equal<ALLOC>(lhsArray->at(i), rhsArray->at(i)))
193 template <
typename ALLOC>
197 for (
const auto& parameterInfo : lhsCompound->getTypeInfo().getParameters())
199 auto lhsParameter = lhsCompound->getParameter(parameterInfo.schemaName);
200 auto rhsParameter = rhsCompound->getParameter(parameterInfo.schemaName);
201 if (!equal<ALLOC>(lhsParameter, rhsParameter))
207 if (lhsCompound->getChoice() != rhsCompound->getChoice())
210 if (!lhsCompound->getChoice().empty())
212 auto lhsField = lhsCompound->getField(lhsCompound->getChoice());
213 auto rhsField = rhsCompound->getField(rhsCompound->getChoice());
214 if (!equal<ALLOC>(lhsField, rhsField))
220 for (
const auto& fieldInfo : lhsCompound->getTypeInfo().getFields())
222 auto lhsField = lhsCompound->getField(fieldInfo.schemaName);
223 auto rhsField = rhsCompound->getField(fieldInfo.schemaName);
224 if (!equal<ALLOC>(lhsField, rhsField))
232 template <
typename ALLOC>
236 CppType cppType = lhsValue->getTypeInfo().getCppType();
238 cppType = lhsValue->getTypeInfo().getUnderlyingType().getCppType();
243 return lhsValue->getBool() == rhsValue->getBool();
248 return lhsValue->toInt() == rhsValue->toInt();
253 return lhsValue->toUInt() == rhsValue->toUInt();
256 return doubleValuesAlmostEqual(lhsValue->toDouble(), rhsValue->toDouble());
265 return lhsValue->getStringView() == rhsValue->getStringView();
267 return lhsValue->getBitBuffer() == rhsValue->getBitBuffer();
273 inline bool ReflectableUtil::doubleValuesAlmostEqual(
double lhs,
double rhs)
275 if (std::isinf(lhs) || std::isinf(rhs))
276 return std::isinf(lhs) && std::isinf(rhs) && ((lhs > 0.0 && rhs > 0.0) || (lhs < 0.0 && rhs < 0.0));
278 if (std::isnan(lhs) || std::isnan(rhs))
279 return std::isnan(lhs) && std::isnan(rhs);
282 return std::fabs(lhs - rhs) <= std::numeric_limits<double>::epsilon() * std::fabs(lhs + rhs)
283 || std::fabs(lhs - rhs) < std::numeric_limits<double>::min();
288 #endif // ZSERIO_REFLECTABLE_UTIL_H_INC typename IBasicReflectable< ALLOC >::ConstPtr IBasicReflectableConstPtr
typename IBasicReflectable< ALLOC >::Ptr IBasicReflectablePtr
static T getValue(const IBasicReflectableConstPtr< ALLOC > &reflectable, const ALLOC &allocator=ALLOC())
static const T & getValue(const IBasicReflectablePtr< ALLOC > &reflectable, const ALLOC &allocator=ALLOC())
constexpr size_type size() const noexcept
static bool isCompound(SchemaType schemaType)
static const T & getValue(const IBasicReflectableConstPtr< ALLOC > &reflectable, const ALLOC &allocator=ALLOC())
static bool hasChoice(SchemaType schemaType)
static bool equal(const IBasicReflectableConstPtr< ALLOC > &lhs, const IBasicReflectableConstPtr< ALLOC > &rhs)
constexpr iterator begin() const noexcept
constexpr iterator end() const noexcept
static T & getValue(const IBasicReflectablePtr< ALLOC > &reflectable, const ALLOC &allocator=ALLOC())