Add CodeWriter utility class.
Helps simplify code generation code. Instead of this: code += "inline const " + cpp_qualified_name + " *Get"; code += name; code += "(const void *buf) {\n return flatbuffers::GetRoot<"; code += cpp_qualified_name + ">(buf);\n}\n\n"; You do this: code.SetValue("NAME", struct_def.name); code.SetValue("CPP_NAME", cpp_qualified_name); code += "inline const {{CPP_NAME}} *Get{{NAME}}(const void *buf) {"; code += " return flatbuffers::GetRoot<{{CPP_NAME}}>(buf);"; code += "}"; code += ""; Updated the CPP code generator to use the CodeWriter class. Most of the changes in the generated code are white-space changes, esp. around new lines (since the code generator class automatically appends new lines when appending a string). Actual code changes include: * Renamed "rehasher" to "_rehasher" for consistency with other args in Pack function. * Renamed "union_obj" to "obj: in UnPack function. * Always do "(void)_o;" to prevent unused variable warning in Create function (instead of only doing it if there are no fields) in order to avoid two-passes. * Renamed padding variables from __paddingX to paddingX__. "Each name that contains a double underscore (_ _) [...] is reserved to the implementation for any use." C++ standards 17.4.3.1.2. * Add braces around switch cases. * Calculate index as a separate statement in EnumName function, eg. const size_t index = ...; return EnumNamesX()[index]; vs. return EnumNamesX()[...]; * Stored end table offset in variable in Finish() functions, eg. const auto end = fbb_.EndTable(start_, ...); auto o = flatbuffers::Offset<T>(end); vs. auto o = flatbuffers::Offset<T>(fbb_.EndTable(start, ...)); * Separate reinterpret_cast calls from function calls in Union functions, eg. auto ptr = reinterpret_cast<const T *>(obj); return ptr->UnPack(resolver); vs. return reinterpret_cast<const T *>(obj)->UnPack(resolver); * Removed unecessary (void)(padding__X) no-ops from constructors, eg. Test(int16_t a, int8_t b) : ... { (void)__padding0; // <-- Removed this line. } In the idl_gen_cpp.cpp file itself, I refactored some code generation into new functions: GenParam, GenNativeTable, GenVerifyCall, GenBuilders, GenUnpackFieldStatement, and GenCreateParam. Change-Id: I727b1bd8719d05b7ce33cbce00eb58fda817b25d
Showing
- CMakeLists.txt 2 additions, 8 deletionsCMakeLists.txt
- include/flatbuffers/code_generators.h 67 additions, 54 deletionsinclude/flatbuffers/code_generators.h
- include/flatbuffers/idl.h 0 additions, 7 deletionsinclude/flatbuffers/idl.h
- include/flatbuffers/reflection_generated.h 293 additions, 93 deletionsinclude/flatbuffers/reflection_generated.h
- samples/monster_generated.h 222 additions, 91 deletionssamples/monster_generated.h
- src/code_generators.cpp 158 additions, 0 deletionssrc/code_generators.cpp
- src/idl_gen_cpp.cpp 1019 additions, 753 deletionssrc/idl_gen_cpp.cpp
- src/idl_gen_fbs.cpp 1 addition, 0 deletionssrc/idl_gen_fbs.cpp
- src/idl_gen_general.cpp 1 addition, 32 deletionssrc/idl_gen_general.cpp
- tests/monster_test_generated.h 552 additions, 223 deletionstests/monster_test_generated.h
- tests/namespace_test/namespace_test1_generated.h 46 additions, 15 deletionstests/namespace_test/namespace_test1_generated.h
- tests/namespace_test/namespace_test2_generated.h 61 additions, 21 deletionstests/namespace_test/namespace_test2_generated.h
Loading
Please register or sign in to comment