Commit Graph

18 Commits

Author SHA1 Message Date
Jean-François Roy 43203984f7
[C++] Support C++ object copies and moves (#5988)
Augment the C++ generator to emit a C++ copy constructor and a by-value
copy assignment operator. This is enabled by default when the C++
standard is C++11 or later. These additional functions are only emitted
for objects that need it, typically tables containing other tables.

These new functions are declared in the object table type and are
defined as inline functions after table declarations.

When these new functions are declared, a user-defined
explicitly-defaulted default constructor and move constructor are also
emitted.

The copy assignment operator uses the copy-and-swap idiom to provide
strong exception safety, at the expense of keeping 2 full table copies
in memory temporarily.

fixes #5783
2022-01-29 14:24:24 -08:00
Justin T Conroy 19920db39f
Include a SizePrefixed..HasIdentifier for c++. (#6871)
* Include a SizePrefixed..HasIdentifier for c++.

* Add updated generated code.
2022-01-28 20:52:32 -08:00
Wouter van Oortmerssen a42e898979
Added verifier alignment checking to table fields (#7018) 2022-01-14 14:39:15 -08:00
Stefan F b20c4d3aad
[C++] Add GetMutableSizePrefixedRoot and generate GetMutableSizePrefixedXxx functions (#6815)
* flattests_cpp17 doesn't compile with Visual Studio 2017: warning C4100: 'indent': unreferenced formal parameter

stringify_util.h(127): error C2220: warning treated as error - no 'object' file generated
stringify_util.h(127): warning C4100: 'indent': unreferenced formal parameter
stringify_util.h(85): warning C4100: 'indent': unreferenced formal parameter
stringify_util.h(85): warning C4100: 'fbs': unreferenced formal parameter

* [C++] Add GetMutableSizePrefixedRoot() and generate a GetMutableSizePrefixed function
When using the mutable API together with size prefixed buffers these functions should be present.

* clang-format

* Cleanup branch for PR
Revert "flattests_cpp17 doesn't compile with Visual Studio 2017: warning C4100: 'indent': unreferenced formal parameter"

This reverts commit a92055203e.
2021-08-30 15:34:42 -07:00
mogemimi c39fc9dd9c
[C++] Use nullptr instead of 0 in generated headers (#6762) 2021-08-02 13:41:15 -07:00
Mika Raento 962751a6ec
Improve generated comparisons for tables (#6486)
* Improve generated comparisons for tables

Previously, the generated code called std::unique_ptr<>::operator== on
non-scalar members, which would be correct only if the member was null
on both sides. After this CL, comparison is done on the pointed-to
values (including using a default-constructed value if the pointer is
null).

* Don't equate null Tables with all defaults

Also removes the cost of default-constructing tables for comparisons.

* Regenerate code

* fix formatting
2021-06-21 11:37:56 -07:00
Vladimir Glavnyy 7f33cf682a
[C++] Switch `flatc` to `--cpp-std c++11` C++ code generator (#6306)
Prior to this commit the default C++ code generator was `c++0x`.
A code generated with `c++0x` code-gen might have a vulnerability (undefined behavior) connected evolution of enums in a schema. This UB could break the backward compatibility if previously generated code casts an unknown enumerator to enum type that knows nothing about future enumerators added to the schema.

The main differences between `c++0x` and `c++11`:

- generated enums use explicitly declared underlying type;
- generated object-API tables don't declare default ctor() explicitly, instead of it default data member initializers are generated.

Please use `flatc --cpp-std c++0x` option for backward compatibility with old compilers.
2020-12-07 11:19:36 -08:00
Vladimir Glavnyy 04bec23a37
Add Array initialization from struct constructor (#5865) (#6147)
- add flatbuffers::span
- add new constructor for `struct` with `array`
- add some test for flatbuffers::span and 'arrays_test.fbs'
2020-10-12 12:24:18 -07:00
Mark Nauwelaerts 34d67b425e
Minireflect fixed array (#6129)
* CMakeLists: also really generate optional_scalars as needed by test elsewhere

* [C++] Handle fixed-length array in minireflection

Fixes #6128
2020-09-22 17:57:01 -07:00
bakinovsky-m 988164f6e1
[C++] Got rid of memset's in constructors (#5938)
* [C++] removed array's memsets from struct parametrized constructor

now POD-typed arrays are zero-initialized
and class-typed arrays are default-initialized

* [C++] memset -> zero/default initialization in default constructor

* [C++] Struct-type and array default initialization

* [C++] Newly generated tests

* [C++] forgotten test

* [C++] curly brace by code style

* [C++] test if memory is 0's after placement new

* [C++] memory leak fix

* [C++] simplifying and non-dynamic memory in test

* [C++] code cleanup

* [C++] disable old-compiler warning

* [C++] windows build fix (try)

* [C++] debug-new win build fix
2020-06-01 17:58:52 -07:00
Arthur O'Dwyer 6b271b7ecb
Fix Clang-trunk warnings about special members deprecated in C++20. (#5829)
For example:

    include/flatbuffers/reflection.h:365:8: error: definition of implicit copy
          constructor for 'pointer_inside_vector<flatbuffers::Table, unsigned char>'
          is deprecated because it has a user-declared copy assignment operator
          [-Werror,-Wdeprecated-copy]
      void operator=(const pointer_inside_vector &piv);
           ^

It's unclear why the old code wanted to declare a public `operator=`
without defining it; that just seems like a misunderstanding of the C++03 idiom
for deleting a member function. And anyway, we don't *want* to delete the
assignment operator; these are polymorphic types that do not follow value
semantics and nobody should ever be trying to copy them. So the simplest fix
is just to go back to the Rule of Zero: remove the declaration of `operator=`
and let the compiler do what it wanted to do originally anyway.
"The best code is no code."

Also, update the generated .h files.

Fixes #5649.
2020-03-23 10:01:11 -07:00
xtrm0 54f8b787cb
Fix memory leak on cpp object api (#5761)
Previously UnPack would allocate data with new and assign it to a
raw pointer. This behavior makes it possible for the pointer to be
leaked in case of OOM. This commit defaults to use the user specified
pointer (which needs to implement a move constructor, a .get() and a
 .release() operators), thus preventing these leaks.
2020-02-12 12:12:45 -08:00
Austin Schuh 901b89e733 [C++] Add Builder and Table typedefs (#5685)
* Add Builder and Table typedefs

This gives us a way to use templates to go from a builder to a table
and back again without having to pass both types in.

* Fix tests/cpp17/generated_cpp17/monster_test_generated.h
2019-12-26 14:56:46 -08:00
Vladimir Glavnyy 44bf719883 Add flatc '--cpp_std' switch (#5656)
* Add flatc '--cpp_std' switch and sandbox for C++17 code generator

- Added 'flac --cpp_std legacy' for compatibility with old compilers (VS2010);
- Added experimental switch 'flac --cpp_std c++17' for future development;
- Added C++17 sandbox test_cpp17.cpp;
- C++ code generator generates enums with explicit underlying type to avoid problems with the forward and backward schema compatibility;
- Adjusted CMakeLists.txt, CI and generate code scripts to support of introduced '--cpp_std';

* Fix --cpp_std values: c++0x, c++11, c++17

* Add 'cpp::CppStandard' enum

* Add testing engine into test_cpp17

* Rebase to upstream/master

* Set default '--cpp-std C++0x'

* Fix code generation (--cpp_std C++11) in CMakeLists.txt

- Fix dependency declaration of grpctest target

* Revert --cpp-std for the tests from explicit C++11 to flatc default value (C++0x)
2019-12-23 12:13:48 -08:00
svenk177 4525c91be3 Fix incorrect padding in arrays of structs (Issue #5484) (#5491) 2019-08-23 10:46:47 -07:00
Wouter van Oortmerssen b97b342f59 Fixed missing generated code.
Change-Id: Iaa0633167c2a4d4543fc4de2af58112d60d5d1e6
2019-08-19 17:57:23 -07:00
Wes McKinney c1058a903b C++ IDL generation adds superfluous semicolon in GenTablePost, causing (#5483)
-Wextra-semi-stmt warning when building with Clang 8.0.0. Update generated
files for testing
2019-08-19 17:49:37 -07:00
svenk177 e635141d5b Add support for fixed-size arrays (#5313) 2019-06-18 00:15:13 +02:00