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:
Vladimir Glavnyy
2019-01-08 01:04:44 +07:00
committed by Wouter van Oortmerssen
parent 155c55900f
commit dd288f71f3
10 changed files with 379 additions and 5 deletions

View File

@@ -130,6 +130,33 @@ extern void GenComment(const std::vector<std::string> &dc,
std::string *code_ptr, const CommentConfig *config,
const char *prefix = "");
class FloatConstantGenerator {
public:
virtual ~FloatConstantGenerator(){};
std::string GenFloatConstant(const FieldDef &field) const;
private:
virtual std::string Inf(double v) const = 0;
virtual std::string NaN(double v) const = 0;
virtual std::string Value(double v, const std::string &src) const {
(void)v;
return src;
}
virtual std::string Inf(float v) const {
return this->Inf(static_cast<double>(v));
}
virtual std::string NaN(float v) const {
return this->NaN(static_cast<double>(v));
}
virtual std::string Value(float v, const std::string &src) const {
return this->Value(static_cast<double>(v), src);
}
template<typename T>
std::string GenFloatConstantImpl(const FieldDef &field) const;
};
} // namespace flatbuffers
#endif // FLATBUFFERS_CODE_GENERATORS_H_