Commit Graph

101 Commits

Author SHA1 Message Date
Vladimir Glavnyy d44931656a Fix high certainty warnings from PVS-studio (#5115)
* Fix high certainty warnings from PVS-studio

- Introduced FLATBUFFERS_ATTRIBUTE macro to use [[attribute]] if modern C++ compiler used

* Update the note about __cplusplus usage in the MSVC
2019-01-17 09:49:27 -08:00
tira-misu dba962ebb8 Enable flatbuffer to initialize Parser from bfbs (#4283) (#5077)
* Enable flatbuffer to initialize Parser from bfbs (#4283)

Now its possible to generate json data from bfbs data type and flatbuffers data
and visa versa.

* add deserialize functionality in parser from bfbs
* add small usage sample

* Fix build break

* Merge branch 'pr/1' into fix-issue4283

* Fix buildbreak

* Build monster_test.bfbs with --bfbs-builtins

Attribute flexbuffer has be included in bfbs. Only with this attribute test
will run. By initialization a parser by a bfbs the attribute has to be known
for this filed. monsterdata_test.golden has a flexbuffer field so parse would
fail.

* Fix generate_code.sh

* Revert automatic indent changes by IDE

* Auto detect size prefixed binary schema files

* Use identifier (bfbs) to detect schema files
2018-12-13 11:59:27 -08:00
Wouter van Oortmerssen f575b02fda Make string/vector field serialization order not depend on optimizer.
Multiple calls of e.g. CreateString inside a call to a CreateTable
could cause those strings to end up in different locations in the
wire format, since order or argument evaluation is undefined.

This is allowed by the FlatBuffer format, but it is not helpful,
especially when debugging the contents of binaries, or comparing
against a "golden" binary for tests etc.

Now making sure that all the CreateTableDirect calls first serialize
sub strings/vectors before calling CreateTable.

Also made similar changes to the serialization of "binary schemas".

Change-Id: I5747c4038b37a0d400aca2bc592bec751cf5c172
2018-11-16 17:08:35 -08:00
Wouter van Oortmerssen 705577de51 Revert "Convert net/FlatBuffers project to be compatible with netstandard (#4811)"
This reverts commit 72b05bc865.

Change-Id: I17510b7fa49b9861ce0a870b16f5c14a41082db5
2018-10-29 17:21:30 -07:00
Frank Benkstein ca417426c7 make flatbuffers::IsFieldPresent safer (#4988)
Give the vtable offset enum inside each table the name
"FlatBuffersVTableOffset" and base type voffset_t so it can be used as a
dependent type in IsFieldPresent. This makes that function slightly
safer since it prevents calling it with arbitrary, non-table types.
Now, the only way to use IsFieldPresent incorrectly is to create your
own type which does not inherit from flatbuffers::Table but has a
dependent voffset convertible type "FlatBuffersVTableOffset".
2018-10-22 15:57:45 -07:00
Frank Benkstein 99fe1dc80f don't crash when calling EnumNameXXX on sparse enum (#4982)
Make an out-of-bounds check for enum values before using them to index the
names array.  For consistency with non-sparse enums an empty string is
returned.

Fixes #4821
2018-10-08 14:37:35 -07:00
Michael Edwards a4f9d1bfcc Fix expected type of enum values in reflection tables (#4944)
Fixes #4930
2018-10-08 14:29:29 -07:00
Stewart Miles 569492e890 Disable armeabi builds for Android and re-enable CI builds. (#4970)
armeabi support was removed from the Android NDK so we should no
longer build it.  Since this fixes the Android build failures this
commit also re-enables Travis Android builds.

While re-enabling Android builds, some recent changes broke C++98
support so this fixes those issues as well which include:
- Conditionally compiling use of move constructors, operators and
  std::move.
- Changing sample to use flatbuffers::unique_ptr rather than
  std::unique_ptr.

Finally, added the special "default_ptr_type" value for the
"cpp_ptr_type" attribute.  This expands to the value passed to
the "--cpp-ptr-type" argument of flatc.
2018-10-08 12:43:57 -07:00
Thomas 33791dc7b0 Add compare operator to code generated for c++ (#4940)
* Add operator== for c++ genated code

New "--gen-compare" option for flatc to generate compare operators. The operators are defined based on object based api types.

Inspired by issue #263.

* Improve compare operator for c++.
Thanks for the code review.

- Improve robustness against future schema extensions
- Code style
- Fix --rust generation in generate_code.sh
2018-09-21 16:53:59 -07:00
Robert 3c54fd964b Port FlatBuffers to Rust (#4898)
This is a port of FlatBuffers to Rust. It provides code generation and a
runtime library derived from the C++ implementation. It utilizes the
Rust type system to provide safe and fast traversal of FlatBuffers data.

There are 188 tests, including many fuzz tests of roundtrips for various
serialization scenarios. Initial benchmarks indicate that the canonical
example payload can be written in ~700ns, and traversed in ~100ns.

Rustaceans may be interested in the Follow, Push, and SafeSliceAccess
traits. These traits lift traversals, reads, writes, and slice accesses
into the type system, providing abstraction with no runtime penalty.
2018-09-02 18:26:55 -07:00
aardappel 4898809eca FlatBuffers implementation for the Lobster programming language
Language, see: http://strlen.com/lobster/ and https://github.com/aardappel/lobster
2018-07-29 13:23:00 -07:00
Wouter van Oortmerssen 79f2adc50a Renamed Verifier methods
The name Verify was getting too overloaded, and confused
the VS compiler

Change-Id: I26423a4d513e4def2f4e41d7f278bb683fc12518
2018-07-16 16:42:10 -07:00
Damien Pontifex 72b05bc865 Convert net/FlatBuffers project to be compatible with netstandard (#4811) 2018-07-16 09:13:36 -07:00
Derek Bailey ba5eb3b5cf Lua (5.3) Language addition (#4804)
* starting Lua port of python implmention. Syncing commit

* Bulk of Lua module port from Python done. Not tested, only static analysis. Need to work on binary strings. Started work on flatc lua code generation

* Fixed all the basic errors to produced a binary output from the builder, don't know if it is generated correctly, but it contains data, so that must be good

* fixed binary set command that was extending the array improperly

* continued improvement

* Moved lua submodules down a directory so their names don't clash with potential other modules. Added compat module to provide Lua versioning logic

* Successful sample port from Python

* working on testing Lua code with formal tests

* continued to work on tests and fixes to code to make tests pass

* Added reading buffer test

* Changed binaryarray implmentation to use a temporary table for storing data, and then serialize it to a string when requested. This double the rate of building flatbuffers compared to the string approach.

* Didn't need encode module as it just added another layer of indirection that isn't need

* profiled reading buffers, optimizations to increase read performance of monster data to ~7 monster / millisecond

* Writing profiler improvments. Get about
~2 monsters/millisecond building rate

* removed Numpy generation from Lua (came from the Python port)

* math.pow is deprecated in Lua 5.3, so changed to ^ notation. Also added .bat script for starting Lua tests

* adding results of generate_code.bat

* simple edits for code review in PR.

* There was a buffer overflow in inserting the keywords into the unorder set for both the Lua and Python code gens. Changed insertion to use iterators.

* fixed spacing issue

* basic documenation/tutorial updates. Updated sample_binary.lua to reflect the tutorial better

* removed windows-specific build step in Lua tests
2018-07-05 15:55:57 -07:00
Dan Field 88912640d0 Add [Dart] support (#4676)
* Add [Dart] support

* fix enum vectors

* Allow for opt out of string interning

* fix comment style, make interning opt in

* remove Offset<T>, prefer int

* avoid creating unnecessary vtable objects

* start work on tests - do not generate builder if struct has 0 fields - add int64

* support reading structs properly

* correctly handle reading vectors of structs, dartfmt

* support structs, fix unnecessary prepares

* fix bool customizations

* undo unintentional removal of file

* docs updates, complete tutorial, bug fix for codegen

* more documentation

* Update docs, add to doxygen file

* update package structure, add samples script/code

* rearrange sample

* Tests

* Add readme for pub

* cleanup package for pub

* update docs for renamed file

* remove custom matcher, use `closeTo` instead

* remove unintentional file

* remove unintended file checkin

* use auto, move method, cleanup

* refactor to ObjectBuilders, add Builders

* Update tests, examples

* Add files missing from previous commit

* documentation and example updates

* Update LICENSE, make dartanalyzer happy, fix minor bugs, get rid of duplicate files, publish script

* fix sample for slightly different schema

* Update pubspec.yaml
2018-05-18 11:06:15 -07:00
Paul Reimer af3c598189 Rename MANUALLY_ALIGNED_STRUCT to add FLATBUFFERS_ prefix in generated code within tests/. Via running `cd tests && sh generate_code.sh` (#4696) 2018-04-05 16:36:42 -07:00
Paul Reimer eac0bc6490 Add FLATBUFFERS_ prefix to defines [C++] (#4695)
* Rename STRUCT_END to add FLATBUFFERS_ prefix, now FLATBUFFERS_STRUCT_END. Via running `ag -l STRUCT_END | xargs rpl STRUCT_END FLATBUFFERS_STRUCT_END`

* Rename MANUALLY_ALIGNED_STRUCT to add FLATBUFFERS_ prefix, now FLATBUFFERS_MANUALLY_ALIGNED_STRUCT. Via running `ag -l MANUALLY_ALIGNED_STRUCT | xargs rpl MANUALLY_ALIGNED_STRUCT FLATBUFFERS_MANUALLY_ALIGNED_STRUCT && cd tests && sh generate_code.sh`

* Rename DEFINE_BITMASK_OPERATORS to add FLATBUFFERS_ prefix, now FLATBUFFERS_DEFINE_BITMASK_OPERATORS. Via running `ag -l DEFINE_BITMASK_OPERATORS | xargs rpl DEFINE_BITMASK_OPERATORS FLATBUFFERS_DEFINE_BITMASK_OPERATORS`
2018-04-05 16:00:54 -07:00
Flaviu 79f62ee353 Const correctness in generated code and in code generators. Added missing \reflection\generate_code.bat file. (#4679) 2018-03-23 08:58:07 -07:00
desqaz 132e6a8220 Add new c++ helpers to ease usage of size prefixed FlatBuffers (#4626)
It was missing some helpers when we choose to use
size prefixed FlatBuffers.

* Add general helper : GetPrefixedSize
* Add generated helpers :
 * GetSizePrefixedXXX
 * VerfifySizePrefixedXXXBuffer
 * FinishSizePrefixedXXXBuffer
2018-02-22 09:19:08 -08:00
Alexander Gallego 36f8564846 C++: mini_reflect: Add DefaultTypeTable (#4614)
* mini_reflect: Add DefaultTypeTable

Currently it's very easy to make a mistake when it comes to
instantiating the TypeTable to print a buffer because it is not type
safe.

This will allow us to write safer cpp code:

flatbuffers::FlatBufferToString(reinterpret_cast<const uint8_t *>(&t),
                                decltype(t)::DefaultTypeTable());

* c++: mini_reflect: update generated code

* Ensure types and names are set for mini_reflect

* c++: mini_refelct: update unit tests with new typed TypeTable

* Adding PR feedback of sylte and naming convention
2018-02-15 14:58:06 -08:00
Wouter van Oortmerssen 0c86929e39 Added missing nullptr check in vector of union verifiers.
Change-Id: Iec720991e68ad27580537135bfd8eb4159333921
Tested: on Linux.
2017-12-28 10:09:48 -08:00
Wouter van Oortmerssen 89711c9c47 Made all C++ files clang-formatted.
Also added missing generated files.

Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
2017-12-21 11:45:02 -08:00
Wouter van Oortmerssen be1ad33910 Missing generated code files.
Change-Id: I8808b0b419981ba7d0699da4a1effb0b660a21cb
2017-11-20 09:19:35 -08:00
Sergey Avseyev 6eb031de9a Text files should not have executable bit set (#4480) 2017-11-06 10:20:22 -08:00
Aaron 5b4acf809e [C++] Add explicit keyword on generated constructors for Builder structs (#4452)
* Add explicit keyword on C++ generated constructors for Builder structs

* Add missing generated code for review
2017-10-06 08:50:24 -07:00
Wouter van Oortmerssen 7a6b2bf521 Fixed TypeTable function not being inline.
Change-Id: Id335980daf909e885ed26a21b64623b78d01fb49
Tested: on OS X.
2017-09-25 11:44:58 -07:00
Wouter van Oortmerssen 72a99abfb7 Added support for mini-reflection tables.
Change-Id: I83453d074685fa57bbf1c7c87b1d9392ce972085
Tested: on Linux.
2017-09-22 16:17:02 -07:00
endorph-soft 21a8121982 Add constant accessors to C++ unions (#4433)
* Add constant accessors to C++ unions

* Remove redundant const pointer return type

* Update generate_code.bat to reflect generate_code.sh

* Add updated generated files

* Remove extra space from generated code

* Update generated files

* Change directory back to tests after generating code
2017-09-21 14:06:20 -07:00
Wouter van Oortmerssen ac1015e3c4 Trimmed vtables of trailing zeroes.
This is something the format supports, but none of the builders
were doing. Can save 10-20% on FlatBuffer binary size!

Also fixed the Go tests.

Change-Id: I616c56ce9bbcfcaee23aa24f0532fcb60b6a8c75
Tested: on Linux.
2017-08-24 09:35:54 -07:00
Wouter van Oortmerssen 7fe281295f Fixed clang-tidy warnings.
Change-Id: Ib94ef29254c116b60e4ff298baa2ce323ac9655c
Tested: on Linux.
2017-08-07 15:33:36 -07:00
Stewart Miles a892322203 C++98 (stlport) support for core FlatBuffers and FlexBuffers.
* Added internal - limited - implementation of flatbuffers::unique_ptr
  for STLs that don't ship with std::unique_ptr.  In C++11 and beyond
  this is just an alias for std::unique_ptr.
* Aliased used type traits structs is_scalar is_floating_point is_unsigned
  into flatbuffers namespace so they can be replaced in C++98 implementations.
  Right now these point at stlport's TR1 implementations.
* Wrapped vector::data() in vector_data().
* Wrapped vector::emplace_back() in vector_emplace_back().
* Wrapper string::back() in string_back().
* Added variants of FlatBufferBuilder::CreateVector() and
  FlatBufferBuilder::CreateVectorOfStructs() that allow the use of plain
  function pointers.
  Generated code has also been modified to use plain functions to build objects
  rather than std::function() so all generated code will work in C++98
  applications.
* Added flexbuffers::Builder::Vector(), flexbuffers::Builder::TypedVector()
  and flexbuffers::Builder::Map() methods that allow the use of plain function
  pointers.
* Changed Parser to internally use plain function pointers when parsing table
  and vector delimiters.
* Added specializations of NumToString() for 64-bit types that aren't supported
  by stringstream in stlport.
* Overloaded numeric_limits for 64-bit types not supported by stlport.
* Replaced build_apk.sh (which was broken by deprecation of the
  "android" tool in the Android SDK) with build.gradle and the
  appropriate gradle wrapper to build an APK.
* Switched Android build to build against all STL variants.
* Updated travis configuration to build Android test and sample.

Tested:
* Verified all tests continue to work on Linux, OSX and Android.
* Verified Travis build is green.

Change-Id: I9e634363793f85b9f141d21454b10686020a2065
2017-07-20 11:01:00 -07:00
rufeooo dadd1a926e Generate a C++ function for EnumValues{{ENUM_NAME}} (#4337)
* enables "for each" logic on enumeration types
2017-06-07 13:49:56 -07:00
Wouter van Oortmerssen 398ae0cb6b Some code in the samples and docs still used old C# API.
Change-Id: I7914c88ad7b31baa7586771423069dc2b90d534f
Tested: on Linux.
2017-05-22 15:14:46 -07:00
Wouter van Oortmerssen 8b92122f33 Made the verifier catch zero-offsets.
Zero offsets are non-sensical in FlatBuffers (since offsets are
relative to themselves) but were allowed by the verifier. This could
cause buffers made up of all zeroes to be interpreted as correct
buffers with an empty root object.

Generally, not allowing such offsets will make the verifier more
likely to catch problems earlier.

Change-Id: I54010bea29721b326ff8e5348fcd9fe78e5e7506
Tested: on Linux.
2017-04-26 14:26:18 -07:00
Wouter van Oortmerssen 523f3833eb VS fix for ambiguous union assignment operator.
Change-Id: I1c37db1ced462fd558d3e893a501341f3eca6379
2017-04-17 18:00:41 -07:00
Wouter van Oortmerssen e093f72d00 Improved union copy constructor.
It now at least works in simple cases.

Change-Id: I3af0738e676e62166b69accaa6bd19f531fbe5ee
Tested: on Linux.
2017-04-17 17:27:20 -07:00
Wouter van Oortmerssen 728bb64fed Fixed clang needing union copy constructor.
Move constructors are present, which it should use instead.

This is a temp fix to make it compile, but eventually we should
generate a proper copy constructor just in-case people want to
copy objects with unions.

Tested on: Linux, OS X.

Change-Id: Idf85419995c96f5959061882157541573e306083
2017-04-17 14:01:43 -07:00
Wouter van Oortmerssen b0752e179b Added support for structs and strings in unions.
(C++ only for now).
Also fixed vector of union support in the object API.

Bug: 36902939
Change-Id: I935f4cc2c303a4728e26c7916a8ec0adcd6f84cb
Tested: on Linux.
2017-04-12 17:47:47 -07:00
moti 2aec880347 [C++]Use noexcept in union type move ctor/Add move assingment (#4249)
* Use noexcept in union type move ctor/Add move assingment

* Add NOEXCEPT macro to deal with _MS_VER/Remove delegating ctor in union type class

* Add FLATBUFFERS_NOEXCEPT to generated union class
2017-03-29 18:00:20 -07:00
Lawrence Chan 1a27c7017a C++: Add default value handling to mutation/SetField code (#4230)
* Add default value handling to mutation/SetField code

* Shorten reflection SetField impl

* Modify impl to work with C++03

* Add more mutation tests

* Fail SetField if non-scalar

* Add IsScalar/IsInteger/IsFloat for reflection::BaseType

* Use new IsScalar/IsInteger/IsFloat in reflection SetField

* Assume scalar is either int or float
2017-03-20 17:36:27 -07:00
Koichi Shiraishi c6aae45364 sample: update auto-generated monster_generated.h (#4219)
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
2017-03-15 14:52:47 -07:00
Louis-Paul CORDIER bb223da258 C++ generator: Pre-allocating std::vector in UnPack methods. (#4187) 2017-02-25 22:10:17 -08:00
Andrei Lebedev 17c5f89d4f Fixed move constructor in generated union class (#4192)
* Fixed move constructor in generated union class

* Removed delegating constructor
2017-02-25 22:06:48 -08:00
Wouter van Oortmerssen 2df3d1c965 Added missing generated code.
Change-Id: If3d2e11627b6d08bc6433941208ead577aa0e3b9
2017-02-10 13:28:15 -08:00
水樹素子 b7bfecb4ee Add move constructor to generated union class. (#4167)
* Add move constructor to generated union class.

* Unused default

* Add generated code
2017-02-10 10:32:34 -08:00
Wouter van Oortmerssen 87e29b25de Some small speed optimizations to the core copying functions.
Change-Id: Id8c1afb84f4ab0e2edca4290e3de5589fa06e578
Tested: on Linux.
2017-01-27 11:26:35 -08:00
Bei Li 68bbe983e9 Union Vector 2017-01-24 11:52:36 -08:00
Wouter van Oortmerssen 3f936c5655 More native code gen functionality.
Allow tables to be mapped to native types directly.  For example, a table
representing a vector3 (eg. table Vec3 { x:float; y:float; z:float; }) can
be mapped to a "mathfu::vec3" native type in NativeTables.  This requires
users to provide Pack and UnPack functions that convert between the
Table and native types.  This is done by adding the "native_type" attribute
to the table definition.

To support user-defined flatbuffers::Pack and flatbuffers::UnPack functions,
support a "native_include" markup that will generate a corresponding

Also add an UnPackTo function which allows users to pass in a pointer to
a NativeTable object into which to UnPack the Table.  The existing UnPack
function is now simply:

  NativeTable* UnPack() {
    NativeTable* obj = new NativeTable();
    Table::UnPackTo(obj);
    return obj;
  }

Finally, allow native types to be given a default value as well which are
set in the NativeTable constructor.  This is done by providing a
"native_default" attribute to the member of a table.

Change-Id: Ic45cb48b0e6d7cfa5734b24819e54aa96d847cfd
2017-01-18 16:23:35 -08:00
Wouter van Oortmerssen 7b94eab2b1 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
2017-01-13 17:44:42 -08:00
Pascal S. de Kloe b55f18649a Resolve Go fmt and vet warnings (#4134)
* Resolve Go fmt and vet warnings.

* Undo generated code gofmt.
2017-01-09 17:13:44 -08:00