mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
Add NaN and Inf defaults to the C++ generated code. (#5102)
* Add `NaN` and `Inf` defaults to the C++ generated code. * Refactoring: add FloatConstantGenerator * Refactoring-2: - remove isnan checking for all float/double values - add most probable implementation of virtual methods of FloatConstantGenerator * Add conditional (FLATBUFFERS_NAN_DEFAULTS) isnan checking
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
155c55900f
commit
dd288f71f3
@@ -19,6 +19,8 @@
|
||||
#include "flatbuffers/base.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable : 4127) // C4127: conditional expression is constant
|
||||
@@ -157,6 +159,35 @@ void GenComment(const std::vector<std::string> &dc, std::string *code_ptr,
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string FloatConstantGenerator::GenFloatConstantImpl(
|
||||
const FieldDef &field) const {
|
||||
const auto &constant = field.value.constant;
|
||||
T v;
|
||||
auto done = StringToNumber(constant.c_str(), &v);
|
||||
FLATBUFFERS_ASSERT(done);
|
||||
if (done) {
|
||||
#if (!defined(_MSC_VER) || (_MSC_VER >= 1800))
|
||||
if (std::isnan(v)) return NaN(v);
|
||||
if (std::isinf(v)) return Inf(v);
|
||||
#endif
|
||||
return Value(v, constant);
|
||||
}
|
||||
return "#"; // compile time error
|
||||
}
|
||||
|
||||
std::string FloatConstantGenerator::GenFloatConstant(
|
||||
const FieldDef &field) const {
|
||||
switch (field.value.type.base_type) {
|
||||
case BASE_TYPE_FLOAT: return GenFloatConstantImpl<float>(field);
|
||||
case BASE_TYPE_DOUBLE: return GenFloatConstantImpl<double>(field);
|
||||
default: {
|
||||
FLATBUFFERS_ASSERT(false);
|
||||
return "INVALID_BASE_TYPE";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace flatbuffers
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
Reference in New Issue
Block a user