mirror of
https://github.com/vee1e/flatbuffers.git
synced 2026-09-03 02:37:31 +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:
parent
155c55900f
commit
dd288f71f3
10 changed files with 379 additions and 5 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue