userver: userver/formats/common/validations.hpp Source File
Loading...
Searching...
No Matches
validations.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file universal/include/userver/formats/common/validations.hpp
4/// @brief @copybrief formats::common::ValidateFloat()
5
6#include <cmath>
7
8#include <userver/utils/assert.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace formats::common {
13
14/// @brief Common restrictions to floating type values serialization. Asserts
15/// or throws on NaN and Inf values.
16template <typename ExceptionType, typename Float>
17Float ValidateFloat(Float value) {
18 if (std::isnan(value)) {
19 UASSERT_MSG(false, "Floating point nan value serialization is forbidden");
20 throw ExceptionType("Floating point nan value serialization is forbidden");
21 }
22 if (std::isinf(value)) {
23 UASSERT_MSG(false, "Floating point inf value serialization is forbidden");
24 throw ExceptionType("Floating point inf value serialization is forbidden");
25 }
26 return value;
27}
28
29} // namespace formats::common
30
31USERVER_NAMESPACE_END